Skip to content

Commit

Permalink
Using OS targeted go files to separate out the username logic.
Browse files Browse the repository at this point in the history
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
phillipsj committed Nov 17, 2021
1 parent 9ad2462 commit 16e27a9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
34 changes: 0 additions & 34 deletions klog_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import (
"errors"
"fmt"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -57,38 +55,6 @@ func init() {
}
}

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.
if runtime.GOOS == "windows" {
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
}
} else {
current, err := user.Current()
if err == nil {
userName = current.Username
}
}
})

return userName
}

// shortHostname returns its argument, truncating at the first period.
// For instance, given "www.google.com" it returns "www".
func shortHostname(hostname string) string {
Expand Down
19 changes: 19 additions & 0 deletions klog_file_others.go
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
}
34 changes: 34 additions & 0 deletions klog_file_windows.go
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
}

0 comments on commit 16e27a9

Please sign in to comment.