mirror of https://github.com/docker/cli.git
cli/config: prevent warning if HOME is not set
commitc2626a8270
replaced the use of github.com/docker/docker/pkg/homedir with Golang's os.UserHomeDir(). This change was partially reverted in7a279af43d
to account for situations where `$HOME` is not set. In situations where no configuration file is present in `~/.config/`, the CLI falls back to looking for the (deprecated) `~/.dockercfg` configuration file, which was still using `os.UserHomeDir()`, which produces an error/warning if `$HOME` is not set. This patch introduces a helper function and a global variable to get the user's home-directory. The global variable is used to prevent repeatedly looking up the user's information (which, depending on the setup can be a costly operation). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
9a3fdc1d64
commit
c85a37dbb4
|
@ -26,15 +26,29 @@ const (
|
||||||
var (
|
var (
|
||||||
initConfigDir sync.Once
|
initConfigDir sync.Once
|
||||||
configDir string
|
configDir string
|
||||||
|
homeDir string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// resetHomeDir is used in testing to resets the "homeDir" package variable to
|
||||||
|
// force re-lookup of the home directory between tests.
|
||||||
|
func resetHomeDir() {
|
||||||
|
homeDir = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func getHomeDir() string {
|
||||||
|
if homeDir == "" {
|
||||||
|
homeDir = homedir.Get()
|
||||||
|
}
|
||||||
|
return homeDir
|
||||||
|
}
|
||||||
|
|
||||||
func setConfigDir() {
|
func setConfigDir() {
|
||||||
if configDir != "" {
|
if configDir != "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
configDir = os.Getenv("DOCKER_CONFIG")
|
configDir = os.Getenv("DOCKER_CONFIG")
|
||||||
if configDir == "" {
|
if configDir == "" {
|
||||||
configDir = filepath.Join(homedir.Get(), configFileDir)
|
configDir = filepath.Join(getHomeDir(), configFileDir)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,11 +123,7 @@ 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
|
||||||
home, err := os.UserHomeDir()
|
filename = filepath.Join(getHomeDir(), oldConfigfile)
|
||||||
if err != nil {
|
|
||||||
return configFile, errors.Wrap(err, oldConfigfile)
|
|
||||||
}
|
|
||||||
filename = filepath.Join(home, oldConfigfile)
|
|
||||||
if file, err := os.Open(filename); err == nil {
|
if file, err := os.Open(filename); err == nil {
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
if err := configFile.LegacyLoadFromReader(file); err != nil {
|
if err := configFile.LegacyLoadFromReader(file); err != nil {
|
||||||
|
|
|
@ -115,6 +115,7 @@ password`: "Invalid Auth config file",
|
||||||
email`: "Invalid auth configuration file",
|
email`: "Invalid auth configuration file",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resetHomeDir()
|
||||||
tmpHome, err := ioutil.TempDir("", "config-test")
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(tmpHome)
|
defer os.RemoveAll(tmpHome)
|
||||||
|
@ -131,6 +132,7 @@ email`: "Invalid auth configuration file",
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOldValidAuth(t *testing.T) {
|
func TestOldValidAuth(t *testing.T) {
|
||||||
|
resetHomeDir()
|
||||||
tmpHome, err := ioutil.TempDir("", "config-test")
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(tmpHome)
|
defer os.RemoveAll(tmpHome)
|
||||||
|
@ -165,6 +167,7 @@ func TestOldValidAuth(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOldJSONInvalid(t *testing.T) {
|
func TestOldJSONInvalid(t *testing.T) {
|
||||||
|
resetHomeDir()
|
||||||
tmpHome, err := ioutil.TempDir("", "config-test")
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(tmpHome)
|
defer os.RemoveAll(tmpHome)
|
||||||
|
@ -184,6 +187,7 @@ func TestOldJSONInvalid(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOldJSON(t *testing.T) {
|
func TestOldJSON(t *testing.T) {
|
||||||
|
resetHomeDir()
|
||||||
tmpHome, err := ioutil.TempDir("", "config-test")
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(tmpHome)
|
defer os.RemoveAll(tmpHome)
|
||||||
|
|
Loading…
Reference in New Issue