From: Brad Fitzpatrick Date: Wed, 3 Oct 2018 21:21:55 +0000 (+0000) Subject: os: add UserHomeDir X-Git-Tag: go1.12beta1~908 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=fa1a49aa556d8;p=gostls13.git os: add UserHomeDir Fixes #26463 Change-Id: Iaef1c7456ffaeadeead6027a37d09c44a3d05bd5 Reviewed-on: https://go-review.googlesource.com/c/139418 Reviewed-by: Ian Lance Taylor --- diff --git a/src/os/example_test.go b/src/os/example_test.go index 95a4a67817..5c96ebb417 100644 --- a/src/os/example_test.go +++ b/src/os/example_test.go @@ -76,32 +76,29 @@ func ExampleIsNotExist() { // file does not exist } -func init() { - os.Setenv("USER", "gopher") - os.Setenv("HOME", "/usr/gopher") - os.Unsetenv("GOPATH") -} - func ExampleExpand() { mapper := func(placeholderName string) string { switch placeholderName { case "DAY_PART": return "morning" - case "USER": + case "NAME": return "Gopher" } return "" } - fmt.Println(os.Expand("Good ${DAY_PART}, $USER!", mapper)) + fmt.Println(os.Expand("Good ${DAY_PART}, $NAME!", mapper)) // Output: // Good morning, Gopher! } func ExampleExpandEnv() { - fmt.Println(os.ExpandEnv("$USER lives in ${HOME}.")) + os.Setenv("NAME", "gopher") + os.Setenv("BURROW", "/usr/gopher") + + fmt.Println(os.ExpandEnv("$NAME lives in ${BURROW}.")) // Output: // gopher lives in /usr/gopher. @@ -117,16 +114,24 @@ func ExampleLookupEnv() { } } - show("USER") - show("GOPATH") + os.Setenv("SOME_KEY", "value") + os.Setenv("EMPTY_KEY", "") + + show("SOME_KEY") + show("EMPTY_KEY") + show("MISSING_KEY") // Output: - // USER=gopher - // GOPATH not set + // SOME_KEY=value + // EMPTY_KEY= + // MISSING_KEY not set } func ExampleGetenv() { - fmt.Printf("%s lives in %s.\n", os.Getenv("USER"), os.Getenv("HOME")) + os.Setenv("NAME", "gopher") + os.Setenv("BURROW", "/usr/gopher") + + fmt.Printf("%s lives in %s.\n", os.Getenv("NAME"), os.Getenv("BURROW")) // Output: // gopher lives in /usr/gopher. diff --git a/src/os/file.go b/src/os/file.go index cba70d78fb..eb2277dece 100644 --- a/src/os/file.go +++ b/src/os/file.go @@ -381,6 +381,24 @@ func UserCacheDir() (string, error) { return dir, nil } +// UserHomeDir returns the current user's home directory. +// +// On Unix, including macOS, it returns the $HOME environment variable. +// On Windows, it returns the concatenation of %HOMEDRIVE% and %HOMEPATH%. +// On Plan 9, it returns the $home environment variable. +func UserHomeDir() string { + if runtime.GOOS == "windows" { + return Getenv("HOMEDRIVE") + Getenv("HOMEPATH") + } + if runtime.GOOS == "plan9" { + return Getenv("home") + } + if runtime.GOOS == "nacl" { + return "/" + } + return Getenv("HOME") +} + // Chmod changes the mode of the named file to mode. // If the file is a symbolic link, it changes the mode of the link's target. // If there is an error, it will be of type *PathError. diff --git a/src/os/os_test.go b/src/os/os_test.go index 894105a886..08853691a9 100644 --- a/src/os/os_test.go +++ b/src/os/os_test.go @@ -2333,3 +2333,17 @@ func TestDoubleCloseError(t *testing.T) { t.Logf("second close returned expected error %q", err) } } + +func TestUserHomeDir(t *testing.T) { + dir := UserHomeDir() + if dir == "" { + t.Fatal("UserHomeDir returned an empty string") + } + fi, err := Stat(dir) + if err != nil { + t.Fatal(err) + } + if !fi.IsDir() { + t.Fatalf("dir %s is not directory; type = %v", dir, fi.Mode()) + } +}