Skip to content

Commit

Permalink
fix default mode issue
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Mar 22, 2022
1 parent 69877d2 commit 4b1ccaa
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ resource/metas.go
local-*
coverage.txt
oss/lib/ossutil_test*
coverage.html
8 changes: 4 additions & 4 deletions config/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestCFNewProfile(t *testing.T) {
assert.Len(t, cf.Profiles, 1)
exp := Profile{
Name: "default",
Mode: AK,
Mode: "",
OutputFormat: "json",
Language: "en",
}
Expand All @@ -60,7 +60,7 @@ func TestConfiguration(t *testing.T) {
assert.False(t, ok)
assert.Equal(t, Profile{Name: "hh"}, p)
p, ok = cf.GetProfile("default")
assert.Equal(t, Profile{Name: "default", Mode: AK, OutputFormat: "json", Language: "en"}, p)
assert.Equal(t, Profile{Name: "default", Mode: "", OutputFormat: "json", Language: "en"}, p)

//PutProfile
assert.Len(t, cf.Profiles, 1)
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestConfiguration(t *testing.T) {
assert.Equal(t, Profile{Name: "test", Mode: StsToken, OutputFormat: "json", Language: "en"}, p)
os.Setenv("ALICLOUD_PROFILE", "")
p = cf.GetCurrentProfile(ctx)
assert.Equal(t, Profile{Name: "default", Mode: AK, OutputFormat: "json", Language: "en"}, p)
assert.Equal(t, Profile{Name: "default", Mode: "", OutputFormat: "json", Language: "en"}, p)
}

func TestLoadProfile(t *testing.T) {
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestLoadConfiguration(t *testing.T) {
//testcase 1
cf, err := LoadConfiguration(GetConfigPath() + "/" + configFile)
assert.Nil(t, err)
assert.Equal(t, &Configuration{CurrentProfile: "default", Profiles: []Profile{Profile{Name: "default", Mode: "AK", OutputFormat: "json", Language: "en"}}}, cf)
assert.Equal(t, &Configuration{CurrentProfile: "default", Profiles: []Profile{Profile{Name: "default", Mode: "", OutputFormat: "json", Language: "en"}}}, cf)
conf := &Configuration{Profiles: []Profile{Profile{Language: "en", Name: "default", Mode: "AK", AccessKeyId: "access_key_id", AccessKeySecret: "access_key_secret", RegionId: "cn-hangzhou", OutputFormat: "json"}}}
err = SaveConfiguration(conf)
assert.Nil(t, err)
Expand Down
1 change: 1 addition & 0 deletions config/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestDoHello(t *testing.T) {
ctx := cli.NewCommandContext(w, stderr)
ctx.Flags().AddByName("skip-secure-verify")
profile := NewProfile("default")
profile.Mode = AK

exw := "-----------------------------------------------\n" +
"!!! Configure Failed please configure again !!!\n" +
Expand Down
13 changes: 10 additions & 3 deletions config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,13 @@ type Profile struct {
func NewProfile(name string) Profile {
return Profile{
Name: name,
Mode: AK,
Mode: "",
OutputFormat: "json",
Language: "en",
}
}

func (cp *Profile) Validate() error {

if cp.RegionId == "" {
return fmt.Errorf("region can't be empty")
}
Expand Down Expand Up @@ -198,7 +197,14 @@ func (cp *Profile) OverwriteWithFlags(ctx *cli.Context) {
}

if cp.StsToken == "" {
cp.StsToken = os.Getenv("SECURITY_TOKEN")
switch {
case os.Getenv("ALIBABACLOUD_SECURITY_TOKEN") != "":
cp.StsToken = os.Getenv("ALIBABACLOUD_SECURITY_TOKEN")
case os.Getenv("ALICLOUD_SECURITY_TOKEN") != "":
cp.StsToken = os.Getenv("ALICLOUD_SECURITY_TOKEN")
case os.Getenv("SECURITY_TOKEN") != "":
cp.StsToken = os.Getenv("SECURITY_TOKEN")
}
}

if cp.RegionId == "" {
Expand All @@ -211,6 +217,7 @@ func (cp *Profile) OverwriteWithFlags(ctx *cli.Context) {
cp.RegionId = os.Getenv("REGION")
}
}

AutoModeRecognition(cp)
}

Expand Down
1 change: 1 addition & 0 deletions config/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestNewProfile(t *testing.T) {
exp := newProfile()
exp.Mode = AK
p := NewProfile("default")
p.Mode = AK
assert.Equal(t, exp, &p)
}

Expand Down
1 change: 1 addition & 0 deletions openapi/commando_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ func Test_complete(t *testing.T) {

func TestCreateInvoker(t *testing.T) {
profile := config.NewProfile("test")
profile.Mode = config.AK
profile.AccessKeyId = "AccessKeyId"
profile.AccessKeySecret = "AccessKeySecret"
profile.RegionId = "cn-hangzhou"
Expand Down

0 comments on commit 4b1ccaa

Please sign in to comment.