// Package cliout writes command output and remembers the first failure, so a // run of prints needs one error check at the end instead of one per line. package cliout import ( "fmt" "io" ) // Writer wraps an io.Writer. After the first write error every later write is // skipped, and Err reports that error. A CLI returns Err so a closed pipe or a // full disk becomes exit code 1 instead of silently truncated output. type Writer struct { w io.Writer err error } // New returns a Writer that prints to w. func New(w io.Writer) *Writer { return &Writer{w: w} } // Printf formats like fmt.Fprintf. func (w *Writer) Printf(format string, a ...any) { if w.err == nil { _, w.err = fmt.Fprintf(w.w, format, a...) } } // Println formats like fmt.Fprintln. func (w *Writer) Println(a ...any) { if w.err == nil { _, w.err = fmt.Fprintln(w.w, a...) } } // Write makes Writer an io.Writer, for example for a tabwriter. It reports // success after a failure so callers keep going; check Err once at the end. func (w *Writer) Write(p []byte) (int, error) { if w.err == nil { _, w.err = w.w.Write(p) } return len(p), nil } // Err returns the first write error, or nil. func (w *Writer) Err() error { return w.err }