diff --git a/config/configuration.go b/config/configuration.go index 5ab6071e2..f04981a3e 100644 --- a/config/configuration.go +++ b/config/configuration.go @@ -91,7 +91,7 @@ func (c *Configuration) PutProfile(profile Profile) { } func LoadCurrentProfile() (Profile, error) { - return LoadProfile(GetConfigPath()+"/"+configFile, "") + return LoadProfile(GetConfigPath(GetHomePath())+"/"+configFile, "") } func LoadProfile(path string, name string) (Profile, error) { @@ -115,7 +115,7 @@ func getConfigurePath(ctx *cli.Context) (currentPath string) { if path, ok := ConfigurePathFlag(ctx.Flags()).GetValue(); ok { currentPath = path } else { - currentPath = GetConfigPath() + "/" + configFile + currentPath = GetConfigPath(GetHomePath()) + "/" + configFile } return } @@ -185,7 +185,7 @@ func SaveConfiguration(config *Configuration) (err error) { if err != nil { return } - path := GetConfigPath() + "/" + configFile + path := GetConfigPath(GetHomePath()) + "/" + configFile err = os.WriteFile(path, bytes, 0600) return } @@ -196,8 +196,8 @@ func NewConfigFromBytes(bytes []byte) (conf *Configuration, err error) { return } -func GetConfigPath() string { - path := hookGetHomePath(GetHomePath)() + configPath +func GetConfigPath(homePath string) string { + path := homePath + configPath if _, err := os.Stat(path); os.IsNotExist(err) { err = os.MkdirAll(path, 0755) if err != nil { diff --git a/config/configuration_test.go b/config/configuration_test.go index 97646fab6..f779e91df 100644 --- a/config/configuration_test.go +++ b/config/configuration_test.go @@ -118,13 +118,13 @@ func TestLoadProfile(t *testing.T) { } } //testcase 1 - p, err := LoadProfile(GetConfigPath()+"/"+configFile, "") + p, err := LoadProfile(GetConfigPath(".")+"/"+configFile, "") assert.Nil(t, err) p.parent = nil assert.Equal(t, Profile{Name: "default", Mode: AK, AccessKeyId: "default_aliyun_access_key_id", AccessKeySecret: "default_aliyun_access_key_secret", OutputFormat: "json"}, p) //testcase 2 - _, err = LoadProfile(GetConfigPath()+"/"+configFile, "hello") + _, err = LoadProfile(GetConfigPath(".")+"/"+configFile, "hello") assert.EqualError(t, err, "unknown profile hello, run configure to check") //LoadCurrentProfile testcase @@ -141,7 +141,7 @@ func TestLoadProfile(t *testing.T) { } } w.Reset() - p, err = LoadProfile(GetConfigPath()+"/"+configFile, "") + p, err = LoadProfile(GetConfigPath(".")+"/"+configFile, "") assert.Empty(t, p) assert.EqualError(t, err, "init config failed error") } @@ -155,17 +155,10 @@ func TestHomePath(t *testing.T) { } func TestGetConfigPath(t *testing.T) { - orighookGetHomePath := hookGetHomePath defer func() { os.RemoveAll("./.aliyun") - hookGetHomePath = orighookGetHomePath }() - hookGetHomePath = func(fn func() string) func() string { - return func() string { - return "." - } - } - assert.Equal(t, "./.aliyun", GetConfigPath()) + assert.Equal(t, "./.aliyun", GetConfigPath(".")) } func TestNewConfigFromBytes(t *testing.T) { @@ -202,22 +195,15 @@ func TestNewConfigFromBytes(t *testing.T) { } func TestSaveConfiguration(t *testing.T) { - orighookGetHomePath := hookGetHomePath defer func() { os.RemoveAll("./.aliyun") - hookGetHomePath = orighookGetHomePath }() - hookGetHomePath = func(fn func() string) func() string { - return func() string { - return "." - } - } conf := &Configuration{Profiles: []Profile{{Language: "en", Name: "default", Mode: "AK", AccessKeyId: "access_key_id", AccessKeySecret: "access_key_secret", RegionId: "cn-hangzhou", OutputFormat: "json"}}} bytes, err := json.MarshalIndent(conf, "", "\t") assert.Nil(t, err) err = SaveConfiguration(conf) assert.Nil(t, err) - file, err := os.Open(GetConfigPath() + "/" + configFile) + file, err := os.Open(GetConfigPath(".") + "/" + configFile) assert.Nil(t, err) buf := make([]byte, 1024) n, _ := file.Read(buf) @@ -226,20 +212,13 @@ func TestSaveConfiguration(t *testing.T) { } func TestLoadConfiguration(t *testing.T) { - orighookGetHomePath := hookGetHomePath defer func() { os.RemoveAll("./.aliyun") - hookGetHomePath = orighookGetHomePath }() - hookGetHomePath = func(fn func() string) func() string { - return func() string { - return "." - } - } w := new(bytes.Buffer) //testcase 1 - cf, err := LoadConfiguration(GetConfigPath() + "/" + configFile) + cf, err := LoadConfiguration(GetConfigPath(".") + "/" + configFile) assert.Nil(t, err) assert.Equal(t, &Configuration{CurrentProfile: "default", Profiles: []Profile{{Name: "default", Mode: "", OutputFormat: "json", Language: "en"}}}, cf) conf := &Configuration{Profiles: []Profile{{Language: "en", Name: "default", Mode: "AK", AccessKeyId: "access_key_id", AccessKeySecret: "access_key_secret", RegionId: "cn-hangzhou", OutputFormat: "json"}}} @@ -248,7 +227,7 @@ func TestLoadConfiguration(t *testing.T) { //testcase 2 w.Reset() - cf, err = LoadConfiguration(GetConfigPath() + "/" + configFile) + cf, err = LoadConfiguration(GetConfigPath(".") + "/" + configFile) assert.Equal(t, &Configuration{CurrentProfile: "", Profiles: []Profile{{Name: "default", Mode: "AK", AccessKeyId: "access_key_id", AccessKeySecret: "access_key_secret", RegionId: "cn-hangzhou", OutputFormat: "json", Language: "en"}}}, cf) assert.Nil(t, err) diff --git a/config/configure.go b/config/configure.go index ef86a9cc4..19e945629 100644 --- a/config/configure.go +++ b/config/configure.go @@ -34,7 +34,7 @@ var hookSaveConfiguration = func(fn func(config *Configuration) error) func(conf } func loadConfiguration() (*Configuration, error) { - return hookLoadConfiguration(LoadConfiguration)(GetConfigPath() + "/" + configFile) + return hookLoadConfiguration(LoadConfiguration)(GetConfigPath(GetHomePath()) + "/" + configFile) } func NewConfigureCommand() *cli.Command {