From e2fdd335415d39083ef9ff5950dd4050f25b193a Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Fri, 8 Jan 2016 12:35:57 +0100 Subject: common: Fix HomeDir detection I am working on porting geth to [Ubuntu Core](https://developer.ubuntu.com/en/snappy/https://developer.ubuntu.com/en/snappy/). I am testing geth on a Raspberry PI and for Ubuntu Core the $HOME directory is unique for each application. See [here](https://developer.ubuntu.com/en/snappy/guides/filesystem-layout) for more information of their filesystem layout. For some reason in Go `usr.HomeDir` returns a different value than `$HOME` in Ubuntu Core. Adding this at the end of `HomeDir()` ```go fmt.Printf("at HomeDir, user.HomeDir = %s and $HOME is %s\n", usr.HomeDir, os.Getenv("HOME")) ``` gives the following output ``` at HomeDir, user.HomeDir = /home/ubuntu and $HOME is /home/ubuntu/apps/geth.sideload/IJcODREBYbHO ``` With this commit, I propose giving precedence to the `$HOME` environment variable as is also suggested by the [homedir](https://github.com/mitchellh/go-homedir/blob/master/homedir.go) project. --- common/path.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/common/path.go b/common/path.go index 39eacacee..9ba2f93c0 100644 --- a/common/path.go +++ b/common/path.go @@ -63,13 +63,14 @@ func AbsolutePath(Datadir string, filename string) string { return filepath.Join(Datadir, filename) } -func HomeDir() (home string) { +func HomeDir() string { + if home := os.Getenv("HOME"); home != "" { + return home + } if usr, err := user.Current(); err == nil { - home = usr.HomeDir - } else { - home = os.Getenv("HOME") + return usr.HomeDir } - return + return "" } func DefaultDataDir() string { -- cgit