diff --git a/cli/command.go b/cli/command.go index b5f35d486..a84a192c6 100644 --- a/cli/command.go +++ b/cli/command.go @@ -75,8 +75,6 @@ func (c *Command) Flags() *FlagSet { } func (c *Command) Execute(ctx *Context, args []string) { - - // // if completion if ctx.completion != nil { args = ctx.completion.GetArgs() @@ -148,27 +146,23 @@ func (c *Command) ExecuteComplete(ctx *Context, args []string) { } func (c *Command) executeInner(ctx *Context, args []string) error { - // // fmt.Printf(">>> Execute Command: %s args=%v\n", c.Name, args) parser := NewParser(args, ctx) - // // get next arg nextArg, _, err := parser.ReadNextArg() if err != nil { return err } - // + // if next arg is help, run help if nextArg == "help" { ctx.help = true return c.executeInner(ctx, parser.GetRemains()) } - // // if next args is not empty, try find sub commands if nextArg != "" { - // // if has sub command, run it subCommand := c.GetSubCommand(nextArg) if subCommand != nil { @@ -176,7 +170,6 @@ func (c *Command) executeInner(ctx *Context, args []string) error { return subCommand.executeInner(ctx, parser.GetRemains()) } - // // no sub command and command.Run == nil // raise error if c.Run == nil { @@ -192,7 +185,6 @@ func (c *Command) executeInner(ctx *Context, args []string) error { return fmt.Errorf("parse failed %s", err) } - // // check flags err = ctx.CheckFlags() if err != nil { diff --git a/cli/context.go b/cli/context.go index 26ec0cbc6..9d407995b 100644 --- a/cli/context.go +++ b/cli/context.go @@ -22,7 +22,6 @@ import ( // // default help flag - func HelpFlag(fs *FlagSet) *Flag { return fs.Get("help") } diff --git a/config/configure.go b/config/configure.go index 76e99c6ac..598eb896e 100644 --- a/config/configure.go +++ b/config/configure.go @@ -50,9 +50,7 @@ func NewConfigureCommand() *cli.Command { if len(args) > 0 { return cli.NewInvalidCommandError(args[0], ctx) } - profileName, _ := ProfileFlag(ctx.Flags()).GetValue() - mode, _ := ModeFlag(ctx.Flags()).GetValue() - return doConfigure(ctx, profileName, mode) + return doConfigure(ctx) }, } @@ -63,7 +61,7 @@ func NewConfigureCommand() *cli.Command { return c } -func doConfigure(ctx *cli.Context, profileName string, mode string) error { +func doConfigure(ctx *cli.Context) error { w := ctx.Writer() conf, err := loadConfiguration() @@ -71,6 +69,9 @@ func doConfigure(ctx *cli.Context, profileName string, mode string) error { return err } + profileName, _ := ProfileFlag(ctx.Flags()).GetValue() + mode, _ := ModeFlag(ctx.Flags()).GetValue() + if profileName == "" { if conf.CurrentProfile == "" { profileName = "default" diff --git a/config/configure_test.go b/config/configure_test.go index dfc3bb0cf..3a32d82f4 100644 --- a/config/configure_test.go +++ b/config/configure_test.go @@ -151,17 +151,23 @@ func TestDoConfigure(t *testing.T) { stderr := new(bytes.Buffer) ctx := cli.NewCommandContext(w, stderr) AddFlags(ctx.Flags()) - err := doConfigure(ctx, "profile", "AK") + ProfileFlag(ctx.Flags()).SetValue("profile") + ModeFlag(ctx.Flags()).SetValue("AK") + err := doConfigure(ctx) assert.Nil(t, err) 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()) w.Reset() - err = doConfigure(ctx, "", "") + ProfileFlag(ctx.Flags()).SetValue("") + ModeFlag(ctx.Flags()).SetValue("") + err = doConfigure(ctx) assert.Nil(t, err) 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()) w.Reset() - err = doConfigure(ctx, "", "StsToken") + ProfileFlag(ctx.Flags()).SetValue("") + ModeFlag(ctx.Flags()).SetValue("StsToken") + err = doConfigure(ctx) assert.Nil(t, err) 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")) w.Reset() diff --git a/config/flags.go b/config/flags.go index c04d7c9ca..5327660bd 100644 --- a/config/flags.go +++ b/config/flags.go @@ -21,6 +21,7 @@ import ( const ( ProfileFlagName = "profile" ModeFlagName = "mode" + VpcFlagName = "vpc" AccessKeyIdFlagName = "access-key-id" AccessKeySecretFlagName = "access-key-secret" StsTokenFlagName = "sts-token" @@ -63,6 +64,7 @@ func AddFlags(fs *cli.FlagSet) { fs.Add(NewSkipSecureVerify()) fs.Add(NewExpiredSecondsFlag()) fs.Add(NewProcessCommandFlag()) + fs.Add(NewVpcFlag()) } func ConnectTimeoutFlag(fs *cli.FlagSet) *cli.Flag { @@ -77,6 +79,10 @@ func ModeFlag(fs *cli.FlagSet) *cli.Flag { return fs.Get(ModeFlagName) } +func VpcFlag(fs *cli.FlagSet) *cli.Flag { + return fs.Get(VpcFlagName) +} + func AccessKeyIdFlag(fs *cli.FlagSet) *cli.Flag { return fs.Get(AccessKeyIdFlagName) } @@ -136,12 +142,15 @@ func RetryCountFlag(fs *cli.FlagSet) *cli.Flag { func SkipSecureVerify(fs *cli.FlagSet) *cli.Flag { return fs.Get(SkipSecureVerifyName) } + func ConfigurePathFlag(fs *cli.FlagSet) *cli.Flag { return fs.Get(ConfigurePathFlagName) } + func ExpiredSecondsFlag(fs *cli.FlagSet) *cli.Flag { return fs.Get(ExpiredSecondsFlagName) } + func ProcessCommandFlag(fs *cli.FlagSet) *cli.Flag { return fs.Get(ProcessCommandFlagName) } @@ -175,6 +184,16 @@ func NewModeFlag() *cli.Flag { "使用 `--mode {AK|StsToken|RamRoleArn|EcsRamRole|RsaKeyPair|RamRoleArnWithRoleName}` 指定认证方式")} } +func NewVpcFlag() *cli.Flag { + return &cli.Flag{ + Category: "config", + Name: VpcFlagName, + AssignedMode: cli.AssignedNone, + Short: i18n.T( + "use `--vpc` to enable vpc endpoint", + "使用 `--vpc` 启用 VPC 接入点地址")} +} + func NewAccessKeyIdFlag() *cli.Flag { return &cli.Flag{ Category: "config", diff --git a/config/hello.go b/config/hello.go index 3df3fb536..47da56a25 100644 --- a/config/hello.go +++ b/config/hello.go @@ -24,7 +24,11 @@ import ( func DoHello(ctx *cli.Context, profile *Profile) { w := ctx.Writer() + vpc := VpcFlag(ctx.Flags()).IsAssigned() client, err := profile.GetClient(ctx) + if vpc { + client.Network = "vpc" + } if err != nil { cli.Println(w, "-----------------------------------------------") @@ -36,6 +40,7 @@ func DoHello(ctx *cli.Context, profile *Profile) { cli.Println(w, "-----------------------------------------------") return } + request := ecs.CreateDescribeRegionsRequest() response := ecs.CreateDescribeRegionsResponse() if vendorEnv, ok := os.LookupEnv("ALIBABA_CLOUD_VENDOR"); ok { diff --git a/openapi/flags.go b/openapi/flags.go index 50d46aa2e..895bcb9e3 100644 --- a/openapi/flags.go +++ b/openapi/flags.go @@ -21,6 +21,7 @@ import ( func AddFlags(fs *cli.FlagSet) { fs.Add(NewSecureFlag()) fs.Add(NewForceFlag()) + // fs.Add(NewVpcFlag()) fs.Add(NewEndpointFlag()) fs.Add(NewVersionFlag()) fs.Add(NewHeaderFlag()) @@ -38,6 +39,7 @@ func AddFlags(fs *cli.FlagSet) { const ( SecureFlagName = "secure" ForceFlagName = "force" + VpcFlagName = "vpc" EndpointFlagName = "endpoint" VersionFlagName = "version" HeaderFlagName = "header" @@ -62,6 +64,10 @@ func ForceFlag(fs *cli.FlagSet) *cli.Flag { return fs.Get(ForceFlagName) } +func VpcFlag(fs *cli.FlagSet) *cli.Flag { + return fs.Get(VpcFlagName) +} + func EndpointFlag(fs *cli.FlagSet) *cli.Flag { return fs.Get(EndpointFlagName) } @@ -125,6 +131,14 @@ func NewForceFlag() *cli.Flag { "添加 `--force` 开关可跳过API与参数的合法性检查")} } +func NewVpcFlag() *cli.Flag { + return &cli.Flag{Category: "caller", + Name: VpcFlagName, AssignedMode: cli.AssignedOnce, + Short: i18n.T( + "use `--vpc` to enable vpc endpoint", + "使用 `--vpc` 来启用 VPC 接入点地址")} +} + func NewEndpointFlag() *cli.Flag { return &cli.Flag{Category: "caller", Name: EndpointFlagName, AssignedMode: cli.AssignedOnce,