mirror of https://github.com/docker/cli.git
cli/config: use os.UserHomeDir instead of github.com/docker/docker/pkg/homedir
Signed-off-by: Tibor Vass <tibor@docker.com>
This commit is contained in:
parent
83751b9781
commit
c2626a8270
|
@ -10,7 +10,6 @@ import (
|
||||||
"github.com/docker/cli/cli/config/configfile"
|
"github.com/docker/cli/cli/config/configfile"
|
||||||
"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/docker/docker/pkg/homedir"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -28,7 +27,11 @@ var (
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if configDir == "" {
|
if configDir == "" {
|
||||||
configDir = filepath.Join(homedir.Get(), configFileDir)
|
homedir, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
configDir = filepath.Join(homedir, configFileDir)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,7 +109,11 @@ 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
|
||||||
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 {
|
if _, err := os.Stat(confFile); err != nil {
|
||||||
return configFile, nil //missing file is not an error
|
return configFile, nil //missing file is not an error
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,17 +7,25 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/docker/cli/cli/config/configfile"
|
"github.com/docker/cli/cli/config/configfile"
|
||||||
"github.com/docker/cli/cli/config/credentials"
|
"github.com/docker/cli/cli/config/credentials"
|
||||||
"github.com/docker/docker/pkg/homedir"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"gotest.tools/assert"
|
"gotest.tools/assert"
|
||||||
is "gotest.tools/assert/cmp"
|
is "gotest.tools/assert/cmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var homeKey = "HOME"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
homeKey = "USERPROFILE"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func setupConfigDir(t *testing.T) (string, func()) {
|
func setupConfigDir(t *testing.T) (string, func()) {
|
||||||
tmpdir, err := ioutil.TempDir("", "config-test")
|
tmpdir, err := ioutil.TempDir("", "config-test")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
|
@ -113,10 +121,10 @@ email`: "Invalid auth configuration file",
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(tmpHome)
|
defer os.RemoveAll(tmpHome)
|
||||||
|
|
||||||
homeKey := homedir.Key()
|
homeDir, err := os.UserHomeDir()
|
||||||
homeVal := homedir.Get()
|
assert.NilError(t, err)
|
||||||
|
|
||||||
defer func() { os.Setenv(homeKey, homeVal) }()
|
defer func() { os.Setenv(homeKey, homeDir) }()
|
||||||
os.Setenv(homeKey, tmpHome)
|
os.Setenv(homeKey, tmpHome)
|
||||||
|
|
||||||
for content, expectedError := range invalids {
|
for content, expectedError := range invalids {
|
||||||
|
@ -134,10 +142,10 @@ func TestOldValidAuth(t *testing.T) {
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(tmpHome)
|
defer os.RemoveAll(tmpHome)
|
||||||
|
|
||||||
homeKey := homedir.Key()
|
homeDir, err := os.UserHomeDir()
|
||||||
homeVal := homedir.Get()
|
assert.NilError(t, err)
|
||||||
|
|
||||||
defer func() { os.Setenv(homeKey, homeVal) }()
|
defer func() { os.Setenv(homeKey, homeDir) }()
|
||||||
os.Setenv(homeKey, tmpHome)
|
os.Setenv(homeKey, tmpHome)
|
||||||
|
|
||||||
fn := filepath.Join(tmpHome, oldConfigfile)
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
||||||
|
@ -173,10 +181,10 @@ func TestOldJSONInvalid(t *testing.T) {
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(tmpHome)
|
defer os.RemoveAll(tmpHome)
|
||||||
|
|
||||||
homeKey := homedir.Key()
|
homeDir, err := os.UserHomeDir()
|
||||||
homeVal := homedir.Get()
|
assert.NilError(t, err)
|
||||||
|
|
||||||
defer func() { os.Setenv(homeKey, homeVal) }()
|
defer func() { os.Setenv(homeKey, homeDir) }()
|
||||||
os.Setenv(homeKey, tmpHome)
|
os.Setenv(homeKey, tmpHome)
|
||||||
|
|
||||||
fn := filepath.Join(tmpHome, oldConfigfile)
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
||||||
|
@ -197,10 +205,10 @@ func TestOldJSON(t *testing.T) {
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(tmpHome)
|
defer os.RemoveAll(tmpHome)
|
||||||
|
|
||||||
homeKey := homedir.Key()
|
homeDir, err := os.UserHomeDir()
|
||||||
homeVal := homedir.Get()
|
assert.NilError(t, err)
|
||||||
|
|
||||||
defer func() { os.Setenv(homeKey, homeVal) }()
|
defer func() { os.Setenv(homeKey, homeDir) }()
|
||||||
os.Setenv(homeKey, tmpHome)
|
os.Setenv(homeKey, tmpHome)
|
||||||
|
|
||||||
fn := filepath.Join(tmpHome, oldConfigfile)
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
||||||
|
|
Loading…
Reference in New Issue