Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cliutil: return user's answer on prompt #1104

Merged
merged 1 commit into from
Jan 27, 2021
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
16 changes: 8 additions & 8 deletions pkg/cliutil/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,32 @@ func Prompt(prompt string) string {

// PromptForConfirmYes accepts yes / no from console by user, default to No and only return true
// if the user input is Yes
func PromptForConfirmYes(format string, a ...interface{}) bool {
func PromptForConfirmYes(format string, a ...interface{}) (bool, string) {
ans := Prompt(fmt.Sprintf(format, a...))
switch strings.TrimSpace(strings.ToLower(ans)) {
case "y", "yes":
return true
return true, ans
default:
return false
return false, ans
}
}

// PromptForConfirmNo accepts yes / no from console by user, default to Yes and only return true
// if the user input is No
func PromptForConfirmNo(format string, a ...interface{}) bool {
func PromptForConfirmNo(format string, a ...interface{}) (bool, string) {
ans := Prompt(fmt.Sprintf(format, a...))
switch strings.TrimSpace(strings.ToLower(ans)) {
case "n", "no":
return true
return true, ans
default:
return false
return false, ans
}
}

// PromptForConfirmOrAbortError accepts yes / no from console by user, generates AbortError if user does not input yes.
func PromptForConfirmOrAbortError(format string, a ...interface{}) error {
if !PromptForConfirmYes(format, a...) {
return errOperationAbort.New("Operation aborted by user")
if pass, ans := PromptForConfirmYes(format, a...); !pass {
return errOperationAbort.New("Operation aborted by user (with answer '%s')", ans)
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/manager/edit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (m *Manager) editTopo(origTopo spec.Topology, data []byte, skipConfirm bool
if err != nil {
fmt.Print(color.RedString("New topology could not be saved: "))
log.Infof("Failed to parse topology file: %v", err)
if !cliutil.PromptForConfirmNo("Do you want to continue editing? [Y/n]: ") {
if pass, _ := cliutil.PromptForConfirmNo("Do you want to continue editing? [Y/n]: "); !pass {
return m.editTopo(origTopo, newData, skipConfirm)
}
log.Infof("Nothing changed.")
Expand All @@ -114,7 +114,7 @@ func (m *Manager) editTopo(origTopo spec.Topology, data []byte, skipConfirm bool
if err := utils.ValidateSpecDiff(origTopo, newTopo); err != nil {
fmt.Print(color.RedString("New topology could not be saved: "))
log.Errorf("%s", err)
if !cliutil.PromptForConfirmNo("Do you want to continue editing? [Y/n]: ") {
if pass, _ := cliutil.PromptForConfirmNo("Do you want to continue editing? [Y/n]: "); !pass {
return m.editTopo(origTopo, newData, skipConfirm)
}
log.Infof("Nothing changed.")
Expand Down