github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/configuration/confighelpers/config_helpers.go (about) 1 package confighelpers 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "runtime" 8 ) 9 10 func homeDir() (string, error) { 11 var homeDir string 12 13 if os.Getenv("CF_HOME") != "" { 14 homeDir = os.Getenv("CF_HOME") 15 16 if _, err := os.Stat(homeDir); os.IsNotExist(err) { 17 return "", fmt.Errorf("Error locating CF_HOME folder '%s'", homeDir) 18 } 19 } else { 20 homeDir = userHomeDir() 21 } 22 23 return homeDir, nil 24 } 25 26 func DefaultFilePath() (string, error) { 27 homeDir, err := homeDir() 28 29 if err != nil { 30 return "", err 31 } 32 33 return filepath.Join(homeDir, ".cf", "config.json"), nil 34 } 35 36 // See: http://stackoverflow.com/questions/7922270/obtain-users-home-directory 37 // we can't cross compile using cgo and use user.Current() 38 var userHomeDir = func() string { 39 40 if runtime.GOOS == "windows" { 41 home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") 42 if home == "" { 43 home = os.Getenv("USERPROFILE") 44 } 45 return home 46 } 47 48 return os.Getenv("HOME") 49 } 50 51 var PluginRepoDir = func() string { 52 if os.Getenv("CF_PLUGIN_HOME") != "" { 53 return os.Getenv("CF_PLUGIN_HOME") 54 } 55 56 // Ignore the error here because we expect that the directory which is 57 // ostensibly created in this call shall have already been created by 58 // DefaultFilePath(). 59 pluginDir, _ := homeDir() 60 61 return pluginDir 62 }