use EUID if $USER is unset or does not resolve to a POSIX account

This commit is contained in:
Kenneth Bingham
2025-05-02 13:27:48 -04:00
parent 3f5db643aa
commit 4342dbe89d
2 changed files with 12 additions and 7 deletions
+8 -7
View File
@@ -56,14 +56,15 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) {
panic(err)
}
var username string
user, err := user2.Current()
if err != nil {
username := os.Getenv("USER")
if username == "" {
logrus.Panicf("unable to determine the current user: %v", err)
}
userObj, err := user2.Current()
if err == nil && userObj.Username != "" {
username = userObj.Username
} else {
username = user.Username
username = os.Getenv("USER")
if username == "" {
username = fmt.Sprintf("user-%d", os.Geteuid())
logrus.Warnf("unable to determine the current user, using effective UID: %v", err)
}
}
hostDetail = fmt.Sprintf("%v; %v", username, hostDetail)
if cmd.description == "<user>@<hostname>" {