osutil.go 766 B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package osutil
  5. import (
  6. "os"
  7. )
  8. // IsFile returns true if given path exists as a file (i.e. not a directory).
  9. func IsFile(path string) bool {
  10. f, e := os.Stat(path)
  11. if e != nil {
  12. return false
  13. }
  14. return !f.IsDir()
  15. }
  16. // IsExist returns true if a file or directory exists.
  17. func IsExist(path string) bool {
  18. _, err := os.Stat(path)
  19. return err == nil || os.IsExist(err)
  20. }
  21. // CurrentUsername returns the current system user via environment variables.
  22. func CurrentUsername() string {
  23. curUserName := os.Getenv("USER")
  24. if len(curUserName) > 0 {
  25. return curUserName
  26. }
  27. return os.Getenv("USERNAME")
  28. }