From 808daec5ea969b8888c1ba3804319421bb73dda0 Mon Sep 17 00:00:00 2001 From: Nils Lagerkvist Date: Mon, 22 Feb 2016 21:32:36 +0100 Subject: [PATCH] Added fallback when looking up home directory. The problem is cross-compiling with cgo to darwin --- config.go | 41 ++++++++++++++++++++++++++++++++++++----- main.go | 9 ++++----- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/config.go b/config.go index 6a34c96..cd39917 100644 --- a/config.go +++ b/config.go @@ -5,6 +5,7 @@ import ( "os" "os/user" "path/filepath" + "runtime" ) // Config hold configuration read from the configuration file @@ -77,12 +78,12 @@ func doCheck(fn func() (bool, error), err error) (bool, error) { } func activeAWSConfigFile() (active bool, err error) { - usr, err := user.Current() + home, err := homeDir() if err != nil { return false, fmt.Errorf("unable to fetch user information: %v", err) } - if _, err := os.Stat(filepath.Join(usr.HomeDir, awsConfDir, awsConfigFile)); os.IsNotExist(err) { + if _, err := os.Stat(filepath.Join(home, awsConfDir, awsConfigFile)); os.IsNotExist(err) { return false, nil } @@ -90,12 +91,12 @@ func activeAWSConfigFile() (active bool, err error) { } func activeAWSCredentialsFile() (active bool, err error) { - usr, err := user.Current() + home, err := homeDir() if err != nil { - return false, err + return false, fmt.Errorf("unable to fetch user information: %v", err) } - if _, err := os.Stat(filepath.Join(usr.HomeDir, awsConfDir, awsCredentialsFile)); os.IsNotExist(err) { + if _, err := os.Stat(filepath.Join(home, awsConfDir, awsCredentialsFile)); os.IsNotExist(err) { return false, nil } @@ -114,3 +115,33 @@ func activeAWSEnvironment() (active bool, err error) { return false, nil } + +func homeDir() (homeDir string, err error) { + usr, err := user.Current() + if err == nil { + return usr.HomeDir, nil + } + + // fallback to environemt variables + var home string + if runtime.GOOS == "windows" { + home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") + if home != "" { + return home, nil + } + + home = os.Getenv("USERPROFILE") + if home != "" { + return home, nil + } + + return "", fmt.Errorf("fallback failed, set `USERPROFILE` environment variable") + } + + home = os.Getenv("HOME") + if home != "" { + return home, nil + } + + return "", fmt.Errorf("fallback failed, set `HOME` environment variable") +} diff --git a/main.go b/main.go index a712fa2..475b650 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "errors" "log" "os" - "os/user" "path/filepath" "github.com/bobziuchkovski/writ" @@ -137,12 +136,12 @@ func setDefaultSocketAdress(adress string) string { return adress } - usr, err := user.Current() + home, err := homeDir() if err != nil { log.Fatalf("unable to extract user information: %v", err) } - return filepath.Join(usr.HomeDir, domainSocketPath) + return filepath.Join(home, domainSocketPath) } func setDefaultConfigPath(path string) string { @@ -150,12 +149,12 @@ func setDefaultConfigPath(path string) string { return path } - usr, err := user.Current() + home, err := homeDir() if err != nil { log.Fatalf("unable to extract user information: %v", err) } - return filepath.Join(usr.HomeDir, configFilePath) + return filepath.Join(home, configFilePath) } func main() {