Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli/config: use os.UserHomeDir instead of github.com/docker/docker/pkg/homedir #2101

Merged
merged 1 commit into from
Sep 24, 2019
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
13 changes: 10 additions & 3 deletions cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/config/credentials"
"github.com/docker/cli/cli/config/types"
"github.com/docker/docker/pkg/homedir"
"github.com/pkg/errors"
)

Expand All @@ -28,7 +27,11 @@ var (

func init() {
if configDir == "" {
configDir = filepath.Join(homedir.Get(), configFileDir)
homedir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
configDir = filepath.Join(homedir, configFileDir)
}
}

Expand Down Expand Up @@ -106,7 +109,11 @@ func Load(configDir string) (*configfile.ConfigFile, error) {
}

// Can't find latest config file so check for the old one
confFile := filepath.Join(homedir.Get(), oldConfigfile)
homedir, err := os.UserHomeDir()
if err != nil {
return configFile, errors.Wrap(err, oldConfigfile)
}
confFile := filepath.Join(homedir, oldConfigfile)
if _, err := os.Stat(confFile); err != nil {
return configFile, nil //missing file is not an error
}
Expand Down
34 changes: 21 additions & 13 deletions cli/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,25 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/config/credentials"
"github.com/docker/docker/pkg/homedir"
"github.com/pkg/errors"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)

var homeKey = "HOME"

func init() {
if runtime.GOOS == "windows" {
homeKey = "USERPROFILE"
}
}

func setupConfigDir(t *testing.T) (string, func()) {
tmpdir, err := ioutil.TempDir("", "config-test")
assert.NilError(t, err)
Expand Down Expand Up @@ -113,10 +121,10 @@ email`: "Invalid auth configuration file",
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)

homeKey := homedir.Key()
homeVal := homedir.Get()
homeDir, err := os.UserHomeDir()
assert.NilError(t, err)

defer func() { os.Setenv(homeKey, homeVal) }()
defer func() { os.Setenv(homeKey, homeDir) }()
os.Setenv(homeKey, tmpHome)

for content, expectedError := range invalids {
Expand All @@ -134,10 +142,10 @@ func TestOldValidAuth(t *testing.T) {
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)

homeKey := homedir.Key()
homeVal := homedir.Get()
homeDir, err := os.UserHomeDir()
assert.NilError(t, err)

defer func() { os.Setenv(homeKey, homeVal) }()
defer func() { os.Setenv(homeKey, homeDir) }()
os.Setenv(homeKey, tmpHome)

fn := filepath.Join(tmpHome, oldConfigfile)
Expand Down Expand Up @@ -173,10 +181,10 @@ func TestOldJSONInvalid(t *testing.T) {
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)

homeKey := homedir.Key()
homeVal := homedir.Get()
homeDir, err := os.UserHomeDir()
assert.NilError(t, err)

defer func() { os.Setenv(homeKey, homeVal) }()
defer func() { os.Setenv(homeKey, homeDir) }()
os.Setenv(homeKey, tmpHome)

fn := filepath.Join(tmpHome, oldConfigfile)
Expand All @@ -197,10 +205,10 @@ func TestOldJSON(t *testing.T) {
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)

homeKey := homedir.Key()
homeVal := homedir.Get()
homeDir, err := os.UserHomeDir()
assert.NilError(t, err)

defer func() { os.Setenv(homeKey, homeVal) }()
defer func() { os.Setenv(homeKey, homeDir) }()
os.Setenv(homeKey, tmpHome)

fn := filepath.Join(tmpHome, oldConfigfile)
Expand Down