Skip to content

Commit

Permalink
Merge pull request #1023 from twpayne/chezmoi2-fixes
Browse files Browse the repository at this point in the history
chezmoi2: Don't output prompts when --no-tty is set
  • Loading branch information
twpayne committed Feb 5, 2021
2 parents bee76ce + a97bc8c commit 2e5a76c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 1 addition & 3 deletions chezmoi2/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down
30 changes: 30 additions & 0 deletions chezmoi2/cmd/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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) {}

0 comments on commit 2e5a76c

Please sign in to comment.