You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.go 720B

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package user
  4. import (
  5. "os"
  6. "os/user"
  7. "runtime"
  8. "strings"
  9. )
  10. // CurrentUsername return current login OS user name
  11. func CurrentUsername() string {
  12. userinfo, err := user.Current()
  13. if err != nil {
  14. return fallbackCurrentUsername()
  15. }
  16. username := userinfo.Username
  17. if runtime.GOOS == "windows" {
  18. parts := strings.Split(username, "\\")
  19. username = parts[len(parts)-1]
  20. }
  21. return username
  22. }
  23. // Old method, used if new method doesn't work on your OS for some reason
  24. func fallbackCurrentUsername() string {
  25. curUserName := os.Getenv("USER")
  26. if len(curUserName) > 0 {
  27. return curUserName
  28. }
  29. return os.Getenv("USERNAME")
  30. }