Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions cli/config/configfile/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,21 @@ func (configFile *ConfigFile) Save() error {
return errors.Errorf("Can't save config with empty filename")
}

if err := os.MkdirAll(filepath.Dir(configFile.Filename), 0700); err != nil {
dir := filepath.Dir(configFile.Filename)
if err := os.MkdirAll(dir, 0700); err != nil {
return err
}
f, err := os.OpenFile(configFile.Filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
temp, err := ioutil.TempFile(dir, filepath.Base(configFile.Filename))
if err != nil {
return err
}
defer f.Close()
return configFile.SaveToWriter(f)
err = configFile.SaveToWriter(temp)
temp.Close()
if err != nil {
os.Remove(temp.Name())
return err
}
return os.Rename(temp.Name(), configFile.Filename)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit; if the rename fails we'll leave behind the temp file.

Chances are probably slim that that happens, so I'm ok with keeping this as-is (we can do a follow-up if we think that's a concern)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel it's not that big of a deal, so I'm ok keeping as-is and do a follow-up if we need 👼

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alrighty; let's get this merged; thanks!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent catch! Fixed in #1366 since this one already sailed.

}

// ParseProxyConfig computes proxy configuration by retrieving the config for the provided host and
Expand Down
12 changes: 12 additions & 0 deletions cli/config/configfile/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package configfile

import (
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/docker/cli/cli/config/credentials"
Expand Down Expand Up @@ -413,3 +415,13 @@ func TestCheckKubernetesConfigurationRaiseAnErrorOnInvalidValue(t *testing.T) {
}
}
}

func TestSave(t *testing.T) {
configFile := New("test-save")
defer os.Remove("test-save")
err := configFile.Save()
assert.NilError(t, err)
cfg, err := ioutil.ReadFile("test-save")
assert.NilError(t, err)
assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}"))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like an omitempty tag is missing in the Auths field.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can fix in another PR

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Review after merge (so maybe for the just opened follow-up) 😅:
You can use backquotes instead:

assert.Check(t, is.Equal(string(cfg), `{
	"auths": {}
}`))

}