Commit fa1a49aa authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

os: add UserHomeDir

Fixes #26463

Change-Id: Iaef1c7456ffaeadeead6027a37d09c44a3d05bd5
Reviewed-on: https://go-review.googlesource.com/c/139418Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 86e251c9
...@@ -76,32 +76,29 @@ func ExampleIsNotExist() { ...@@ -76,32 +76,29 @@ func ExampleIsNotExist() {
// file does not exist // file does not exist
} }
func init() {
os.Setenv("USER", "gopher")
os.Setenv("HOME", "/usr/gopher")
os.Unsetenv("GOPATH")
}
func ExampleExpand() { func ExampleExpand() {
mapper := func(placeholderName string) string { mapper := func(placeholderName string) string {
switch placeholderName { switch placeholderName {
case "DAY_PART": case "DAY_PART":
return "morning" return "morning"
case "USER": case "NAME":
return "Gopher" return "Gopher"
} }
return "" return ""
} }
fmt.Println(os.Expand("Good ${DAY_PART}, $USER!", mapper)) fmt.Println(os.Expand("Good ${DAY_PART}, $NAME!", mapper))
// Output: // Output:
// Good morning, Gopher! // Good morning, Gopher!
} }
func ExampleExpandEnv() { 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: // Output:
// gopher lives in /usr/gopher. // gopher lives in /usr/gopher.
...@@ -117,16 +114,24 @@ func ExampleLookupEnv() { ...@@ -117,16 +114,24 @@ func ExampleLookupEnv() {
} }
} }
show("USER") os.Setenv("SOME_KEY", "value")
show("GOPATH") os.Setenv("EMPTY_KEY", "")
show("SOME_KEY")
show("EMPTY_KEY")
show("MISSING_KEY")
// Output: // Output:
// USER=gopher // SOME_KEY=value
// GOPATH not set // EMPTY_KEY=
// MISSING_KEY not set
} }
func ExampleGetenv() { 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: // Output:
// gopher lives in /usr/gopher. // gopher lives in /usr/gopher.
......
...@@ -381,6 +381,24 @@ func UserCacheDir() (string, error) { ...@@ -381,6 +381,24 @@ func UserCacheDir() (string, error) {
return dir, nil 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. // 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 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. // If there is an error, it will be of type *PathError.
......
...@@ -2333,3 +2333,17 @@ func TestDoubleCloseError(t *testing.T) { ...@@ -2333,3 +2333,17 @@ func TestDoubleCloseError(t *testing.T) {
t.Logf("second close returned expected error %q", err) 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())
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment