Apply kati output rewriting to dumpvars

This way we strip out ANSI codes when using dumb terminals. It's likely
overkill to use katiRewriteOutput, but we should rarely see any output
on stderr while dumping variables. This also lets us turn on kati_stats.

Bug: 71729611
Test: lunch missing-eng             (colored error)
Test: lunch missing-eng 2>&1 | cat  (non-colored)
Test: TERM=dumb lunch missing-eng   (non-colored)
Change-Id: Ic63fd42d82a4a64e5c68aecd9ae0f242a0d703f1
This commit is contained in:
Dan Willemsen
2018-01-09 02:09:52 -08:00
parent e6f4ee2061
commit 0c51851f08
3 changed files with 19 additions and 6 deletions

View File

@@ -103,6 +103,13 @@ func (c ContextImpl) IsTerminal() bool {
return false return false
} }
func (c ContextImpl) IsErrTerminal() bool {
if term, ok := os.LookupEnv("TERM"); ok {
return term != "dumb" && isTerminal(c.Stderr())
}
return false
}
func (c ContextImpl) TermWidth() (int, bool) { func (c ContextImpl) TermWidth() (int, bool) {
return termWidth(c.Stdout()) return termWidth(c.Stdout())
} }

View File

@@ -42,6 +42,7 @@ func dumpMakeVars(ctx Context, config Config, goals, vars []string, write_soong_
config.PrebuiltBuildTool("ckati"), config.PrebuiltBuildTool("ckati"),
"-f", "build/make/core/config.mk", "-f", "build/make/core/config.mk",
"--color_warnings", "--color_warnings",
"--kati_stats",
"dump-many-vars", "dump-many-vars",
"MAKECMDGOALS="+strings.Join(goals, " ")) "MAKECMDGOALS="+strings.Join(goals, " "))
cmd.Environment.Set("CALLED_FROM_SETUP", "true") cmd.Environment.Set("CALLED_FROM_SETUP", "true")
@@ -51,15 +52,19 @@ func dumpMakeVars(ctx Context, config Config, goals, vars []string, write_soong_
} }
cmd.Environment.Set("DUMP_MANY_VARS", strings.Join(vars, " ")) cmd.Environment.Set("DUMP_MANY_VARS", strings.Join(vars, " "))
cmd.Sandbox = dumpvarsSandbox cmd.Sandbox = dumpvarsSandbox
// TODO: error out when Stderr contains any content output := bytes.Buffer{}
cmd.Stderr = ctx.Stderr() cmd.Stdout = &output
output, err := cmd.Output() pipe, err := cmd.StderrPipe()
if err != nil { if err != nil {
return nil, err ctx.Fatalln("Error getting output pipe for ckati:", err)
} }
cmd.StartOrFatal()
// TODO: error out when Stderr contains any content
katiRewriteOutput(ctx, pipe)
cmd.WaitOrFatal()
ret := make(map[string]string, len(vars)) ret := make(map[string]string, len(vars))
for _, line := range strings.Split(string(output), "\n") { for _, line := range strings.Split(output.String(), "\n") {
if len(line) == 0 { if len(line) == 0 {
continue continue
} }

View File

@@ -115,6 +115,7 @@ var katiLogRe = regexp.MustCompile(`^\*kati\*: `)
func katiRewriteOutput(ctx Context, pipe io.ReadCloser) { func katiRewriteOutput(ctx Context, pipe io.ReadCloser) {
haveBlankLine := true haveBlankLine := true
smartTerminal := ctx.IsTerminal() smartTerminal := ctx.IsTerminal()
errSmartTerminal := ctx.IsErrTerminal()
scanner := bufio.NewScanner(pipe) scanner := bufio.NewScanner(pipe)
for scanner.Scan() { for scanner.Scan() {
@@ -155,7 +156,7 @@ func katiRewriteOutput(ctx Context, pipe io.ReadCloser) {
// that message instead of overwriting it. // that message instead of overwriting it.
fmt.Fprintln(ctx.Stdout()) fmt.Fprintln(ctx.Stdout())
haveBlankLine = true haveBlankLine = true
} else if !smartTerminal { } else if !errSmartTerminal {
// Most editors display these as garbage, so strip them out. // Most editors display these as garbage, so strip them out.
line = string(stripAnsiEscapes([]byte(line))) line = string(stripAnsiEscapes([]byte(line)))
} }