LoadConfig loads the config from the .cf/config.json and os.ENV. If the config.json does not exists, it will use a default config in its place. Takes in an optional FlagOverride, will only use the first one passed, that can override the given flag values. The '.cf' directory will be read in one of
(flags ...FlagOverride)
| 64 | // 2. HOMEDRIVE\HOMEPATH\.cf if HOMEDRIVE or HOMEPATH is set |
| 65 | // 3. USERPROFILE\.cf as the default |
| 66 | func LoadConfig(flags ...FlagOverride) (*Config, error) { |
| 67 | err := removeOldTempConfigFiles() |
| 68 | if err != nil { |
| 69 | return nil, err |
| 70 | } |
| 71 | |
| 72 | configFilePath := ConfigFilePath() |
| 73 | |
| 74 | config := Config{ |
| 75 | ConfigFile: JSONConfig{ |
| 76 | ConfigVersion: CurrentConfigVersion, |
| 77 | Target: DefaultTarget, |
| 78 | ColorEnabled: DefaultColorEnabled, |
| 79 | PluginRepositories: []PluginRepository{{ |
| 80 | Name: DefaultPluginRepoName, |
| 81 | URL: DefaultPluginRepoURL, |
| 82 | }}, |
| 83 | }, |
| 84 | } |
| 85 | |
| 86 | var jsonError error |
| 87 | |
| 88 | if _, err = os.Stat(configFilePath); err == nil || !os.IsNotExist(err) { |
| 89 | var file []byte |
| 90 | file, err = os.ReadFile(configFilePath) |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | |
| 95 | if len(file) == 0 { |
| 96 | // TODO: change this to not use translatableerror |
| 97 | jsonError = translatableerror.EmptyConfigError{FilePath: configFilePath} |
| 98 | } else { |
| 99 | var configFile JSONConfig |
| 100 | err = json.Unmarshal(file, &configFile) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | if configFile.ConfigVersion == CurrentConfigVersion { |
| 105 | config.ConfigFile = configFile |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if config.ConfigFile.SSHOAuthClient == "" { |
| 111 | config.ConfigFile.SSHOAuthClient = DefaultSSHOAuthClient |
| 112 | } |
| 113 | |
| 114 | if config.ConfigFile.UAAOAuthClient == "" { |
| 115 | config.ConfigFile.UAAOAuthClient = DefaultUAAOAuthClient |
| 116 | config.ConfigFile.UAAOAuthClientSecret = DefaultUAAOAuthClientSecret |
| 117 | } |
| 118 | |
| 119 | config.ENV = EnvOverride{ |
| 120 | BinaryName: filepath.Base(os.Args[0]), |
| 121 | CFColor: os.Getenv("CF_COLOR"), |
| 122 | CFDialTimeout: os.Getenv("CF_DIAL_TIMEOUT"), |
| 123 | CFLogLevel: os.Getenv("CF_LOG_LEVEL"), |
no test coverage detected