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