context use: don't create/update config file and directories if not needed by thaJeztah · Pull Request #3721 · docker/cli (original) (raw)

context use: skip validation for "default" context

This code was handling validation and parsing, only to discard the results if it was the default context.

context use: don't create/update config file and directories if not needed

Avoid updating the config-file if nothing changed. This also prevents creating
the file and config-directory if the default is used and no config-file existed
yet.

config.Save() performs various steps (creating the directory, updating
or copying permissions, etc etc), which are not needed if the defaults are
used;

// Save encodes and writes out all the authorization information
func (configFile *ConfigFile) Save() (retErr error) {
if configFile.Filename == "" {
return errors.Errorf("Can't save config with empty filename")
}
dir := filepath.Dir(configFile.Filename)
if err := os.MkdirAll(dir, 0700); err != nil {
return err
}
temp, err := os.CreateTemp(dir, filepath.Base(configFile.Filename))
if err != nil {
return err
}
defer func() {
temp.Close()
if retErr != nil {
if err := os.Remove(temp.Name()); err != nil {
logrus.WithError(err).WithField("file", temp.Name()).Debug("Error cleaning up temp file")
}
}
}()
err = configFile.SaveToWriter(temp)
if err != nil {
return err
}
if err := temp.Close(); err != nil {
return errors.Wrap(err, "error closing temp file")
}
// Handle situation where the configfile is a symlink
cfgFile := configFile.Filename
if f, err := os.Readlink(cfgFile); err == nil {
cfgFile = f
}
// Try copying the current config file (if any) ownership and permissions
copyFilePermissions(cfgFile, temp.Name())
return os.Rename(temp.Name(), cfgFile)
}

- Description for the changelog

The client no longer creates or updates the CLI config file when running `docker context use` and the selected context is the current context.

- A picture of a cute animal (not mandatory but encouraged)