|
@@ -36,6 +36,13 @@ func IsExist(path string) bool {
|
|
|
|
|
|
// CurrentUsername returns the username of the current user.
|
|
|
func CurrentUsername() string {
|
|
|
+ // To get the real user of the current process, you should use user.Current(),
|
|
|
+ // and USER may not be the real user of the process (for example, executing it
|
|
|
+ // through sudo will display the original user)
|
|
|
+ if currentUser, err := user.Current(); err == nil && currentUser != nil && len(currentUser.Username) > 0 {
|
|
|
+ return currentUser.Username
|
|
|
+ }
|
|
|
+
|
|
|
username := os.Getenv("USER")
|
|
|
if len(username) > 0 {
|
|
|
return username
|
|
@@ -45,9 +52,5 @@ func CurrentUsername() string {
|
|
|
if len(username) > 0 {
|
|
|
return username
|
|
|
}
|
|
|
-
|
|
|
- if user, err := user.Current(); err == nil {
|
|
|
- username = user.Username
|
|
|
- }
|
|
|
return username
|
|
|
}
|