Log messages are lower-case event names with their details as key/value
attributes ("error" for errors), in place of formatted strings.
Loggable package take a WithLogger option as part of their "New*" call,
defaulting to slog.Default(). Each tags its lines with its `component`.
Some debug loggers print directly rather than using slog when in
terminal logging mode.
36 lines
691 B
Go
36 lines
691 B
Go
package cli
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log/slog"
|
|
"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 {
|
|
slog.Error("reading the answer", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
response = strings.ToLower(strings.TrimSpace(response))
|
|
|
|
switch response {
|
|
case "y", "yes":
|
|
return true
|
|
case "n", "no":
|
|
return false
|
|
}
|
|
}
|
|
}
|