Skip to content

Commit

Permalink
Added fallback when looking up home directory.
Browse files Browse the repository at this point in the history
The problem is cross-compiling with cgo to darwin
  • Loading branch information
otm committed Feb 22, 2016
1 parent fc0e325 commit 808daec
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
41 changes: 36 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/user"
"path/filepath"
"runtime"
)

// Config hold configuration read from the configuration file
Expand Down Expand Up @@ -77,25 +78,25 @@ 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
}

return true, nil
}

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
}

Expand All @@ -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")
}
9 changes: 4 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"log"
"os"
"os/user"
"path/filepath"

"github.com/bobziuchkovski/writ"
Expand Down Expand Up @@ -137,25 +136,25 @@ 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 {
if path != "" {
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() {
Expand Down

0 comments on commit 808daec

Please sign in to comment.