Skip to content

Commit 858b2a0

Browse files
committed
In addition to StdOut/Err check the outfile for TTYness
1 parent 2c2975b commit 858b2a0

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

setup.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,13 @@ func configFromEnv() Config {
350350
}
351351
}
352352

353+
// Check that neither of the requested Std* nor the file are TTYs
354+
// At this stage (configFromEnv) we do not have a uniform list to examine yet
353355
if noExplicitFormat &&
354-
(!cfg.Stdout || !isTerm(os.Stdout)) &&
355-
(!cfg.Stderr || !isTerm(os.Stderr)) {
356+
!(cfg.Stdout && isTerm(os.Stdout)) &&
357+
!(cfg.Stderr && isTerm(os.Stderr)) &&
358+
// check this last: expensive
359+
!(cfg.File != "" && pathIsTerm(cfg.File)) {
356360
cfg.Format = PlaintextOutput
357361
}
358362

@@ -375,3 +379,12 @@ func configFromEnv() Config {
375379
func isTerm(f *os.File) bool {
376380
return isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd())
377381
}
382+
383+
func pathIsTerm(p string) bool {
384+
// !!!no!!! O_CREAT, if we fail - we fail
385+
f, err := os.OpenFile(p, os.O_WRONLY, 0)
386+
if f != nil {
387+
defer f.Close() // nolint:errcheck
388+
}
389+
return err == nil && isTerm(f)
390+
}

0 commit comments

Comments
 (0)