github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/util/configv3/load_config.go (about) 1 package configv3 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "math" 7 "os" 8 "path/filepath" 9 10 "code.cloudfoundry.org/cli/command/translatableerror" 11 "golang.org/x/crypto/ssh/terminal" 12 ) 13 14 // LoadConfig loads the config from the .cf/config.json and os.ENV. If the 15 // config.json does not exists, it will use a default config in it's place. 16 // Takes in an optional FlagOverride, will only use the first one passed, that 17 // can override the given flag values. 18 // 19 // The '.cf' directory will be read in one of the following locations on UNIX 20 // Systems: 21 // 1. $CF_HOME/.cf if $CF_HOME is set 22 // 2. $HOME/.cf as the default 23 // 24 // The '.cf' directory will be read in one of the following locations on 25 // Windows Systems: 26 // 1. CF_HOME\.cf if CF_HOME is set 27 // 2. HOMEDRIVE\HOMEPATH\.cf if HOMEDRIVE or HOMEPATH is set 28 // 3. USERPROFILE\.cf as the default 29 func LoadConfig(flags ...FlagOverride) (*Config, error) { 30 err := removeOldTempConfigFiles() 31 if err != nil { 32 return nil, err 33 } 34 35 configFilePath := ConfigFilePath() 36 37 config := Config{ 38 ConfigFile: JSONConfig{ 39 ConfigVersion: 3, 40 Target: DefaultTarget, 41 ColorEnabled: DefaultColorEnabled, 42 PluginRepositories: []PluginRepository{{ 43 Name: DefaultPluginRepoName, 44 URL: DefaultPluginRepoURL, 45 }}, 46 }, 47 } 48 49 var jsonError error 50 51 if _, err = os.Stat(configFilePath); err == nil || !os.IsNotExist(err) { 52 var file []byte 53 file, err = ioutil.ReadFile(configFilePath) 54 if err != nil { 55 return nil, err 56 } 57 58 if len(file) == 0 { 59 // TODO: change this to not use translatableerror 60 jsonError = translatableerror.EmptyConfigError{FilePath: configFilePath} 61 } else { 62 var configFile JSONConfig 63 err = json.Unmarshal(file, &configFile) 64 if err != nil { 65 return nil, err 66 } 67 config.ConfigFile = configFile 68 } 69 } 70 71 if config.ConfigFile.SSHOAuthClient == "" { 72 config.ConfigFile.SSHOAuthClient = DefaultSSHOAuthClient 73 } 74 75 if config.ConfigFile.UAAOAuthClient == "" { 76 config.ConfigFile.UAAOAuthClient = DefaultUAAOAuthClient 77 config.ConfigFile.UAAOAuthClientSecret = DefaultUAAOAuthClientSecret 78 } 79 80 config.ENV = EnvOverride{ 81 BinaryName: filepath.Base(os.Args[0]), 82 CFColor: os.Getenv("CF_COLOR"), 83 CFDialTimeout: os.Getenv("CF_DIAL_TIMEOUT"), 84 CFLogLevel: os.Getenv("CF_LOG_LEVEL"), 85 CFPassword: os.Getenv("CF_PASSWORD"), 86 CFPluginHome: os.Getenv("CF_PLUGIN_HOME"), 87 CFStagingTimeout: os.Getenv("CF_STAGING_TIMEOUT"), 88 CFStartupTimeout: os.Getenv("CF_STARTUP_TIMEOUT"), 89 CFTrace: os.Getenv("CF_TRACE"), 90 CFUsername: os.Getenv("CF_USERNAME"), 91 DockerPassword: os.Getenv("CF_DOCKER_PASSWORD"), 92 Experimental: os.Getenv("CF_CLI_EXPERIMENTAL"), 93 ForceTTY: os.Getenv("FORCE_TTY"), 94 HTTPSProxy: os.Getenv("https_proxy"), 95 Lang: os.Getenv("LANG"), 96 LCAll: os.Getenv("LC_ALL"), 97 } 98 99 pluginFilePath := filepath.Join(config.PluginHome(), "config.json") 100 if _, err = os.Stat(pluginFilePath); os.IsNotExist(err) { 101 config.pluginsConfig = PluginsConfig{ 102 Plugins: make(map[string]Plugin), 103 } 104 } else { 105 var file []byte 106 file, err = ioutil.ReadFile(pluginFilePath) 107 if err != nil { 108 return nil, err 109 } 110 111 err = json.Unmarshal(file, &config.pluginsConfig) 112 if err != nil { 113 return nil, err 114 } 115 116 for name, plugin := range config.pluginsConfig.Plugins { 117 plugin.Name = name 118 config.pluginsConfig.Plugins[name] = plugin 119 } 120 } 121 122 if len(flags) > 0 { 123 config.Flags = flags[0] 124 } 125 126 pwd, err := os.Getwd() 127 if err != nil { 128 return nil, err 129 } 130 131 // Developer Note: The following is untested! Change at your own risk. 132 isTTY := terminal.IsTerminal(int(os.Stdout.Fd())) 133 terminalWidth := math.MaxInt32 134 135 if isTTY { 136 var err error 137 terminalWidth, _, err = terminal.GetSize(int(os.Stdout.Fd())) 138 if err != nil { 139 return nil, err 140 } 141 } 142 143 config.detectedSettings = detectedSettings{ 144 currentDirectory: pwd, 145 terminalWidth: terminalWidth, 146 tty: isTTY, 147 } 148 149 return &config, jsonError 150 } 151 152 func removeOldTempConfigFiles() error { 153 oldTempFileNames, err := filepath.Glob(filepath.Join(configDirectory(), "temp-config?*")) 154 if err != nil { 155 return err 156 } 157 158 for _, oldTempFileName := range oldTempFileNames { 159 err = os.Remove(oldTempFileName) 160 if err != nil { 161 return err 162 } 163 } 164 165 return nil 166 }