fix: address PR review — unexport StripAnsi and simplify error path

Make StripAnsi unexported since it is only used within the exec package.
Extract repeated StripAnsi(stderr.String()) call into a local variable
for clarity.

Signed-off-by: Moritz Röseler <moritz.roeseler@gmail.com>
This commit is contained in:
Moritz Röseler 2026-04-09 18:25:17 +02:00
parent 39bc94e9ac
commit 9d5eb0f9c0
2 changed files with 7 additions and 6 deletions

View file

@ -32,8 +32,8 @@ var (
ansiEscapeRegex = regexp.MustCompile(`\x1b\[[0-9;?]*[a-zA-Z]`)
)
// StripAnsi removes ANSI escape sequences from s.
func StripAnsi(s string) string {
// stripAnsi removes ANSI escape sequences from s.
func stripAnsi(s string) string {
return ansiEscapeRegex.ReplaceAllString(s, "")
}
@ -273,12 +273,13 @@ func RunCommandExt(cmd *exec.Cmd, opts CmdOpts) (string, error) {
return strings.TrimSuffix(output, "\n"), err
case err := <-done:
if err != nil {
output := StripAnsi(stdout.String())
stderrStr := stripAnsi(stderr.String())
output := stripAnsi(stdout.String())
if opts.CaptureStderr {
output += StripAnsi(stderr.String())
output += stderrStr
}
logCtx.WithFields(logrus.Fields{"duration": time.Since(start)}).Debug(redactor(output))
err := newCmdError(redactor(args), errors.New(redactor(err.Error())), strings.TrimSpace(redactor(StripAnsi(stderr.String()))))
err := newCmdError(redactor(args), errors.New(redactor(err.Error())), strings.TrimSpace(redactor(stderrStr)))
if !opts.SkipErrorLogging {
logCtx.Error(err.Error())
}

View file

@ -221,7 +221,7 @@ func TestStripAnsi(t *testing.T) {
"\x1b[KNo newline clear": "No newline clear",
}
for in, want := range cases {
assert.Equal(t, want, StripAnsi(in))
assert.Equal(t, want, stripAnsi(in))
}
}