Short and single-letter names in application code become full words (tests keep theirs). Wrapped calls, signatures and composite literals put one item per line, and one-line function bodies are expanded. Exported names and groups of constants get doc comments, and comments refer to the renamed identifiers.
36 lines
706 B
Go
36 lines
706 B
Go
package cli
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log/slog"
|
|
"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(prompt string) bool {
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
for {
|
|
fmt.Printf("%s [y/n]: ", prompt)
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|