mirror of https://github.com/docker/cli.git
Merge pull request #2595 from cpuguy83/handle_close_error_on_save
Handle errors on close in config file write.
This commit is contained in:
commit
e4e754bb74
|
@ -13,6 +13,7 @@ import (
|
||||||
"github.com/docker/cli/cli/config/credentials"
|
"github.com/docker/cli/cli/config/credentials"
|
||||||
"github.com/docker/cli/cli/config/types"
|
"github.com/docker/cli/cli/config/types"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -177,7 +178,7 @@ func (configFile *ConfigFile) SaveToWriter(writer io.Writer) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save encodes and writes out all the authorization information
|
// Save encodes and writes out all the authorization information
|
||||||
func (configFile *ConfigFile) Save() error {
|
func (configFile *ConfigFile) Save() (retErr error) {
|
||||||
if configFile.Filename == "" {
|
if configFile.Filename == "" {
|
||||||
return errors.Errorf("Can't save config with empty filename")
|
return errors.Errorf("Can't save config with empty filename")
|
||||||
}
|
}
|
||||||
|
@ -190,15 +191,26 @@ func (configFile *ConfigFile) Save() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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)
|
err = configFile.SaveToWriter(temp)
|
||||||
temp.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Remove(temp.Name())
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := temp.Close(); err != nil {
|
||||||
|
return errors.Wrap(err, "error closing temp file")
|
||||||
|
}
|
||||||
|
|
||||||
// Try copying the current config file (if any) ownership and permissions
|
// Try copying the current config file (if any) ownership and permissions
|
||||||
copyFilePermissions(configFile.Filename, temp.Name())
|
copyFilePermissions(configFile.Filename, temp.Name())
|
||||||
|
|
||||||
return os.Rename(temp.Name(), configFile.Filename)
|
return os.Rename(temp.Name(), configFile.Filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue