mirror of https://github.com/docker/cli.git
Merge pull request #4384 from thaJeztah/config_sync
cli/config: add synchronisation for configDir (Dir, SetDir)
This commit is contained in:
commit
e382d43f20
|
@ -35,6 +35,13 @@ var (
|
||||||
configDir string
|
configDir string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// resetConfigDir is used in testing to reset the "configDir" package variable
|
||||||
|
// and its sync.Once to force re-lookup between tests.
|
||||||
|
func resetConfigDir() {
|
||||||
|
configDir = ""
|
||||||
|
initConfigDir = new(sync.Once)
|
||||||
|
}
|
||||||
|
|
||||||
// Dir returns the directory the configuration file is stored in
|
// Dir returns the directory the configuration file is stored in
|
||||||
func Dir() string {
|
func Dir() string {
|
||||||
initConfigDir.Do(func() {
|
initConfigDir.Do(func() {
|
||||||
|
@ -53,6 +60,8 @@ func ContextStoreDir() string {
|
||||||
|
|
||||||
// SetDir sets the directory the configuration file is stored in
|
// SetDir sets the directory the configuration file is stored in
|
||||||
func SetDir(dir string) {
|
func SetDir(dir string) {
|
||||||
|
// trigger the sync.Once to synchronise with Dir()
|
||||||
|
initConfigDir.Do(func() {})
|
||||||
configDir = filepath.Clean(dir)
|
configDir = filepath.Clean(dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -382,3 +382,12 @@ func TestConfigPath(t *testing.T) {
|
||||||
|
|
||||||
SetDir(oldDir)
|
SetDir(oldDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestSetDir verifies that Dir() does not overwrite the value set through
|
||||||
|
// SetDir() if it has not been run before.
|
||||||
|
func TestSetDir(t *testing.T) {
|
||||||
|
const expected = "my_config_dir"
|
||||||
|
resetConfigDir()
|
||||||
|
SetDir(expected)
|
||||||
|
assert.Check(t, is.Equal(Dir(), expected))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue