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