From 885e4cde81663537437e119067f9058c6d342ba0 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 18 Dec 2017 11:38:51 -0500 Subject: [PATCH] don't loop indefinitely in confirm method Only check for input twice in the meta.confirm method. This prevents an errant newline from aborting the run while allowing Terraform to exit if there is no input available. We don't just check for a tty, since we still rely on being able to pipe input in for testing. Remove the redundant confirmation loops in the migration code, and only use the confirm method. --- command/meta.go | 4 +++- command/meta_backend_migrate.go | 33 ++------------------------------- 2 files changed, 5 insertions(+), 32 deletions(-) diff --git a/command/meta.go b/command/meta.go index 27f7765f9..04c757497 100644 --- a/command/meta.go +++ b/command/meta.go @@ -476,7 +476,8 @@ func (m *Meta) confirm(opts *terraform.InputOpts) (bool, error) { if !m.Input() { return false, errors.New("input is disabled") } - for { + + for i := 0; i < 2; i++ { v, err := m.UIInput().Input(opts) if err != nil { return false, fmt.Errorf( @@ -490,6 +491,7 @@ func (m *Meta) confirm(opts *terraform.InputOpts) (bool, error) { return true, nil } } + return false, nil } // showDiagnostics displays error and warning messages in the UI. diff --git a/command/meta_backend_migrate.go b/command/meta_backend_migrate.go index 552d70887..21c33d66d 100644 --- a/command/meta_backend_migrate.go +++ b/command/meta_backend_migrate.go @@ -346,22 +346,7 @@ func (m *Meta) backendMigrateEmptyConfirm(one, two state.State, opts *backendMig opts.OneType, opts.TwoType), } - // Confirm with the user that the copy should occur - for { - v, err := m.UIInput().Input(inputOpts) - if err != nil { - return false, fmt.Errorf( - "Error asking for state copy action: %s", err) - } - - switch strings.ToLower(v) { - case "no": - return false, nil - - case "yes": - return true, nil - } - } + return m.confirm(inputOpts) } func (m *Meta) backendMigrateNonEmptyConfirm( @@ -410,21 +395,7 @@ func (m *Meta) backendMigrateNonEmptyConfirm( } // Confirm with the user that the copy should occur - for { - v, err := m.UIInput().Input(inputOpts) - if err != nil { - return false, fmt.Errorf( - "Error asking for state copy action: %s", err) - } - - switch strings.ToLower(v) { - case "no": - return false, nil - - case "yes": - return true, nil - } - } + return m.confirm(inputOpts) } type backendMigrateOpts struct {