]> Cypherpunks repositories - gostls13.git/commitdiff
os/user: make Current return better error w/o cgo & complete environment
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 9 May 2019 21:04:39 +0000 (21:04 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 9 May 2019 21:47:43 +0000 (21:47 +0000)
Fixes #31949

Change-Id: Ib96a43e4c56a00c5ba04e4d213255a063058ae08
Reviewed-on: https://go-review.googlesource.com/c/go/+/176337
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/os/user/lookup_stubs.go

index 6a20d787810dde36558f7e7694fba737b67285b1..d3acbddd18019e9a469cca395bf015145468a313 100644 (file)
@@ -57,7 +57,17 @@ func current() (*User, error) {
        if u.Uid != "" && u.Username != "" && u.HomeDir != "" {
                return u, nil
        }
-       return u, fmt.Errorf("user: Current not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
+       var missing string
+       if u.Username == "" {
+               missing = "$USER"
+       }
+       if u.HomeDir == "" {
+               if missing != "" {
+                       missing += ", "
+               }
+               missing += "$HOME"
+       }
+       return u, fmt.Errorf("user: Current requires cgo or %s set in environment", missing)
 }
 
 func listGroups(*User) ([]string, error) {