cmd/age: decide to buffer output based on stdin source

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 <hi@filippo.io>
This commit is contained in:
codesoap
2021-01-06 17:28:14 +01:00
committed by Filippo Valsorda
parent 4a5a042583
commit 902a3d4e6b

View File

@@ -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.
@@ -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 {