Skip to content

Commit 2cae7cd

Browse files
committed
add --vpc support
1 parent a0491d3 commit 2cae7cd

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
@@ -50,9 +50,7 @@ func NewConfigureCommand() *cli.Command {
5050
if len(args) > 0 {
5151
return cli.NewInvalidCommandError(args[0], ctx)
5252
}
53-
profileName, _ := ProfileFlag(ctx.Flags()).GetValue()
54-
mode, _ := ModeFlag(ctx.Flags()).GetValue()
55-
return doConfigure(ctx, profileName, mode)
53+
return doConfigure(ctx)
5654
},
5755
}
5856

@@ -63,14 +61,17 @@ func NewConfigureCommand() *cli.Command {
6361
return c
6462
}
6563

66-
func doConfigure(ctx *cli.Context, profileName string, mode string) error {
64+
func doConfigure(ctx *cli.Context) error {
6765
w := ctx.Writer()
6866

6967
conf, err := loadConfiguration()
7068
if err != nil {
7169
return err
7270
}
7371

72+
profileName, _ := ProfileFlag(ctx.Flags()).GetValue()
73+
mode, _ := ModeFlag(ctx.Flags()).GetValue()
74+
7475
if profileName == "" {
7576
if conf.CurrentProfile == "" {
7677
profileName = "default"

config/configure_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,23 @@ func TestDoConfigure(t *testing.T) {
151151
stderr := new(bytes.Buffer)
152152
ctx := cli.NewCommandContext(w, stderr)
153153
AddFlags(ctx.Flags())
154-
err := doConfigure(ctx, "profile", "AK")
154+
ProfileFlag(ctx.Flags()).SetValue("profile")
155+
ModeFlag(ctx.Flags()).SetValue("AK")
156+
err := doConfigure(ctx)
155157
assert.Nil(t, err)
156158
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())
157159
w.Reset()
158160

159-
err = doConfigure(ctx, "", "")
161+
ProfileFlag(ctx.Flags()).SetValue("")
162+
ModeFlag(ctx.Flags()).SetValue("")
163+
err = doConfigure(ctx)
160164
assert.Nil(t, err)
161165
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())
162166
w.Reset()
163167

164-
err = doConfigure(ctx, "", "StsToken")
168+
ProfileFlag(ctx.Flags()).SetValue("")
169+
ModeFlag(ctx.Flags()).SetValue("StsToken")
170+
err = doConfigure(ctx)
165171
assert.Nil(t, err)
166172
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"))
167173
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"
@@ -63,6 +64,7 @@ func AddFlags(fs *cli.FlagSet) {
6364
fs.Add(NewSkipSecureVerify())
6465
fs.Add(NewExpiredSecondsFlag())
6566
fs.Add(NewProcessCommandFlag())
67+
fs.Add(NewVpcFlag())
6668
}
6769

6870
func ConnectTimeoutFlag(fs *cli.FlagSet) *cli.Flag {
@@ -77,6 +79,10 @@ func ModeFlag(fs *cli.FlagSet) *cli.Flag {
7779
return fs.Get(ModeFlagName)
7880
}
7981

82+
func VpcFlag(fs *cli.FlagSet) *cli.Flag {
83+
return fs.Get(VpcFlagName)
84+
}
85+
8086
func AccessKeyIdFlag(fs *cli.FlagSet) *cli.Flag {
8187
return fs.Get(AccessKeyIdFlagName)
8288
}
@@ -136,12 +142,15 @@ func RetryCountFlag(fs *cli.FlagSet) *cli.Flag {
136142
func SkipSecureVerify(fs *cli.FlagSet) *cli.Flag {
137143
return fs.Get(SkipSecureVerifyName)
138144
}
145+
139146
func ConfigurePathFlag(fs *cli.FlagSet) *cli.Flag {
140147
return fs.Get(ConfigurePathFlagName)
141148
}
149+
142150
func ExpiredSecondsFlag(fs *cli.FlagSet) *cli.Flag {
143151
return fs.Get(ExpiredSecondsFlagName)
144152
}
153+
145154
func ProcessCommandFlag(fs *cli.FlagSet) *cli.Flag {
146155
return fs.Get(ProcessCommandFlagName)
147156
}
@@ -175,6 +184,16 @@ func NewModeFlag() *cli.Flag {
175184
"使用 `--mode {AK|StsToken|RamRoleArn|EcsRamRole|RsaKeyPair|RamRoleArnWithRoleName}` 指定认证方式")}
176185
}
177186

187+
func NewVpcFlag() *cli.Flag {
188+
return &cli.Flag{
189+
Category: "config",
190+
Name: VpcFlagName,
191+
AssignedMode: cli.AssignedNone,
192+
Short: i18n.T(
193+
"use `--vpc` to enable vpc endpoint",
194+
"使用 `--vpc` 启用 VPC 接入点地址")}
195+
}
196+
178197
func NewAccessKeyIdFlag() *cli.Flag {
179198
return &cli.Flag{
180199
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)