package cli import ( "bufio" "fmt" "io" "os" "strings" "golang.org/x/term" ) func (a App) confirmAction(question string, yes bool) error { if yes { return nil } // An agent's Bash tool has no terminal: a prompt would block on a pipe that // never answers. Fail at once with the flag that makes the command // non-interactive instead. if !a.stdinInteractive() { return fmt.Errorf("%s\nconfirmation required but stdin is not a terminal; rerun with --yes to confirm non-interactively", question) } fmt.Fprint(a.Err, question+" [y/N]: ") line, _ := bufio.NewReader(a.In).ReadString('\n') if answer := strings.ToLower(strings.TrimSpace(line)); answer != "y" && answer != "yes" { return fmt.Errorf("action canceled; use --yes for explicit non-interactive confirmation") } return nil } // stdinInteractive reports whether a person can answer a prompt. A real file // must be a terminal; a nil reader cannot answer. Any other reader is an // embedder- or test-supplied answer stream and counts as interactive. func (a App) stdinInteractive() bool { if a.Interactive != nil { return a.Interactive() } return isTerminal(a.In) } // terminalUI reports whether a full-screen terminal UI can run: both stdin and // stdout must be terminals. Without one the CLI uses its JSON paths instead. func (a App) terminalUI() bool { if a.Interactive != nil { return a.Interactive() } return isTerminal(a.In) && isTerminal(a.Out) } func isTerminal(stream any) bool { switch s := stream.(type) { case nil: return false case *os.File: return s != nil && term.IsTerminal(int(s.Fd())) case io.Reader, io.Writer: return true } return false }