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