mirror of https://github.com/docker/cli.git
config: remove redundant os.Stat()
There's no need to perform an `os.Stat()` first, because `os.Open()` also returns the same errors if the file does not exist, or couldn't be opened for other reasons. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
c5c6fb1cd4
commit
89089fb419
|
@ -95,11 +95,7 @@ func Load(configDir string) (*configfile.ConfigFile, error) {
|
||||||
configFile := configfile.New(filename)
|
configFile := configfile.New(filename)
|
||||||
|
|
||||||
// Try happy path first - latest config file
|
// Try happy path first - latest config file
|
||||||
if _, err := os.Stat(filename); err == nil {
|
if file, err := os.Open(filename); err == nil {
|
||||||
file, err := os.Open(filename)
|
|
||||||
if err != nil {
|
|
||||||
return configFile, errors.Wrap(err, filename)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
err = configFile.LoadFromReader(file)
|
err = configFile.LoadFromReader(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -113,23 +109,17 @@ func Load(configDir string) (*configfile.ConfigFile, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can't find latest config file so check for the old one
|
// Can't find latest config file so check for the old one
|
||||||
homedir, err := os.UserHomeDir()
|
home, err := os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return configFile, errors.Wrap(err, oldConfigfile)
|
return configFile, errors.Wrap(err, oldConfigfile)
|
||||||
}
|
}
|
||||||
filename = filepath.Join(homedir, oldConfigfile)
|
filename = filepath.Join(home, oldConfigfile)
|
||||||
if _, err := os.Stat(filename); err != nil {
|
if file, err := os.Open(filename); err == nil {
|
||||||
return configFile, nil // missing file is not an error
|
|
||||||
}
|
|
||||||
file, err := os.Open(filename)
|
|
||||||
if err != nil {
|
|
||||||
return configFile, errors.Wrap(err, filename)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
err = configFile.LegacyLoadFromReader(file)
|
if err := configFile.LegacyLoadFromReader(file); err != nil {
|
||||||
if err != nil {
|
|
||||||
return configFile, errors.Wrap(err, filename)
|
return configFile, errors.Wrap(err, filename)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return configFile, nil
|
return configFile, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -385,6 +385,7 @@ func TestJSONWithCredentialHelpers(t *testing.T) {
|
||||||
|
|
||||||
// Save it and make sure it shows up in new form
|
// Save it and make sure it shows up in new form
|
||||||
func saveConfigAndValidateNewFormat(t *testing.T, config *configfile.ConfigFile, configDir string) string {
|
func saveConfigAndValidateNewFormat(t *testing.T, config *configfile.ConfigFile, configDir string) string {
|
||||||
|
t.Helper()
|
||||||
assert.NilError(t, config.Save())
|
assert.NilError(t, config.Save())
|
||||||
|
|
||||||
buf, err := ioutil.ReadFile(filepath.Join(configDir, ConfigFileName))
|
buf, err := ioutil.ReadFile(filepath.Join(configDir, ConfigFileName))
|
||||||
|
|
Loading…
Reference in New Issue