Skip to content

Commit 2487484

Browse files
JacksonTianyndu13
authored andcommitted
improve get value from envs
1 parent f5e4133 commit 2487484

File tree

6 files changed

+45
-42
lines changed

6 files changed

+45
-42
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ release_windows:
6969
rm aliyun.exe
7070

7171
fmt:
72-
go fmt ./cli/... ./config/... ./i18n/... ./main/... ./openapi/... ./oss/... ./resource/... ./meta/...
72+
go fmt ./util/... ./cli/... ./config/... ./i18n/... ./main/... ./openapi/... ./oss/... ./resource/... ./meta/...
7373

7474
test:
75-
LANG="en_US.UTF-8" go test -race -coverprofile=coverage.txt -covermode=atomic ./cli/... ./config/... ./i18n/... ./main/... ./openapi/... ./meta/...
75+
LANG="en_US.UTF-8" go test -race -coverprofile=coverage.txt -covermode=atomic ./util/... ./cli/... ./config/... ./i18n/... ./main/... ./openapi/... ./meta/...
7676
go tool cover -html=coverage.txt -o coverage.html

config/configuration.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"runtime"
2121

2222
"github.com/aliyun/aliyun-cli/cli"
23+
"github.com/aliyun/aliyun-cli/util"
2324
)
2425

2526
const (
@@ -69,15 +70,11 @@ func (c *Configuration) GetProfile(pn string) (Profile, bool) {
6970
func (c *Configuration) GetCurrentProfile(ctx *cli.Context) Profile {
7071
profileName := ProfileFlag(ctx.Flags()).GetStringOrDefault(c.CurrentProfile)
7172
if profileName == "" || profileName == "default" {
72-
switch {
73-
case os.Getenv("ALIBABACLOUD_PROFILE") != "":
74-
profileName = os.Getenv("ALIBABACLOUD_PROFILE")
75-
case os.Getenv("ALIBABA_CLOUD_PROFILE") != "":
76-
profileName = os.Getenv("ALIBABA_CLOUD_PROFILE")
77-
case os.Getenv("ALICLOUD_PROFILE") != "":
78-
profileName = os.Getenv("ALICLOUD_PROFILE")
73+
if v := util.GetFromEnv("ALIBABACLOUD_PROFILE", "ALIBABA_CLOUD_PROFILE", "ALICLOUD_PROFILE"); v != "" {
74+
profileName = v
7975
}
8076
}
77+
8178
p, _ := c.GetProfile(profileName)
8279
p.OverwriteWithFlags(ctx)
8380
return p

config/configuration_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
66
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
7+
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
@@ -278,5 +278,9 @@ func TestLoadProfileWithContext(t *testing.T) {
278278
ctx.Flags().Get("profile").SetAssigned(true)
279279
_, err = LoadProfileWithContext(ctx)
280280
assert.EqualError(t, err, "region can't be empty")
281+
}
281282

283+
func TestGetHomePath(t *testing.T) {
284+
home := GetHomePath()
285+
assert.NotEqual(t, "", home)
282286
}

config/profile.go

+5-32
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
3333
"github.com/aliyun/aliyun-cli/cli"
3434
"github.com/aliyun/aliyun-cli/i18n"
35+
"github.com/aliyun/aliyun-cli/util"
3536
credentialsv2 "github.com/aliyun/credentials-go/credentials"
3637
jmespath "github.com/jmespath/go-jmespath"
3738
)
@@ -177,47 +178,19 @@ func (cp *Profile) OverwriteWithFlags(ctx *cli.Context) {
177178
cp.ProcessCommand = ProcessCommandFlag(ctx.Flags()).GetStringOrDefault(cp.ProcessCommand)
178179

179180
if cp.AccessKeyId == "" {
180-
switch {
181-
case os.Getenv("ALIBABACLOUD_ACCESS_KEY_ID") != "":
182-
cp.AccessKeyId = os.Getenv("ALIBABACLOUD_ACCESS_KEY_ID")
183-
case os.Getenv("ALICLOUD_ACCESS_KEY_ID") != "":
184-
cp.AccessKeyId = os.Getenv("ALICLOUD_ACCESS_KEY_ID")
185-
case os.Getenv("ACCESS_KEY_ID") != "":
186-
cp.AccessKeyId = os.Getenv("ACCESS_KEY_ID")
187-
}
181+
cp.AccessKeyId = util.GetFromEnv("ALIBABACLOUD_ACCESS_KEY_ID", "ALICLOUD_ACCESS_KEY_ID", "ACCESS_KEY_ID")
188182
}
189183

190184
if cp.AccessKeySecret == "" {
191-
switch {
192-
case os.Getenv("ALIBABACLOUD_ACCESS_KEY_SECRET") != "":
193-
cp.AccessKeySecret = os.Getenv("ALIBABACLOUD_ACCESS_KEY_SECRET")
194-
case os.Getenv("ALICLOUD_ACCESS_KEY_SECRET") != "":
195-
cp.AccessKeySecret = os.Getenv("ALICLOUD_ACCESS_KEY_SECRET")
196-
case os.Getenv("ACCESS_KEY_SECRET") != "":
197-
cp.AccessKeySecret = os.Getenv("ACCESS_KEY_SECRET")
198-
}
185+
cp.AccessKeySecret = util.GetFromEnv("ALIBABACLOUD_ACCESS_KEY_SECRET", "ALICLOUD_ACCESS_KEY_SECRET", "ACCESS_KEY_SECRET")
199186
}
200187

201188
if cp.StsToken == "" {
202-
switch {
203-
case os.Getenv("ALIBABACLOUD_SECURITY_TOKEN") != "":
204-
cp.StsToken = os.Getenv("ALIBABACLOUD_SECURITY_TOKEN")
205-
case os.Getenv("ALICLOUD_SECURITY_TOKEN") != "":
206-
cp.StsToken = os.Getenv("ALICLOUD_SECURITY_TOKEN")
207-
case os.Getenv("SECURITY_TOKEN") != "":
208-
cp.StsToken = os.Getenv("SECURITY_TOKEN")
209-
}
189+
cp.StsToken = util.GetFromEnv("ALIBABACLOUD_SECURITY_TOKEN", "ALICLOUD_SECURITY_TOKEN", "SECURITY_TOKEN")
210190
}
211191

212192
if cp.RegionId == "" {
213-
switch {
214-
case os.Getenv("ALIBABACLOUD_REGION_ID") != "":
215-
cp.RegionId = os.Getenv("ALIBABACLOUD_REGION_ID")
216-
case os.Getenv("ALICLOUD_REGION_ID") != "":
217-
cp.RegionId = os.Getenv("ALICLOUD_REGION_ID")
218-
case os.Getenv("REGION") != "":
219-
cp.RegionId = os.Getenv("REGION")
220-
}
193+
cp.RegionId = util.GetFromEnv("ALIBABACLOUD_REGION_ID", "ALICLOUD_REGION_ID", "REGION")
221194
}
222195

223196
if cp.CredentialsURI == "" {

util/util.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package util
2+
3+
import "os"
4+
5+
func GetFromEnv(args ...string) string {
6+
for _, key := range args {
7+
if value := os.Getenv(key); value != "" {
8+
return value
9+
}
10+
}
11+
12+
return ""
13+
}

util/util_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package util
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestGetFromEnv(t *testing.T) {
11+
os.Setenv("test1", "test1")
12+
os.Setenv("test2", "test2")
13+
assert.Equal(t, "test1", GetFromEnv("test1", "test2"))
14+
assert.Equal(t, "test1", GetFromEnv("test3", "test1", "test2"))
15+
assert.Equal(t, "", GetFromEnv("test3"))
16+
}

0 commit comments

Comments
 (0)