Switch automatically to interactive mode if one of the required flag is not set

Signed-off-by: Guillaume Lours <guillaume.lours@docker.com>
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Guillaume Lours 2020-05-11 16:20:50 +02:00 committed by Nicolas De Loof
parent 51e04a4702
commit 0864513bfe
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
3 changed files with 1 additions and 33 deletions

View File

@ -42,7 +42,6 @@ func (s setupOptions) unsetRequiredArgs() []string {
func SetupCommand() *cobra.Command {
var opts setupOptions
var interactive bool
cmd := &cobra.Command{
Use: "setup",
@ -53,16 +52,10 @@ func SetupCommand() *cobra.Command {
return plugin.PersistentPreRunE(cmd, args)
},
RunE: func(cmd *cobra.Command, args []string) error {
if interactive {
if requiredFlag := opts.unsetRequiredArgs(); len(requiredFlag) > 0 {
if err := interactiveCli(&opts); err != nil {
return err
}
} else {
if requiredFlag := opts.unsetRequiredArgs(); len(requiredFlag) > 0 {
fmt.Printf("required flag(s) %q not set", requiredFlag)
cmd.Help()
os.Exit(1)
}
}
if opts.accessKeyID != "" && opts.secretAccessKey != "" {
if err := saveCredentials(opts.context.Profile, opts.accessKeyID, opts.secretAccessKey); err != nil {
@ -78,7 +71,6 @@ func SetupCommand() *cobra.Command {
cmd.Flags().StringVarP(&opts.context.Region, "region", "r", "", "AWS region")
cmd.Flags().StringVarP(&opts.accessKeyID, "aws-key-id", "k", "", "AWS Access Key ID")
cmd.Flags().StringVarP(&opts.secretAccessKey, "aws-secret-key", "s", "", "AWS Secret Access Key")
cmd.Flags().BoolVarP(&interactive, "interactive", "", false, "Interactively setup Context and Credentials")
return cmd
}

View File

@ -9,17 +9,6 @@ import (
"gotest.tools/v3/icmd"
)
func TestSetupMandatoryArguments(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()
cmd.Command = dockerCli.Command("ecs", "setup")
usage := icmd.RunCmd(cmd).Assert(t, icmd.Expected{
ExitCode: 1,
}).Combined()
goldenFile := "setup-required-flags.golden"
golden.Assert(t, usage, goldenFile)
}
func TestDefaultAwsContextName(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()

View File

@ -1,13 +0,0 @@
required flag(s) ["profile" "cluster" "region"] not set
Usage: docker ecs setup
Options:
-k, --aws-key-id string AWS Access Key ID
-s, --aws-secret-key string AWS Secret Access Key
-c, --cluster string ECS cluster
--interactive Interactively setup Context and Credentials
-n, --name string Context Name (default "aws")
-p, --profile string AWS Profile
-r, --region string AWS region