Skip to content

Commit 7e0991f

Browse files
committedJun 28, 2021
add --vpc support
1 parent 0c19241 commit 7e0991f

File tree

7 files changed

+53
-17
lines changed

7 files changed

+53
-17
lines changed
 

‎cli/command.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ func (c *Command) Flags() *FlagSet {
7575
}
7676

7777
func (c *Command) Execute(ctx *Context, args []string) {
78-
79-
//
8078
// if completion
8179
if ctx.completion != nil {
8280
args = ctx.completion.GetArgs()
@@ -148,35 +146,30 @@ func (c *Command) ExecuteComplete(ctx *Context, args []string) {
148146
}
149147

150148
func (c *Command) executeInner(ctx *Context, args []string) error {
151-
//
152149
// fmt.Printf(">>> Execute Command: %s args=%v\n", c.Name, args)
153150
parser := NewParser(args, ctx)
154151

155-
//
156152
// get next arg
157153
nextArg, _, err := parser.ReadNextArg()
158154
if err != nil {
159155
return err
160156
}
161-
//
157+
162158
// if next arg is help, run help
163159
if nextArg == "help" {
164160
ctx.help = true
165161
return c.executeInner(ctx, parser.GetRemains())
166162
}
167163

168-
//
169164
// if next args is not empty, try find sub commands
170165
if nextArg != "" {
171-
//
172166
// if has sub command, run it
173167
subCommand := c.GetSubCommand(nextArg)
174168
if subCommand != nil {
175169
ctx.EnterCommand(subCommand)
176170
return subCommand.executeInner(ctx, parser.GetRemains())
177171
}
178172

179-
//
180173
// no sub command and command.Run == nil
181174
// raise error
182175
if c.Run == nil {
@@ -192,7 +185,6 @@ func (c *Command) executeInner(ctx *Context, args []string) error {
192185
return fmt.Errorf("parse failed %s", err)
193186
}
194187

195-
//
196188
// check flags
197189
err = ctx.CheckFlags()
198190
if err != nil {

‎cli/context.go

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
//
2424
// default help flag
25-
2625
func HelpFlag(fs *FlagSet) *Flag {
2726
return fs.Get("help")
2827
}

‎config/configure.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ func NewConfigureCommand() *cli.Command {
4646
if len(args) > 0 {
4747
return cli.NewInvalidCommandError(args[0], ctx)
4848
}
49-
profileName, _ := ProfileFlag(ctx.Flags()).GetValue()
50-
mode, _ := ModeFlag(ctx.Flags()).GetValue()
51-
return doConfigure(ctx, profileName, mode)
49+
return doConfigure(ctx)
5250
},
5351
}
5452

@@ -59,14 +57,17 @@ func NewConfigureCommand() *cli.Command {
5957
return c
6058
}
6159

62-
func doConfigure(ctx *cli.Context, profileName string, mode string) error {
60+
func doConfigure(ctx *cli.Context) error {
6361
w := ctx.Writer()
6462

6563
conf, err := hookLoadConfiguration(LoadConfiguration)(GetConfigPath() + "/" + configFile)
6664
if err != nil {
6765
return err
6866
}
6967

68+
profileName, _ := ProfileFlag(ctx.Flags()).GetValue()
69+
mode, _ := ModeFlag(ctx.Flags()).GetValue()
70+
7071
if profileName == "" {
7172
if conf.CurrentProfile == "" {
7273
profileName = "default"

‎config/configure_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,23 @@ func TestDoConfigure(t *testing.T) {
144144
w := new(bytes.Buffer)
145145
ctx := cli.NewCommandContext(w)
146146
AddFlags(ctx.Flags())
147-
err := doConfigure(ctx, "profile", "AK")
147+
ProfileFlag(ctx.Flags()).SetValue("profile")
148+
ModeFlag(ctx.Flags()).SetValue("AK")
149+
err := doConfigure(ctx)
148150
assert.Nil(t, err)
149151
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())
150152
w.Reset()
151153

152-
err = doConfigure(ctx, "", "")
154+
ProfileFlag(ctx.Flags()).SetValue("")
155+
ModeFlag(ctx.Flags()).SetValue("")
156+
err = doConfigure(ctx)
153157
assert.Nil(t, err)
154158
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())
155159
w.Reset()
156160

157-
err = doConfigure(ctx, "", "StsToken")
161+
ProfileFlag(ctx.Flags()).SetValue("")
162+
ModeFlag(ctx.Flags()).SetValue("StsToken")
163+
err = doConfigure(ctx)
158164
assert.Nil(t, err)
159165
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"))
160166
w.Reset()

‎config/flags.go

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
const (
2222
ProfileFlagName = "profile"
2323
ModeFlagName = "mode"
24+
VpcFlagName = "vpc"
2425
AccessKeyIdFlagName = "access-key-id"
2526
AccessKeySecretFlagName = "access-key-secret"
2627
StsTokenFlagName = "sts-token"
@@ -62,6 +63,7 @@ func AddFlags(fs *cli.FlagSet) {
6263
fs.Add(NewSkipSecureVerify())
6364
fs.Add(NewExpiredSecondsFlag())
6465
fs.Add(NewProcessCommandFlag())
66+
fs.Add(NewVpcFlag())
6567
}
6668

6769
func ConnectTimeoutFlag(fs *cli.FlagSet) *cli.Flag {
@@ -76,6 +78,10 @@ func ModeFlag(fs *cli.FlagSet) *cli.Flag {
7678
return fs.Get(ModeFlagName)
7779
}
7880

81+
func VpcFlag(fs *cli.FlagSet) *cli.Flag {
82+
return fs.Get(VpcFlagName)
83+
}
84+
7985
func AccessKeyIdFlag(fs *cli.FlagSet) *cli.Flag {
8086
return fs.Get(AccessKeyIdFlagName)
8187
}
@@ -131,12 +137,15 @@ func RetryCountFlag(fs *cli.FlagSet) *cli.Flag {
131137
func SkipSecureVerify(fs *cli.FlagSet) *cli.Flag {
132138
return fs.Get(SkipSecureVerifyName)
133139
}
140+
134141
func ConfigurePathFlag(fs *cli.FlagSet) *cli.Flag {
135142
return fs.Get(ConfigurePathFlagName)
136143
}
144+
137145
func ExpiredSecondsFlag(fs *cli.FlagSet) *cli.Flag {
138146
return fs.Get(ExpiredSecondsFlagName)
139147
}
148+
140149
func ProcessCommandFlag(fs *cli.FlagSet) *cli.Flag {
141150
return fs.Get(ProcessCommandFlagName)
142151
}
@@ -170,6 +179,16 @@ func NewModeFlag() *cli.Flag {
170179
"使用 `--mode {AK|StsToken|RamRoleArn|EcsRamRole|RsaKeyPair|RamRoleArnWithRoleName}` 指定认证方式")}
171180
}
172181

182+
func NewVpcFlag() *cli.Flag {
183+
return &cli.Flag{
184+
Category: "config",
185+
Name: VpcFlagName,
186+
AssignedMode: cli.AssignedNone,
187+
Short: i18n.T(
188+
"use `--vpc` to enable vpc endpoint",
189+
"使用 `--vpc` 启用 VPC 接入点地址")}
190+
}
191+
173192
func NewAccessKeyIdFlag() *cli.Flag {
174193
return &cli.Flag{
175194
Category: "config",

‎config/hello.go

+5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ import (
2424
func DoHello(ctx *cli.Context, profile *Profile) {
2525
w := ctx.Writer()
2626

27+
vpc := VpcFlag(ctx.Flags()).IsAssigned()
2728
client, err := profile.GetClient(ctx)
29+
if vpc {
30+
client.Network = "vpc"
31+
}
2832

2933
if err != nil {
3034
cli.Println(w, "-----------------------------------------------")
@@ -36,6 +40,7 @@ func DoHello(ctx *cli.Context, profile *Profile) {
3640
cli.Println(w, "-----------------------------------------------")
3741
return
3842
}
43+
3944
request := ecs.CreateDescribeRegionsRequest()
4045
response := ecs.CreateDescribeRegionsResponse()
4146
if vendorEnv, ok := os.LookupEnv("ALIBABA_CLOUD_VENDOR"); ok {

‎openapi/flags.go

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
func AddFlags(fs *cli.FlagSet) {
2222
fs.Add(NewSecureFlag())
2323
fs.Add(NewForceFlag())
24+
// fs.Add(NewVpcFlag())
2425
fs.Add(NewEndpointFlag())
2526
fs.Add(NewVersionFlag())
2627
fs.Add(NewHeaderFlag())
@@ -38,6 +39,7 @@ func AddFlags(fs *cli.FlagSet) {
3839
const (
3940
SecureFlagName = "secure"
4041
ForceFlagName = "force"
42+
VpcFlagName = "vpc"
4143
EndpointFlagName = "endpoint"
4244
VersionFlagName = "version"
4345
HeaderFlagName = "header"
@@ -62,6 +64,10 @@ func ForceFlag(fs *cli.FlagSet) *cli.Flag {
6264
return fs.Get(ForceFlagName)
6365
}
6466

67+
func VpcFlag(fs *cli.FlagSet) *cli.Flag {
68+
return fs.Get(VpcFlagName)
69+
}
70+
6571
func EndpointFlag(fs *cli.FlagSet) *cli.Flag {
6672
return fs.Get(EndpointFlagName)
6773
}
@@ -125,6 +131,14 @@ func NewForceFlag() *cli.Flag {
125131
"添加 `--force` 开关可跳过API与参数的合法性检查")}
126132
}
127133

134+
func NewVpcFlag() *cli.Flag {
135+
return &cli.Flag{Category: "caller",
136+
Name: VpcFlagName, AssignedMode: cli.AssignedOnce,
137+
Short: i18n.T(
138+
"use `--vpc` to enable vpc endpoint",
139+
"使用 `--vpc` 来启用 VPC 接入点地址")}
140+
}
141+
128142
func NewEndpointFlag() *cli.Flag {
129143
return &cli.Flag{Category: "caller",
130144
Name: EndpointFlagName, AssignedMode: cli.AssignedOnce,

0 commit comments

Comments
 (0)