Skip to content
Open
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
10 changes: 5 additions & 5 deletions config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
35 changes: 7 additions & 28 deletions config/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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"}}}
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion config/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down