| 1 | //go:build windows |
| 2 | // +build windows |
| 3 | |
| 4 | package klog |
| 5 | |
| 6 | import ( |
| 7 | "os" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | func getUserName() string { |
| 12 | userNameOnce.Do(func() { |
| 13 | // On Windows, the Go 'user' package requires netapi32.dll. |
| 14 | // This affects Windows Nano Server: |
| 15 | // https://github.com/golang/go/issues/21867 |
| 16 | // Fallback to using environment variables. |
| 17 | u := os.Getenv("USERNAME") |
| 18 | if len(u) == 0 { |
| 19 | return |
| 20 | } |
| 21 | // Sanitize the USERNAME since it may contain filepath separators. |
| 22 | u = strings.Replace(u, `\`, "_", -1) |
| 23 | |
| 24 | // user.Current().Username normally produces something like 'USERDOMAIN\USERNAME' |
| 25 | d := os.Getenv("USERDOMAIN") |
| 26 | if len(d) != 0 { |
| 27 | userName = d + "_" + u |
| 28 | } else { |
| 29 | userName = u |
| 30 | } |
| 31 | }) |
| 32 | |
| 33 | return userName |
| 34 | } |