diff --git a/cmd/mozzle/homedir.go b/cmd/mozzle/homedir.go new file mode 100644 index 0000000..074ce76 --- /dev/null +++ b/cmd/mozzle/homedir.go @@ -0,0 +1,18 @@ +// +build !windows + +package main + +import ( + "os" +) + +func homeDir() string { + var homeDir string + switch { + case os.Getenv("CF_HOME") != "": + homeDir = os.Getenv("CF_HOME") + default: + homeDir = os.Getenv("HOME") + } + return homeDir +} diff --git a/cmd/mozzle/homedir_windows.go b/cmd/mozzle/homedir_windows.go new file mode 100644 index 0000000..f82ea78 --- /dev/null +++ b/cmd/mozzle/homedir_windows.go @@ -0,0 +1,22 @@ +// +build windows + +package main + +import ( + "os" +) + +func homeDir() string { + var homeDir string + switch { + case os.Getenv("CF_HOME") != "": + homeDir = os.Getenv("CF_HOME") + case os.Getenv("HOMEDRIVE")+os.Getenv("HOMEPATH") != "": + homeDir = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") + default: + homeDir = os.Getenv("USERPROFILE") + } + + return homeDir + +} diff --git a/cmd/mozzle/main.go b/cmd/mozzle/main.go index 25ea642..ec9feb9 100644 --- a/cmd/mozzle/main.go +++ b/cmd/mozzle/main.go @@ -163,11 +163,7 @@ type cliConfig struct { // The default location is CF_HOME/.cf/config.json. // If the CF_HOME env variable is not set, it defaults to HOME env variable. func cfcliConfig() (*cliConfig, error) { - var path string - if path = os.Getenv("CF_HOME"); path == "" { - path = os.Getenv("HOME") - } - path = filepath.Join(path, ".cf/config.json") + path := filepath.Join(homeDir(), ".cf", "config.json") fd, err := os.Open(path) if err != nil { return nil, errors.Wrapf(err, "error open %q", path)