Skip to content

Commit

Permalink
cmd/age: decide to buffer output based on stdin source
Browse files Browse the repository at this point in the history
Buffering only when the armorFlag is set disregards use cases where data
from a tty stdin is decrypted or where binary data goes to a tty stdout.

Buffering is only necessary if stdin is a tty and stdout is a tty.

Co-authored-by: Filippo Valsorda <[email protected]>
  • Loading branch information
codesoap and FiloSottile committed Jan 7, 2021
1 parent 4a5a042 commit 902a3d4
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions cmd/age/age.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,6 @@ func main() {
defer f.Close()
out = f
} else if terminal.IsTerminal(int(os.Stdout.Fd())) {
if armorFlag {
// If the output will go to a TTY, and it will be armored, buffer it
// up so it doesn't get in the way of typing the input.
buf := &bytes.Buffer{}
defer func() { io.Copy(os.Stdout, buf) }()
out = buf
}
if name != "-" {
if decryptFlag {
// TODO: buffer the output and check it's printable.
Expand All @@ -193,6 +186,13 @@ func main() {
`Did you mean to use -a/--armor? Force with "-o -".`)
}
}
if in == os.Stdin && terminal.IsTerminal(int(os.Stdin.Fd())) {
// If the input comes from a TTY and output will go to a TTY,
// buffer it up so it doesn't get in the way of typing the input.
buf := &bytes.Buffer{}
defer func() { io.Copy(os.Stdout, buf) }()
out = buf
}
}

switch {
Expand Down

0 comments on commit 902a3d4

Please sign in to comment.