diff --git a/Makefile b/Makefile index d2538a3cf..2df94195a 100644 --- a/Makefile +++ b/Makefile @@ -69,8 +69,8 @@ release_windows: rm aliyun.exe fmt: - go fmt ./cli/... ./config/... ./i18n/... ./main/... ./openapi/... ./oss/... ./resource/... ./meta/... + go fmt ./util/... ./cli/... ./config/... ./i18n/... ./main/... ./openapi/... ./oss/... ./resource/... ./meta/... test: - LANG="en_US.UTF-8" go test -race -coverprofile=coverage.txt -covermode=atomic ./cli/... ./config/... ./i18n/... ./main/... ./openapi/... ./meta/... + LANG="en_US.UTF-8" go test -race -coverprofile=coverage.txt -covermode=atomic ./util/... ./cli/... ./config/... ./i18n/... ./main/... ./openapi/... ./meta/... go tool cover -html=coverage.txt -o coverage.html diff --git a/config/configuration.go b/config/configuration.go index 7ad7cf893..08e798879 100644 --- a/config/configuration.go +++ b/config/configuration.go @@ -20,6 +20,7 @@ import ( "runtime" "github.com/aliyun/aliyun-cli/cli" + "github.com/aliyun/aliyun-cli/util" ) const ( @@ -69,14 +70,7 @@ func (c *Configuration) GetProfile(pn string) (Profile, bool) { func (c *Configuration) GetCurrentProfile(ctx *cli.Context) Profile { profileName := ProfileFlag(ctx.Flags()).GetStringOrDefault(c.CurrentProfile) if profileName == "" || profileName == "default" { - switch { - case os.Getenv("ALIBABACLOUD_PROFILE") != "": - profileName = os.Getenv("ALIBABACLOUD_PROFILE") - case os.Getenv("ALIBABA_CLOUD_PROFILE") != "": - profileName = os.Getenv("ALIBABA_CLOUD_PROFILE") - case os.Getenv("ALICLOUD_PROFILE") != "": - profileName = os.Getenv("ALICLOUD_PROFILE") - } + profileName = util.GetFromEnv("ALIBABACLOUD_PROFILE", "ALIBABA_CLOUD_PROFILE", "ALICLOUD_PROFILE") } p, _ := c.GetProfile(profileName) p.OverwriteWithFlags(ctx) diff --git a/config/configuration_test.go b/config/configuration_test.go index d17c94dfd..df7e40562 100644 --- a/config/configuration_test.go +++ b/config/configuration_test.go @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, @@ -278,5 +278,9 @@ func TestLoadProfileWithContext(t *testing.T) { ctx.Flags().Get("profile").SetAssigned(true) _, err = LoadProfileWithContext(ctx) assert.EqualError(t, err, "region can't be empty") +} +func TestGetHomePath(t *testing.T) { + home := GetHomePath() + assert.NotEqual(t, "", home) } diff --git a/config/profile.go b/config/profile.go index 9683a2ad3..7dced9239 100644 --- a/config/profile.go +++ b/config/profile.go @@ -32,6 +32,7 @@ import ( "github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses" "github.com/aliyun/aliyun-cli/cli" "github.com/aliyun/aliyun-cli/i18n" + "github.com/aliyun/aliyun-cli/util" credentialsv2 "github.com/aliyun/credentials-go/credentials" jmespath "github.com/jmespath/go-jmespath" ) @@ -177,47 +178,19 @@ func (cp *Profile) OverwriteWithFlags(ctx *cli.Context) { cp.ProcessCommand = ProcessCommandFlag(ctx.Flags()).GetStringOrDefault(cp.ProcessCommand) if cp.AccessKeyId == "" { - switch { - case os.Getenv("ALIBABACLOUD_ACCESS_KEY_ID") != "": - cp.AccessKeyId = os.Getenv("ALIBABACLOUD_ACCESS_KEY_ID") - case os.Getenv("ALICLOUD_ACCESS_KEY_ID") != "": - cp.AccessKeyId = os.Getenv("ALICLOUD_ACCESS_KEY_ID") - case os.Getenv("ACCESS_KEY_ID") != "": - cp.AccessKeyId = os.Getenv("ACCESS_KEY_ID") - } + cp.AccessKeyId = util.GetFromEnv("ALIBABACLOUD_ACCESS_KEY_ID", "ALICLOUD_ACCESS_KEY_ID", "ACCESS_KEY_ID") } if cp.AccessKeySecret == "" { - switch { - case os.Getenv("ALIBABACLOUD_ACCESS_KEY_SECRET") != "": - cp.AccessKeySecret = os.Getenv("ALIBABACLOUD_ACCESS_KEY_SECRET") - case os.Getenv("ALICLOUD_ACCESS_KEY_SECRET") != "": - cp.AccessKeySecret = os.Getenv("ALICLOUD_ACCESS_KEY_SECRET") - case os.Getenv("ACCESS_KEY_SECRET") != "": - cp.AccessKeySecret = os.Getenv("ACCESS_KEY_SECRET") - } + cp.AccessKeySecret = util.GetFromEnv("ALIBABACLOUD_ACCESS_KEY_SECRET", "ALICLOUD_ACCESS_KEY_SECRET", "ACCESS_KEY_SECRET") } if cp.StsToken == "" { - 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") - } + cp.StsToken = util.GetFromEnv("ALIBABACLOUD_SECURITY_TOKEN", "ALICLOUD_SECURITY_TOKEN", "SECURITY_TOKEN") } if cp.RegionId == "" { - switch { - case os.Getenv("ALIBABACLOUD_REGION_ID") != "": - cp.RegionId = os.Getenv("ALIBABACLOUD_REGION_ID") - case os.Getenv("ALICLOUD_REGION_ID") != "": - cp.RegionId = os.Getenv("ALICLOUD_REGION_ID") - case os.Getenv("REGION") != "": - cp.RegionId = os.Getenv("REGION") - } + cp.RegionId = util.GetFromEnv("ALIBABACLOUD_REGION_ID", "ALICLOUD_REGION_ID", "REGION") } if cp.CredentialsURI == "" { diff --git a/util/util.go b/util/util.go new file mode 100644 index 000000000..aad1ced89 --- /dev/null +++ b/util/util.go @@ -0,0 +1,13 @@ +package util + +import "os" + +func GetFromEnv(args ...string) string { + for _, key := range args { + if value := os.Getenv(key); value != "" { + return value + } + } + + return "" +} diff --git a/util/util_test.go b/util/util_test.go new file mode 100644 index 000000000..9c8e1b65b --- /dev/null +++ b/util/util_test.go @@ -0,0 +1,16 @@ +package util + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetFromEnv(t *testing.T) { + os.Setenv("test1", "test1") + os.Setenv("test2", "test2") + assert.Equal(t, "test1", GetFromEnv("test1", "test2")) + assert.Equal(t, "test1", GetFromEnv("test3", "test1", "test2")) + assert.Equal(t, "", GetFromEnv("test3")) +}