forked from golang/glog
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using OS targeted go files to separate out the username logic.
We have discovered that in some instances the runtime.GOOS="windows" check is failing on nanoserver when doing cross compilation. It appears to be inconsistent. Moving this into it's own OS targeted go file prevents it from being an issue. Signed-off-by: Jamie Phillips <jamie.phillips@suse.com>
- Loading branch information
Showing
3 changed files
with
53 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package klog | ||
|
||
import ( | ||
"os/user" | ||
) | ||
|
||
func getUserName() string { | ||
userNameOnce.Do(func() { | ||
current, err := user.Current() | ||
if err == nil { | ||
userName = current.Username | ||
} | ||
}) | ||
|
||
return userName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package klog | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
func getUserName() string { | ||
userNameOnce.Do(func() { | ||
// On Windows, the Go 'user' package requires netapi32.dll. | ||
// This affects Windows Nano Server: | ||
// https://github.com/golang/go/issues/21867 | ||
// Fallback to using environment variables. | ||
u := os.Getenv("USERNAME") | ||
if len(u) == 0 { | ||
return | ||
} | ||
// Sanitize the USERNAME since it may contain filepath separators. | ||
u = strings.Replace(u, `\`, "_", -1) | ||
|
||
// user.Current().Username normally produces something like 'USERDOMAIN\USERNAME' | ||
d := os.Getenv("USERDOMAIN") | ||
if len(d) != 0 { | ||
userName = d + "_" + u | ||
} else { | ||
userName = u | ||
} | ||
}) | ||
|
||
return userName | ||
} |