Skip to content

Commit

Permalink
In addition to StdOut/Err check the outfile for TTYness
Browse files Browse the repository at this point in the history
  • Loading branch information
ribasushi committed Jul 8, 2021
1 parent 2c2975b commit 858b2a0
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,13 @@ func configFromEnv() Config {
}
}

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

Expand All @@ -375,3 +379,12 @@ func configFromEnv() Config {
func isTerm(f *os.File) bool {
return isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd())
}

func pathIsTerm(p string) bool {
// !!!no!!! O_CREAT, if we fail - we fail
f, err := os.OpenFile(p, os.O_WRONLY, 0)
if f != nil {
defer f.Close() // nolint:errcheck
}
return err == nil && isTerm(f)
}

0 comments on commit 858b2a0

Please sign in to comment.