Skip to content

Commit c9db2f1

Browse files
committed
cli/config/configfile: use more idiomatic receiver name
Use a shorter name, which is more idiomatic, and prevents accidental shadowing of types or arguments. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 9dc6285 commit c9db2f1

File tree

1 file changed

+51
-51
lines changed

1 file changed

+51
-51
lines changed

cli/config/configfile/file.go

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ func New(fn string) *ConfigFile {
9292

9393
// LoadFromReader reads the configuration data given and sets up the auth config
9494
// information with given directory and populates the receiver object
95-
func (configFile *ConfigFile) LoadFromReader(configData io.Reader) error {
96-
if err := json.NewDecoder(configData).Decode(configFile); err != nil && !errors.Is(err, io.EOF) {
95+
func (c *ConfigFile) LoadFromReader(configData io.Reader) error {
96+
if err := json.NewDecoder(configData).Decode(c); err != nil && !errors.Is(err, io.EOF) {
9797
return err
9898
}
9999
var err error
100-
for addr, ac := range configFile.AuthConfigs {
100+
for addr, ac := range c.AuthConfigs {
101101
if ac.Auth != "" {
102102
ac.Username, ac.Password, err = decodeAuth(ac.Auth)
103103
if err != nil {
@@ -106,33 +106,33 @@ func (configFile *ConfigFile) LoadFromReader(configData io.Reader) error {
106106
}
107107
ac.Auth = ""
108108
ac.ServerAddress = addr
109-
configFile.AuthConfigs[addr] = ac
109+
c.AuthConfigs[addr] = ac
110110
}
111111
return nil
112112
}
113113

114114
// ContainsAuth returns whether there is authentication configured
115115
// in this file or not.
116-
func (configFile *ConfigFile) ContainsAuth() bool {
117-
return configFile.CredentialsStore != "" ||
118-
len(configFile.CredentialHelpers) > 0 ||
119-
len(configFile.AuthConfigs) > 0
116+
func (c *ConfigFile) ContainsAuth() bool {
117+
return c.CredentialsStore != "" ||
118+
len(c.CredentialHelpers) > 0 ||
119+
len(c.AuthConfigs) > 0
120120
}
121121

122122
// GetAuthConfigs returns the mapping of repo to auth configuration
123-
func (configFile *ConfigFile) GetAuthConfigs() map[string]types.AuthConfig {
124-
if configFile.AuthConfigs == nil {
125-
configFile.AuthConfigs = make(map[string]types.AuthConfig)
123+
func (c *ConfigFile) GetAuthConfigs() map[string]types.AuthConfig {
124+
if c.AuthConfigs == nil {
125+
c.AuthConfigs = make(map[string]types.AuthConfig)
126126
}
127-
return configFile.AuthConfigs
127+
return c.AuthConfigs
128128
}
129129

130130
// SaveToWriter encodes and writes out all the authorization information to
131131
// the given writer
132-
func (configFile *ConfigFile) SaveToWriter(writer io.Writer) error {
132+
func (c *ConfigFile) SaveToWriter(writer io.Writer) error {
133133
// Encode sensitive data into a new/temp struct
134-
tmpAuthConfigs := make(map[string]types.AuthConfig, len(configFile.AuthConfigs))
135-
for k, authConfig := range configFile.AuthConfigs {
134+
tmpAuthConfigs := make(map[string]types.AuthConfig, len(c.AuthConfigs))
135+
for k, authConfig := range c.AuthConfigs {
136136
authCopy := authConfig
137137
// encode and save the authstring, while blanking out the original fields
138138
authCopy.Auth = encodeAuth(&authCopy)
@@ -142,18 +142,18 @@ func (configFile *ConfigFile) SaveToWriter(writer io.Writer) error {
142142
tmpAuthConfigs[k] = authCopy
143143
}
144144

145-
saveAuthConfigs := configFile.AuthConfigs
146-
configFile.AuthConfigs = tmpAuthConfigs
147-
defer func() { configFile.AuthConfigs = saveAuthConfigs }()
145+
saveAuthConfigs := c.AuthConfigs
146+
c.AuthConfigs = tmpAuthConfigs
147+
defer func() { c.AuthConfigs = saveAuthConfigs }()
148148

149149
// User-Agent header is automatically set, and should not be stored in the configuration
150-
for v := range configFile.HTTPHeaders {
150+
for v := range c.HTTPHeaders {
151151
if strings.EqualFold(v, "User-Agent") {
152-
delete(configFile.HTTPHeaders, v)
152+
delete(c.HTTPHeaders, v)
153153
}
154154
}
155155

156-
data, err := json.MarshalIndent(configFile, "", "\t")
156+
data, err := json.MarshalIndent(c, "", "\t")
157157
if err != nil {
158158
return err
159159
}
@@ -162,16 +162,16 @@ func (configFile *ConfigFile) SaveToWriter(writer io.Writer) error {
162162
}
163163

164164
// Save encodes and writes out all the authorization information
165-
func (configFile *ConfigFile) Save() (retErr error) {
166-
if configFile.Filename == "" {
165+
func (c *ConfigFile) Save() (retErr error) {
166+
if c.Filename == "" {
167167
return errors.New("can't save config with empty filename")
168168
}
169169

170-
dir := filepath.Dir(configFile.Filename)
170+
dir := filepath.Dir(c.Filename)
171171
if err := os.MkdirAll(dir, 0o700); err != nil {
172172
return err
173173
}
174-
temp, err := os.CreateTemp(dir, filepath.Base(configFile.Filename))
174+
temp, err := os.CreateTemp(dir, filepath.Base(c.Filename))
175175
if err != nil {
176176
return err
177177
}
@@ -185,7 +185,7 @@ func (configFile *ConfigFile) Save() (retErr error) {
185185
}
186186
}()
187187

188-
err = configFile.SaveToWriter(temp)
188+
err = c.SaveToWriter(temp)
189189
if err != nil {
190190
return err
191191
}
@@ -195,7 +195,7 @@ func (configFile *ConfigFile) Save() (retErr error) {
195195
}
196196

197197
// Handle situation where the configfile is a symlink, and allow for dangling symlinks
198-
cfgFile := configFile.Filename
198+
cfgFile := c.Filename
199199
if f, err := filepath.EvalSymlinks(cfgFile); err == nil {
200200
cfgFile = f
201201
} else if os.IsNotExist(err) {
@@ -213,16 +213,16 @@ func (configFile *ConfigFile) Save() (retErr error) {
213213

214214
// ParseProxyConfig computes proxy configuration by retrieving the config for the provided host and
215215
// then checking this against any environment variables provided to the container
216-
func (configFile *ConfigFile) ParseProxyConfig(host string, runOpts map[string]*string) map[string]*string {
216+
func (c *ConfigFile) ParseProxyConfig(host string, runOpts map[string]*string) map[string]*string {
217217
var cfgKey string
218218

219-
if _, ok := configFile.Proxies[host]; !ok {
219+
if _, ok := c.Proxies[host]; !ok {
220220
cfgKey = "default"
221221
} else {
222222
cfgKey = host
223223
}
224224

225-
config := configFile.Proxies[cfgKey]
225+
config := c.Proxies[cfgKey]
226226
permitted := map[string]*string{
227227
"HTTP_PROXY": &config.HTTPProxy,
228228
"HTTPS_PROXY": &config.HTTPSProxy,
@@ -286,12 +286,12 @@ func decodeAuth(authStr string) (string, string, error) {
286286

287287
// GetCredentialsStore returns a new credentials store from the settings in the
288288
// configuration file
289-
func (configFile *ConfigFile) GetCredentialsStore(registryHostname string) credentials.Store {
289+
func (c *ConfigFile) GetCredentialsStore(registryHostname string) credentials.Store {
290290
var store credentials.Store
291-
if helper := configFile.CredentialHelpers[registryHostname]; helper != "" {
292-
store = newNativeStore(configFile, helper)
291+
if helper := c.CredentialHelpers[registryHostname]; helper != "" {
292+
store = newNativeStore(c, helper)
293293
} else {
294-
store = credentials.NewFileStore(configFile)
294+
store = credentials.NewFileStore(c)
295295
}
296296

297297
envConfig := os.Getenv(DockerEnvConfigKey)
@@ -354,30 +354,30 @@ var newNativeStore = func(configFile *ConfigFile, helperSuffix string) credentia
354354
}
355355

356356
// GetAuthConfig for a repository from the credential store
357-
func (configFile *ConfigFile) GetAuthConfig(registryHostname string) (types.AuthConfig, error) {
358-
return configFile.GetCredentialsStore(registryHostname).Get(registryHostname)
357+
func (c *ConfigFile) GetAuthConfig(registryHostname string) (types.AuthConfig, error) {
358+
return c.GetCredentialsStore(registryHostname).Get(registryHostname)
359359
}
360360

361361
// GetAllCredentials returns all of the credentials stored in all of the
362362
// configured credential stores.
363-
func (configFile *ConfigFile) GetAllCredentials() (map[string]types.AuthConfig, error) {
363+
func (c *ConfigFile) GetAllCredentials() (map[string]types.AuthConfig, error) {
364364
auths := make(map[string]types.AuthConfig)
365365
addAll := func(from map[string]types.AuthConfig) {
366366
for reg, ac := range from {
367367
auths[reg] = ac
368368
}
369369
}
370370

371-
defaultStore := configFile.GetCredentialsStore("")
371+
defaultStore := c.GetCredentialsStore("")
372372
newAuths, err := defaultStore.GetAll()
373373
if err != nil {
374374
return nil, err
375375
}
376376
addAll(newAuths)
377377

378378
// Auth configs from a registry-specific helper should override those from the default store.
379-
for registryHostname := range configFile.CredentialHelpers {
380-
newAuth, err := configFile.GetAuthConfig(registryHostname)
379+
for registryHostname := range c.CredentialHelpers {
380+
newAuth, err := c.GetAuthConfig(registryHostname)
381381
if err != nil {
382382
// TODO(thaJeztah): use context-logger, so that this output can be suppressed (in tests).
383383
logrus.WithError(err).Warnf("Failed to get credentials for registry: %s", registryHostname)
@@ -389,16 +389,16 @@ func (configFile *ConfigFile) GetAllCredentials() (map[string]types.AuthConfig,
389389
}
390390

391391
// GetFilename returns the file name that this config file is based on.
392-
func (configFile *ConfigFile) GetFilename() string {
393-
return configFile.Filename
392+
func (c *ConfigFile) GetFilename() string {
393+
return c.Filename
394394
}
395395

396396
// PluginConfig retrieves the requested option for the given plugin.
397-
func (configFile *ConfigFile) PluginConfig(pluginname, option string) (string, bool) {
398-
if configFile.Plugins == nil {
397+
func (c *ConfigFile) PluginConfig(pluginname, option string) (string, bool) {
398+
if c.Plugins == nil {
399399
return "", false
400400
}
401-
pluginConfig, ok := configFile.Plugins[pluginname]
401+
pluginConfig, ok := c.Plugins[pluginname]
402402
if !ok {
403403
return "", false
404404
}
@@ -410,21 +410,21 @@ func (configFile *ConfigFile) PluginConfig(pluginname, option string) (string, b
410410
// plugin. Passing a value of "" will remove the option. If removing
411411
// the final config item for a given plugin then also cleans up the
412412
// overall plugin entry.
413-
func (configFile *ConfigFile) SetPluginConfig(pluginname, option, value string) {
414-
if configFile.Plugins == nil {
415-
configFile.Plugins = make(map[string]map[string]string)
413+
func (c *ConfigFile) SetPluginConfig(pluginname, option, value string) {
414+
if c.Plugins == nil {
415+
c.Plugins = make(map[string]map[string]string)
416416
}
417-
pluginConfig, ok := configFile.Plugins[pluginname]
417+
pluginConfig, ok := c.Plugins[pluginname]
418418
if !ok {
419419
pluginConfig = make(map[string]string)
420-
configFile.Plugins[pluginname] = pluginConfig
420+
c.Plugins[pluginname] = pluginConfig
421421
}
422422
if value != "" {
423423
pluginConfig[option] = value
424424
} else {
425425
delete(pluginConfig, option)
426426
}
427427
if len(pluginConfig) == 0 {
428-
delete(configFile.Plugins, pluginname)
428+
delete(c.Plugins, pluginname)
429429
}
430430
}

0 commit comments

Comments
 (0)