Skip to content

Commit f12fc15

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 bcf36a5 commit f12fc15

1 file changed

Lines changed: 51 additions & 51 deletions

File tree

cli/config/configfile/file.go

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

125125
// LoadFromReader reads the configuration data given and sets up the auth config
126126
// information with given directory and populates the receiver object
127-
func (configFile *ConfigFile) LoadFromReader(configData io.Reader) error {
128-
if err := json.NewDecoder(configData).Decode(configFile); err != nil && !errors.Is(err, io.EOF) {
127+
func (c *ConfigFile) LoadFromReader(configData io.Reader) error {
128+
if err := json.NewDecoder(configData).Decode(c); err != nil && !errors.Is(err, io.EOF) {
129129
return err
130130
}
131131
var err error
132-
for addr, ac := range configFile.AuthConfigs {
132+
for addr, ac := range c.AuthConfigs {
133133
if ac.Auth != "" {
134134
ac.Username, ac.Password, err = decodeAuth(ac.Auth)
135135
if err != nil {
@@ -138,33 +138,33 @@ func (configFile *ConfigFile) LoadFromReader(configData io.Reader) error {
138138
}
139139
ac.Auth = ""
140140
ac.ServerAddress = addr
141-
configFile.AuthConfigs[addr] = ac
141+
c.AuthConfigs[addr] = ac
142142
}
143143
return nil
144144
}
145145

146146
// ContainsAuth returns whether there is authentication configured
147147
// in this file or not.
148-
func (configFile *ConfigFile) ContainsAuth() bool {
149-
return configFile.CredentialsStore != "" ||
150-
len(configFile.CredentialHelpers) > 0 ||
151-
len(configFile.AuthConfigs) > 0
148+
func (c *ConfigFile) ContainsAuth() bool {
149+
return c.CredentialsStore != "" ||
150+
len(c.CredentialHelpers) > 0 ||
151+
len(c.AuthConfigs) > 0
152152
}
153153

154154
// GetAuthConfigs returns the mapping of repo to auth configuration
155-
func (configFile *ConfigFile) GetAuthConfigs() map[string]types.AuthConfig {
156-
if configFile.AuthConfigs == nil {
157-
configFile.AuthConfigs = make(map[string]types.AuthConfig)
155+
func (c *ConfigFile) GetAuthConfigs() map[string]types.AuthConfig {
156+
if c.AuthConfigs == nil {
157+
c.AuthConfigs = make(map[string]types.AuthConfig)
158158
}
159-
return configFile.AuthConfigs
159+
return c.AuthConfigs
160160
}
161161

162162
// SaveToWriter encodes and writes out all the authorization information to
163163
// the given writer
164-
func (configFile *ConfigFile) SaveToWriter(writer io.Writer) error {
164+
func (c *ConfigFile) SaveToWriter(writer io.Writer) error {
165165
// Encode sensitive data into a new/temp struct
166-
tmpAuthConfigs := make(map[string]types.AuthConfig, len(configFile.AuthConfigs))
167-
for k, authConfig := range configFile.AuthConfigs {
166+
tmpAuthConfigs := make(map[string]types.AuthConfig, len(c.AuthConfigs))
167+
for k, authConfig := range c.AuthConfigs {
168168
authCopy := authConfig
169169
// encode and save the authstring, while blanking out the original fields
170170
authCopy.Auth = encodeAuth(&authCopy)
@@ -174,18 +174,18 @@ func (configFile *ConfigFile) SaveToWriter(writer io.Writer) error {
174174
tmpAuthConfigs[k] = authCopy
175175
}
176176

177-
saveAuthConfigs := configFile.AuthConfigs
178-
configFile.AuthConfigs = tmpAuthConfigs
179-
defer func() { configFile.AuthConfigs = saveAuthConfigs }()
177+
saveAuthConfigs := c.AuthConfigs
178+
c.AuthConfigs = tmpAuthConfigs
179+
defer func() { c.AuthConfigs = saveAuthConfigs }()
180180

181181
// User-Agent header is automatically set, and should not be stored in the configuration
182-
for v := range configFile.HTTPHeaders {
182+
for v := range c.HTTPHeaders {
183183
if strings.EqualFold(v, "User-Agent") {
184-
delete(configFile.HTTPHeaders, v)
184+
delete(c.HTTPHeaders, v)
185185
}
186186
}
187187

188-
data, err := json.MarshalIndent(configFile, "", "\t")
188+
data, err := json.MarshalIndent(c, "", "\t")
189189
if err != nil {
190190
return err
191191
}
@@ -194,16 +194,16 @@ func (configFile *ConfigFile) SaveToWriter(writer io.Writer) error {
194194
}
195195

196196
// Save encodes and writes out all the authorization information
197-
func (configFile *ConfigFile) Save() (retErr error) {
198-
if configFile.Filename == "" {
197+
func (c *ConfigFile) Save() (retErr error) {
198+
if c.Filename == "" {
199199
return errors.New("can't save config with empty filename")
200200
}
201201

202-
dir := filepath.Dir(configFile.Filename)
202+
dir := filepath.Dir(c.Filename)
203203
if err := os.MkdirAll(dir, 0o700); err != nil {
204204
return err
205205
}
206-
temp, err := os.CreateTemp(dir, filepath.Base(configFile.Filename))
206+
temp, err := os.CreateTemp(dir, filepath.Base(c.Filename))
207207
if err != nil {
208208
return err
209209
}
@@ -217,7 +217,7 @@ func (configFile *ConfigFile) Save() (retErr error) {
217217
}
218218
}()
219219

220-
err = configFile.SaveToWriter(temp)
220+
err = c.SaveToWriter(temp)
221221
if err != nil {
222222
return err
223223
}
@@ -227,7 +227,7 @@ func (configFile *ConfigFile) Save() (retErr error) {
227227
}
228228

229229
// Handle situation where the configfile is a symlink, and allow for dangling symlinks
230-
cfgFile := configFile.Filename
230+
cfgFile := c.Filename
231231
if f, err := filepath.EvalSymlinks(cfgFile); err == nil {
232232
cfgFile = f
233233
} else if os.IsNotExist(err) {
@@ -245,16 +245,16 @@ func (configFile *ConfigFile) Save() (retErr error) {
245245

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

251-
if _, ok := configFile.Proxies[host]; !ok {
251+
if _, ok := c.Proxies[host]; !ok {
252252
cfgKey = "default"
253253
} else {
254254
cfgKey = host
255255
}
256256

257-
config := configFile.Proxies[cfgKey]
257+
config := c.Proxies[cfgKey]
258258
permitted := map[string]*string{
259259
"HTTP_PROXY": &config.HTTPProxy,
260260
"HTTPS_PROXY": &config.HTTPSProxy,
@@ -318,11 +318,11 @@ func decodeAuth(authStr string) (string, string, error) {
318318

319319
// GetCredentialsStore returns a new credentials store from the settings in the
320320
// configuration file
321-
func (configFile *ConfigFile) GetCredentialsStore(registryHostname string) credentials.Store {
322-
store := credentials.NewFileStore(configFile)
321+
func (c *ConfigFile) GetCredentialsStore(registryHostname string) credentials.Store {
322+
store := credentials.NewFileStore(c)
323323

324-
if helper := getConfiguredCredentialStore(configFile, getAuthConfigKey(registryHostname)); helper != "" {
325-
store = newNativeStore(configFile, helper)
324+
if helper := getConfiguredCredentialStore(c, getAuthConfigKey(registryHostname)); helper != "" {
325+
store = newNativeStore(c, helper)
326326
}
327327

328328
envConfig := os.Getenv(DockerEnvConfigKey)
@@ -385,9 +385,9 @@ var newNativeStore = func(configFile *ConfigFile, helperSuffix string) credentia
385385
}
386386

387387
// GetAuthConfig for a repository from the credential store
388-
func (configFile *ConfigFile) GetAuthConfig(registryHostname string) (types.AuthConfig, error) {
388+
func (c *ConfigFile) GetAuthConfig(registryHostname string) (types.AuthConfig, error) {
389389
acKey := getAuthConfigKey(registryHostname)
390-
return configFile.GetCredentialsStore(acKey).Get(acKey)
390+
return c.GetCredentialsStore(acKey).Get(acKey)
391391
}
392392

393393
// getConfiguredCredentialStore returns the credential helper configured for the
@@ -404,22 +404,22 @@ func getConfiguredCredentialStore(c *ConfigFile, registryHostname string) string
404404

405405
// GetAllCredentials returns all of the credentials stored in all of the
406406
// configured credential stores.
407-
func (configFile *ConfigFile) GetAllCredentials() (map[string]types.AuthConfig, error) {
407+
func (c *ConfigFile) GetAllCredentials() (map[string]types.AuthConfig, error) {
408408
auths := make(map[string]types.AuthConfig)
409409
addAll := func(from map[string]types.AuthConfig) {
410410
maps.Copy(auths, from)
411411
}
412412

413-
defaultStore := configFile.GetCredentialsStore("")
413+
defaultStore := c.GetCredentialsStore("")
414414
newAuths, err := defaultStore.GetAll()
415415
if err != nil {
416416
return nil, err
417417
}
418418
addAll(newAuths)
419419

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

433433
// GetFilename returns the file name that this config file is based on.
434-
func (configFile *ConfigFile) GetFilename() string {
435-
return configFile.Filename
434+
func (c *ConfigFile) GetFilename() string {
435+
return c.Filename
436436
}
437437

438438
// PluginConfig retrieves the requested option for the given plugin.
439-
func (configFile *ConfigFile) PluginConfig(pluginname, option string) (string, bool) {
440-
if configFile.Plugins == nil {
439+
func (c *ConfigFile) PluginConfig(pluginname, option string) (string, bool) {
440+
if c.Plugins == nil {
441441
return "", false
442442
}
443-
pluginConfig, ok := configFile.Plugins[pluginname]
443+
pluginConfig, ok := c.Plugins[pluginname]
444444
if !ok {
445445
return "", false
446446
}
@@ -452,21 +452,21 @@ func (configFile *ConfigFile) PluginConfig(pluginname, option string) (string, b
452452
// plugin. Passing a value of "" will remove the option. If removing
453453
// the final config item for a given plugin then also cleans up the
454454
// overall plugin entry.
455-
func (configFile *ConfigFile) SetPluginConfig(pluginname, option, value string) {
456-
if configFile.Plugins == nil {
457-
configFile.Plugins = make(map[string]map[string]string)
455+
func (c *ConfigFile) SetPluginConfig(pluginname, option, value string) {
456+
if c.Plugins == nil {
457+
c.Plugins = make(map[string]map[string]string)
458458
}
459-
pluginConfig, ok := configFile.Plugins[pluginname]
459+
pluginConfig, ok := c.Plugins[pluginname]
460460
if !ok {
461461
pluginConfig = make(map[string]string)
462-
configFile.Plugins[pluginname] = pluginConfig
462+
c.Plugins[pluginname] = pluginConfig
463463
}
464464
if value != "" {
465465
pluginConfig[option] = value
466466
} else {
467467
delete(pluginConfig, option)
468468
}
469469
if len(pluginConfig) == 0 {
470-
delete(configFile.Plugins, pluginname)
470+
delete(c.Plugins, pluginname)
471471
}
472472
}

0 commit comments

Comments
 (0)