Skip to content

Commit

Permalink
improve get value from envs
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Feb 2, 2024
1 parent f5e4133 commit 8bf9e5e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 2 additions & 8 deletions config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"runtime"

"github.com/aliyun/aliyun-cli/cli"
"github.com/aliyun/aliyun-cli/util"
)

const (
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion config/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
37 changes: 5 additions & 32 deletions config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 == "" {
Expand Down
13 changes: 13 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -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 ""
}
16 changes: 16 additions & 0 deletions util/util_test.go
Original file line number Diff line number Diff line change
@@ -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"))
}

0 comments on commit 8bf9e5e

Please sign in to comment.