Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/pkg/cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
1 change: 1 addition & 0 deletions internal/pkg/cli/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.`
Expand Down
18 changes: 14 additions & 4 deletions internal/pkg/cli/svc_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,18 @@ 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
}
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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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.",
Expand All @@ -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
}
Expand All @@ -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
Expand Down
20 changes: 17 additions & 3 deletions internal/pkg/cli/svc_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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{
Expand Down
9 changes: 8 additions & 1 deletion internal/pkg/cli/task_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
}
Expand All @@ -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
Expand Down