package cli import ( "bufio" "fmt" "io" "os" "strings" ) // Confirm asks the user the yes/no question prompt on stdin and reports whether // they answered yes. "y" and "yes" confirm, "n" and "no" refuse, in any case; // anything else asks again. It exits the program if stdin can't be read. func Confirm(out io.Writer, in io.Reader, prompt string) bool { reader := bufio.NewReader(in) for { _, err := fmt.Fprintf(out, "%s [y/n]: ", prompt) if err != nil { fmt.Fprintf(os.Stderr, "error asking question: %s\n", err) os.Exit(1) } response, err := reader.ReadString('\n') if err != nil { fmt.Fprintf(os.Stderr, "error reading answer: %s\n", err) os.Exit(1) } response = strings.ToLower(strings.TrimSpace(response)) switch response { case "y", "yes": return true case "n", "no": return false } } }