From a97bc8c31b455b969edf67c642ce357fff761397 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Fri, 5 Feb 2021 01:08:55 +0100 Subject: [PATCH] chezmoi2: Don't output prompts when --no-tty is set --- chezmoi2/cmd/config.go | 4 +--- chezmoi2/cmd/terminal.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/chezmoi2/cmd/config.go b/chezmoi2/cmd/config.go index 68bb54e1558..d64ea4fe5a8 100644 --- a/chezmoi2/cmd/config.go +++ b/chezmoi2/cmd/config.go @@ -1311,10 +1311,8 @@ func (c *Config) validateData() error { } func (c *Config) withTerminal(prompt string, f func(terminal) error) error { - // FIXME this seems to wrap lines at 80 characters by default - if c.noTTY { - return f(newDumbTerminal(c.stdin, c.stdout, prompt)) + return f(newNullTerminal(c.stdin)) } if stdinFile, ok := c.stdin.(*os.File); ok && term.IsTerminal(int(stdinFile.Fd())) { diff --git a/chezmoi2/cmd/terminal.go b/chezmoi2/cmd/terminal.go index 07dce846160..a7fb4005715 100644 --- a/chezmoi2/cmd/terminal.go +++ b/chezmoi2/cmd/terminal.go @@ -21,6 +21,11 @@ type dumbTerminal struct { prompt []byte } +// A nullTerminal does not print any prompts and only reads. +type nullTerminal struct { + reader *bufio.Reader +} + // newDumbTerminal returns a new dumbTerminal. func newDumbTerminal(reader io.Reader, writer io.Writer, prompt string) *dumbTerminal { return &dumbTerminal{ @@ -56,6 +61,31 @@ func (t *dumbTerminal) ReadPassword(prompt string) (string, error) { return strings.TrimSuffix(password, "\n"), nil } +// SetPrompt implements terminal.SetPrompt. func (t *dumbTerminal) SetPrompt(prompt string) { t.prompt = []byte(prompt) } + +// newNullTerminal returns a new nullTerminal. +func newNullTerminal(r io.Reader) *nullTerminal { + return &nullTerminal{ + reader: bufio.NewReader(r), + } +} + +// ReadLine implements terminal.ReadLine. +func (t *nullTerminal) ReadLine() (string, error) { + line, err := t.reader.ReadString('\n') + if err != nil { + return "", err + } + return strings.TrimSuffix(line, "\n"), nil +} + +// ReadPassword implements terminal.ReadPassword. +func (t *nullTerminal) ReadPassword(prompt string) (string, error) { + return t.ReadLine() +} + +// SetPrompt implements terminal.SetPrompt. +func (t *nullTerminal) SetPrompt(prompt string) {}