2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
// ErrInvalidInput is an error where user entered invalid input.
|
|
|
|
var ErrInvalidInput = errors.New("user made invalid input")
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
// askToConfirm asks user to confirm an action.
|
|
|
|
// The user will be asked the handed question and can answer with
|
|
|
|
// yes or no.
|
|
|
|
func askToConfirm(cmd *cobra.Command, question string) (bool, error) {
|
|
|
|
reader := bufio.NewReader(cmd.InOrStdin())
|
|
|
|
cmd.Printf("%s [y/n]: ", question)
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
resp, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
resp = strings.ToLower(strings.TrimSpace(resp))
|
|
|
|
if resp == "n" || resp == "no" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if resp == "y" || resp == "yes" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
cmd.Printf("Type 'y' or 'yes' to confirm, or abort action with 'n' or 'no': ")
|
|
|
|
}
|
|
|
|
return false, ErrInvalidInput
|
|
|
|
}
|