Skip to content

Commit e23b96d

Browse files
authored
improve: command 'configure' can automatically configure the default profile when no profile name is specified (#192)
1 parent 2502c26 commit e23b96d

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

config/configure.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ func NewConfigureCommand() *cli.Command {
3434
}
3535
profileName, _ := ProfileFlag(ctx.Flags()).GetValue()
3636
mode, _ := ModeFlag(ctx.Flags()).GetValue()
37-
3837
return doConfigure(ctx, profileName, mode)
3938
},
4039
}
@@ -54,6 +53,22 @@ func doConfigure(ctx *cli.Context, profileName string, mode string) error {
5453
return err
5554
}
5655

56+
if profileName == "" {
57+
if conf.CurrentProfile == "" {
58+
profileName = "default"
59+
} else {
60+
profileName = conf.CurrentProfile
61+
originMode := string(conf.GetCurrentProfile(ctx).Mode)
62+
if mode == "" {
63+
mode = originMode
64+
} else if mode != originMode {
65+
cli.Printf(w, "Warning: You are changing the authentication type of profile '%s' from '%s' to '%s'\n", profileName, originMode, mode)
66+
}
67+
}
68+
}
69+
if mode == "" {
70+
mode = "AK"
71+
}
5772
cp, ok := conf.GetProfile(profileName)
5873
if !ok {
5974
cp = conf.NewProfile(profileName)

config/configure_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"io"
99
"runtime"
10+
"strings"
1011
"testing"
1112

1213
"github.com/stretchr/testify/assert"
@@ -91,16 +92,7 @@ func TestNewConfigureCommand(t *testing.T) {
9192
w.Reset()
9293
err = cmd.Run(ctx, []string{})
9394
assert.Nil(t, err)
94-
assert.Equal(t, "Configuring profile '' in '' authenticate mode...\n"+
95-
"Access Key Id []: Access Key Secret []: Default Region Id []: Default Output Format [json]: json (Only support json)\n"+
96-
"Default Language [zh|en] en: Saving profile[] ...Done.\n"+
97-
"-----------------------------------------------\n"+
98-
"!!! Configure Failed please configure again !!!\n"+
99-
"-----------------------------------------------\n"+
100-
"AccessKeyId/AccessKeySecret is empty! run `aliyun configure` first\n"+
101-
"-----------------------------------------------\n"+
102-
"!!! Configure Failed please configure again !!!\n"+
103-
"-----------------------------------------------\n", w.String())
95+
assert.Equal(t, "Configuring profile 'default' in 'AK' authenticate mode...\nAccess Key Id [*************************_id]: Access Key Secret [*****************************ret]: Default Region Id []: Default Output Format [json]: json (Only support json)\nDefault Language [zh|en] : Saving profile[default] ...Done.\n-----------------------------------------------\n!!! Configure Failed please configure again !!!\n-----------------------------------------------\ndefault RegionId is empty! run `aliyun configure` first\n-----------------------------------------------\n!!! Configure Failed please configure again !!!\n-----------------------------------------------\n", w.String())
10496
}
10597

10698
func TestDoConfigure(t *testing.T) {
@@ -126,6 +118,17 @@ func TestDoConfigure(t *testing.T) {
126118
err := doConfigure(ctx, "profile", "AK")
127119
assert.Nil(t, err)
128120
assert.Equal(t, "Configuring profile 'profile' in 'AK' authenticate mode...\nAccess Key Id []: Access Key Secret []: Default Region Id []: Default Output Format [json]: json (Only support json)\nDefault Language [zh|en] en: Saving profile[profile] ...Done.\n-----------------------------------------------\n!!! Configure Failed please configure again !!!\n-----------------------------------------------\nAccessKeyId/AccessKeySecret is empty! run `aliyun configure` first\n-----------------------------------------------\n!!! Configure Failed please configure again !!!\n-----------------------------------------------\n", w.String())
121+
w.Reset()
122+
123+
err = doConfigure(ctx, "", "")
124+
assert.Nil(t, err)
125+
assert.Equal(t, "Configuring profile 'default' in 'AK' authenticate mode...\nAccess Key Id [*************************_id]: Access Key Secret [*****************************ret]: Default Region Id []: Default Output Format [json]: json (Only support json)\nDefault Language [zh|en] : Saving profile[default] ...Done.\n-----------------------------------------------\n!!! Configure Failed please configure again !!!\n-----------------------------------------------\ndefault RegionId is empty! run `aliyun configure` first\n-----------------------------------------------\n!!! Configure Failed please configure again !!!\n-----------------------------------------------\n", w.String())
126+
w.Reset()
127+
128+
err = doConfigure(ctx, "", "StsToken")
129+
assert.Nil(t, err)
130+
assert.True(t, strings.Contains(w.String(), "Warning: You are changing the authentication type of profile 'default' from 'AK' to 'StsToken'\nConfiguring profile 'default' in 'StsToken' authenticate mode...\nAccess Key Id [*************************_id]: Access Key Secret [*****************************ret]: Sts Token []: Default Region Id []: Default Output Format [json]: json (Only support json)\nDefault Language [zh|en] : Saving profile[default] ...Done.\n-----------------------------------------------\n!!! Configure Failed please configure again !!!\n-----------------------------------------------\n"))
131+
w.Reset()
129132
}
130133

131134
func TestConfigureAK(t *testing.T) {

0 commit comments

Comments
 (0)