|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/mattn/go-isatty" |
| 9 | + "github.com/muesli/termenv" |
| 10 | + "github.com/raystack/salt/cli/printer" |
| 11 | + "github.com/raystack/salt/cli/prompt" |
| 12 | + "github.com/raystack/salt/cli/terminal" |
| 13 | + "golang.org/x/term" |
| 14 | +) |
| 15 | + |
| 16 | +// IOStreams provides centralized access to standard I/O streams and |
| 17 | +// terminal capabilities for CLI commands. |
| 18 | +// |
| 19 | +// Use [System] for production and [Test] for tests. Commands access |
| 20 | +// it via [IO]: |
| 21 | +// |
| 22 | +// ios := cli.IO(cmd) |
| 23 | +// if !ios.CanPrompt() { |
| 24 | +// return fmt.Errorf("--yes required in non-interactive mode") |
| 25 | +// } |
| 26 | +type IOStreams struct { |
| 27 | + In io.ReadCloser // standard input |
| 28 | + Out io.Writer // standard output (may become pager pipe) |
| 29 | + ErrOut io.Writer // standard error |
| 30 | + |
| 31 | + inTTY bool |
| 32 | + outTTY bool |
| 33 | + errTTY bool |
| 34 | + |
| 35 | + colorEnabled bool |
| 36 | + neverPrompt bool |
| 37 | + |
| 38 | + pager *terminal.Pager |
| 39 | + pagerStarted bool |
| 40 | + |
| 41 | + // lazily created |
| 42 | + output *printer.Output |
| 43 | + prompter prompt.Prompter |
| 44 | +} |
| 45 | + |
| 46 | +// System creates IOStreams wired to the real terminal. |
| 47 | +func System() *IOStreams { |
| 48 | + outTTY := isTTYWriter(os.Stdout) |
| 49 | + return &IOStreams{ |
| 50 | + In: os.Stdin, |
| 51 | + Out: os.Stdout, |
| 52 | + ErrOut: os.Stderr, |
| 53 | + inTTY: isTTYWriter(os.Stdin), |
| 54 | + outTTY: outTTY, |
| 55 | + errTTY: isTTYWriter(os.Stderr), |
| 56 | + colorEnabled: outTTY && !termenv.EnvNoColor(), |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Test creates IOStreams backed by buffers for deterministic testing. |
| 61 | +// All TTY flags default to false and color is disabled. |
| 62 | +func Test() (ios *IOStreams, stdin *bytes.Buffer, stdout *bytes.Buffer, stderr *bytes.Buffer) { |
| 63 | + stdin = &bytes.Buffer{} |
| 64 | + stdout = &bytes.Buffer{} |
| 65 | + stderr = &bytes.Buffer{} |
| 66 | + ios = &IOStreams{ |
| 67 | + In: io.NopCloser(stdin), |
| 68 | + Out: stdout, |
| 69 | + ErrOut: stderr, |
| 70 | + } |
| 71 | + return |
| 72 | +} |
| 73 | + |
| 74 | +// IsStdinTTY reports whether standard input is a terminal. |
| 75 | +func (s *IOStreams) IsStdinTTY() bool { return s.inTTY } |
| 76 | + |
| 77 | +// IsStdoutTTY reports whether standard output is a terminal. |
| 78 | +func (s *IOStreams) IsStdoutTTY() bool { return s.outTTY } |
| 79 | + |
| 80 | +// IsStderrTTY reports whether standard error is a terminal. |
| 81 | +func (s *IOStreams) IsStderrTTY() bool { return s.errTTY } |
| 82 | + |
| 83 | +// SetStdinTTY overrides the stdin TTY flag (useful in tests). |
| 84 | +func (s *IOStreams) SetStdinTTY(v bool) { s.inTTY = v } |
| 85 | + |
| 86 | +// SetStdoutTTY overrides the stdout TTY flag (useful in tests). |
| 87 | +func (s *IOStreams) SetStdoutTTY(v bool) { s.outTTY = v; s.output = nil } |
| 88 | + |
| 89 | +// SetStderrTTY overrides the stderr TTY flag (useful in tests). |
| 90 | +func (s *IOStreams) SetStderrTTY(v bool) { s.errTTY = v } |
| 91 | + |
| 92 | +// SetColorEnabled overrides color detection (useful in tests). |
| 93 | +func (s *IOStreams) SetColorEnabled(v bool) { s.colorEnabled = v } |
| 94 | + |
| 95 | +// SetNeverPrompt disables interactive prompting regardless of TTY state. |
| 96 | +func (s *IOStreams) SetNeverPrompt(v bool) { s.neverPrompt = v } |
| 97 | + |
| 98 | +// ColorEnabled reports whether color output is active. |
| 99 | +func (s *IOStreams) ColorEnabled() bool { return s.colorEnabled } |
| 100 | + |
| 101 | +// CanPrompt reports whether interactive prompting is possible. |
| 102 | +// Returns false if prompting is disabled, or stdin/stdout are not terminals. |
| 103 | +func (s *IOStreams) CanPrompt() bool { |
| 104 | + return !s.neverPrompt && s.inTTY && s.outTTY |
| 105 | +} |
| 106 | + |
| 107 | +// TerminalWidth returns the terminal width in columns. |
| 108 | +// Returns 80 if the width cannot be determined. |
| 109 | +func (s *IOStreams) TerminalWidth() int { |
| 110 | + if f, ok := s.Out.(*os.File); ok { |
| 111 | + if w, _, err := term.GetSize(int(f.Fd())); err == nil && w > 0 { |
| 112 | + return w |
| 113 | + } |
| 114 | + } |
| 115 | + return 80 |
| 116 | +} |
| 117 | + |
| 118 | +// StartPager starts a pager process and redirects Out through it. |
| 119 | +// Does nothing if stdout is not a TTY or no pager command is configured. |
| 120 | +func (s *IOStreams) StartPager() error { |
| 121 | + if !s.outTTY { |
| 122 | + return nil |
| 123 | + } |
| 124 | + p := terminal.NewPager() |
| 125 | + p.Out = s.Out |
| 126 | + p.ErrOut = s.ErrOut |
| 127 | + if err := p.Start(); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + s.Out = p.Out |
| 131 | + s.pager = p |
| 132 | + s.pagerStarted = true |
| 133 | + s.output = nil // invalidate cached Output |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +// StopPager stops the pager process and restores the original Out. |
| 138 | +func (s *IOStreams) StopPager() { |
| 139 | + if s.pager != nil && s.pagerStarted { |
| 140 | + s.pager.Stop() |
| 141 | + s.pagerStarted = false |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// Output returns the formatting layer, creating it lazily. |
| 146 | +func (s *IOStreams) Output() *printer.Output { |
| 147 | + if s.output == nil { |
| 148 | + s.output = printer.NewOutputFrom(s.Out, s.ErrOut, s.outTTY) |
| 149 | + } |
| 150 | + return s.output |
| 151 | +} |
| 152 | + |
| 153 | +// Prompter returns the prompt layer, creating it lazily. |
| 154 | +func (s *IOStreams) Prompter() prompt.Prompter { |
| 155 | + if s.prompter == nil { |
| 156 | + s.prompter = prompt.New() |
| 157 | + } |
| 158 | + return s.prompter |
| 159 | +} |
| 160 | + |
| 161 | +func isTTYWriter(f *os.File) bool { |
| 162 | + return isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd()) |
| 163 | +} |
0 commit comments