diff --git a/internal/pkg/cli/exec.go b/internal/pkg/cli/exec.go index e428dcba436..41a9553e7a7 100644 --- a/internal/pkg/cli/exec.go +++ b/internal/pkg/cli/exec.go @@ -14,5 +14,5 @@ type execVars struct { command string taskID string containerName string - skipConfirmation bool + skipConfirmation *bool // If nil, we will prompt to upgrade the ssm plugin. } diff --git a/internal/pkg/cli/flag.go b/internal/pkg/cli/flag.go index 15341fc11fb..3c3ea7d10b8 100644 --- a/internal/pkg/cli/flag.go +++ b/internal/pkg/cli/flag.go @@ -155,6 +155,7 @@ const ( pipelineFlagDescription = "Name of the pipeline." profileFlagDescription = "Name of the profile." yesFlagDescription = "Skips confirmation prompt." + execYesFlagDescription = "Optional. Whether to update the Session Manager Plugin." jsonFlagDescription = "Optional. Outputs in JSON format." imageTagFlagDescription = `Optional. The container image tag.` diff --git a/internal/pkg/cli/svc_exec.go b/internal/pkg/cli/svc_exec.go index cd05efd3f46..0d6e44ebae0 100644 --- a/internal/pkg/cli/svc_exec.go +++ b/internal/pkg/cli/svc_exec.go @@ -207,7 +207,10 @@ func (o *svcExecOpts) selectContainer() string { return o.name } -func validateSSMBinary(prompt prompter, manager ssmPluginManager, skipConfirmation bool) error { +func validateSSMBinary(prompt prompter, manager ssmPluginManager, skipConfirmation *bool) error { + if skipConfirmation != nil && !aws.BoolValue(skipConfirmation) { + return nil + } err := manager.ValidateBinary() if err == nil { return nil @@ -215,7 +218,7 @@ func validateSSMBinary(prompt prompter, manager ssmPluginManager, skipConfirmati switch v := err.(type) { case *exec.ErrSSMPluginNotExist: // If ssm plugin is not install, prompt users to install the plugin. - if !skipConfirmation { + if skipConfirmation == nil { confirmInstall, err := prompt.Confirm(ssmPluginInstallPrompt, ssmPluginInstallPromptHelp) if err != nil { return fmt.Errorf("prompt to confirm installing the plugin: %w", err) @@ -230,7 +233,7 @@ func validateSSMBinary(prompt prompter, manager ssmPluginManager, skipConfirmati return nil case *exec.ErrOutdatedSSMPlugin: // If ssm plugin is not up to date, prompt users to update the plugin. - if !skipConfirmation { + if skipConfirmation == nil { confirmUpdate, err := prompt.Confirm( fmt.Sprintf(ssmPluginUpdatePrompt, v.CurrentVersion, v.LatestVersion), "") if err != nil { @@ -258,6 +261,7 @@ https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-wor // buildSvcExecCmd builds the command for execute a running container in a service. func buildSvcExecCmd() *cobra.Command { vars := execVars{} + var skipPrompt bool cmd := &cobra.Command{ Use: "exec", Short: "Execute a command in a running container part of a service.", @@ -271,6 +275,12 @@ func buildSvcExecCmd() *cobra.Command { if err != nil { return err } + if cmd.Flags().Changed(yesFlag) { + opts.skipConfirmation = aws.Bool(false) + if skipPrompt { + opts.skipConfirmation = aws.Bool(true) + } + } if err := opts.Validate(); err != nil { return err } @@ -286,7 +296,7 @@ func buildSvcExecCmd() *cobra.Command { cmd.Flags().StringVarP(&vars.command, commandFlag, commandFlagShort, defaultCommand, execCommandFlagDescription) cmd.Flags().StringVar(&vars.taskID, taskIDFlag, "", taskIDFlagDescription) cmd.Flags().StringVar(&vars.containerName, containerFlag, "", containerFlagDescription) - cmd.Flags().BoolVar(&vars.skipConfirmation, yesFlag, false, yesFlagDescription) + cmd.Flags().BoolVar(&skipPrompt, yesFlag, false, execYesFlagDescription) cmd.SetUsageTemplate(template.Usage) return cmd diff --git a/internal/pkg/cli/svc_exec_test.go b/internal/pkg/cli/svc_exec_test.go index 5689d299a03..dd270784183 100644 --- a/internal/pkg/cli/svc_exec_test.go +++ b/internal/pkg/cli/svc_exec_test.go @@ -37,7 +37,7 @@ func TestSvcExec_Validate(t *testing.T) { ) mockErr := errors.New("some error") testCases := map[string]struct { - skipConfirmation bool + skipConfirmation *bool setupMocks func(mocks execSvcMocks) wantedError error @@ -76,6 +76,20 @@ func TestSvcExec_Validate(t *testing.T) { wantedError: fmt.Errorf("some error"), }, + "skip without installing/updating if yes flag is set to be false": { + skipConfirmation: aws.Bool(false), + setupMocks: func(m execSvcMocks) { + gomock.InOrder( + m.storeSvc.EXPECT().GetApplication("my-app").Return(&config.Application{ + Name: "my-app", + }, nil), + m.storeSvc.EXPECT().GetEnvironment("my-app", "my-env").Return(&config.Environment{ + Name: "my-env", + }, nil), + m.storeSvc.EXPECT().GetService("my-app", "my-svc").Return(&config.Workload{}, nil), + ) + }, + }, "should bubble error if cannot validate ssm plugin": { setupMocks: func(m execSvcMocks) { gomock.InOrder( @@ -251,8 +265,8 @@ func TestSvcExec_Validate(t *testing.T) { wantedError: nil, }, - "valid case with ssm plugin updating and skip confirming": { - skipConfirmation: true, + "valid case with ssm plugin updating and skip confirming to install": { + skipConfirmation: aws.Bool(true), setupMocks: func(m execSvcMocks) { gomock.InOrder( m.storeSvc.EXPECT().GetApplication("my-app").Return(&config.Application{ diff --git a/internal/pkg/cli/task_exec.go b/internal/pkg/cli/task_exec.go index 6e0ef92b7c8..fcfc064a28d 100644 --- a/internal/pkg/cli/task_exec.go +++ b/internal/pkg/cli/task_exec.go @@ -192,6 +192,7 @@ func (o *taskExecOpts) configSession() (*session.Session, error) { // buildTaskExecCmd builds the command for execute a running container in a one-off task. func buildTaskExecCmd() *cobra.Command { + var skipPrompt bool vars := taskExecVars{} cmd := &cobra.Command{ Use: "exec", @@ -208,6 +209,12 @@ func buildTaskExecCmd() *cobra.Command { if err != nil { return err } + if cmd.Flags().Changed(yesFlag) { + opts.skipConfirmation = aws.Bool(false) + if skipPrompt { + opts.skipConfirmation = aws.Bool(true) + } + } if err := opts.Validate(); err != nil { return err } @@ -223,7 +230,7 @@ func buildTaskExecCmd() *cobra.Command { cmd.Flags().StringVarP(&vars.command, commandFlag, commandFlagShort, defaultCommand, execCommandFlagDescription) cmd.Flags().StringVar(&vars.taskID, taskIDFlag, "", taskIDFlagDescription) cmd.Flags().BoolVar(&vars.useDefault, taskDefaultFlag, false, taskExecDefaultFlagDescription) - cmd.Flags().BoolVar(&vars.skipConfirmation, yesFlag, false, yesFlagDescription) + cmd.Flags().BoolVar(&skipPrompt, yesFlag, false, execYesFlagDescription) cmd.SetUsageTemplate(template.Usage) return cmd