35 lines
640 B
Go
35 lines
640 B
Go
package cli
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// Confirm asks the user the yes/no question s 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(s string) bool {
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
for {
|
|
fmt.Printf("%s [y/n]: ", s)
|
|
|
|
response, err := reader.ReadString('\n')
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
response = strings.ToLower(strings.TrimSpace(response))
|
|
|
|
switch response {
|
|
case "y", "yes":
|
|
return true
|
|
case "n", "no":
|
|
return false
|
|
}
|
|
}
|
|
}
|