diff --git a/.gitignore b/.gitignore index 76906fd06..915d9f44b 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ resource/metas.go local-* coverage.txt oss/lib/ossutil_test* +coverage.html \ No newline at end of file diff --git a/config/configuration_test.go b/config/configuration_test.go index fee74d31c..79e9ed7a5 100644 --- a/config/configuration_test.go +++ b/config/configuration_test.go @@ -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", } @@ -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) @@ -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) { @@ -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) diff --git a/config/hello_test.go b/config/hello_test.go index 98c38c81e..d0d85b1a8 100644 --- a/config/hello_test.go +++ b/config/hello_test.go @@ -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" + diff --git a/config/profile.go b/config/profile.go index af842f361..df7a31d5e 100644 --- a/config/profile.go +++ b/config/profile.go @@ -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") } @@ -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 == "" { @@ -211,6 +217,7 @@ func (cp *Profile) OverwriteWithFlags(ctx *cli.Context) { cp.RegionId = os.Getenv("REGION") } } + AutoModeRecognition(cp) } diff --git a/config/profile_test.go b/config/profile_test.go index e40c1bc66..8de1e9031 100644 --- a/config/profile_test.go +++ b/config/profile_test.go @@ -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) } diff --git a/openapi/commando_test.go b/openapi/commando_test.go index 75302323b..64101b846 100644 --- a/openapi/commando_test.go +++ b/openapi/commando_test.go @@ -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"