Feat: add --strip-args-values-on-exit-error (#887)

* Add --strip-args-values-on-exit-error

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
Jan-Otto Kröpke
2023-06-07 14:39:38 +08:00
committed by GitHub
parent 8249833d14
commit f7b9de6ac1
10 changed files with 141 additions and 60 deletions
+4 -1
View File
@@ -5,13 +5,16 @@ import (
"strings"
)
func newExitError(path string, args []string, exitStatus int, err error, stderr, combined string) ExitError {
func newExitError(path string, args []string, exitStatus int, err error, stderr, combined string, stripArgsValuesOnExitError bool) ExitError {
var out string
out += fmt.Sprintf("PATH:\n%s", Indent(path, " "))
out += "\n\nARGS:"
for i, a := range args {
if i > 0 && strings.HasPrefix(args[i-1], "--set") && stripArgsValuesOnExitError {
a = "*** STRIP ***"
}
out += fmt.Sprintf("\n%s", Indent(fmt.Sprintf("%d: %s (%d bytes)", i, a, len(a)), " "))
}
+62
View File
@@ -0,0 +1,62 @@
package helmexec
import (
"errors"
"testing"
)
func TestNewExitError(t *testing.T) {
for _, tt := range []struct {
name string
stripArgsValuesOnExitError bool
want string
}{
{
name: "newExitError with stripArgsValuesOnExitError false",
stripArgsValuesOnExitError: false,
want: `command "helm" exited with non-zero status:
PATH:
helm
ARGS:
0: --set (5 bytes)
1: a=b (3 bytes)
2: --set-string (12 bytes)
3: a=b (3 bytes)
ERROR:
test
EXIT STATUS
1`,
},
{
name: "newExitError with stripArgsValuesOnExitError true",
stripArgsValuesOnExitError: true,
want: `command "helm" exited with non-zero status:
PATH:
helm
ARGS:
0: --set (5 bytes)
1: *** STRIP *** (13 bytes)
2: --set-string (12 bytes)
3: *** STRIP *** (13 bytes)
ERROR:
test
EXIT STATUS
1`,
},
} {
t.Run(tt.name, func(t *testing.T) {
exitError := newExitError("helm", []string{"--set", "a=b", "--set-string", "a=b"}, 1, errors.New("test"), "", "", tt.stripArgsValuesOnExitError)
if want, have := tt.want, exitError.Error(); want != have {
t.Errorf("want %q, have %q", want, have)
}
})
}
}
+9 -7
View File
@@ -28,6 +28,8 @@ type Runner interface {
type ShellRunner struct {
Dir string
StripArgsValuesOnExitError bool
Logger *zap.SugaredLogger
Ctx context.Context
}
@@ -39,11 +41,11 @@ func (shell ShellRunner) Execute(cmd string, args []string, env map[string]strin
preparedCmd.Env = mergeEnv(os.Environ(), env)
if !enableLiveOutput {
return Output(shell.Ctx, preparedCmd, &logWriterGenerator{
return Output(shell.Ctx, preparedCmd, shell.StripArgsValuesOnExitError, &logWriterGenerator{
log: shell.Logger,
})
} else {
return LiveOutput(shell.Ctx, preparedCmd, os.Stdout)
return LiveOutput(shell.Ctx, preparedCmd, shell.StripArgsValuesOnExitError, os.Stdout)
}
}
@@ -53,12 +55,12 @@ func (shell ShellRunner) ExecuteStdIn(cmd string, args []string, env map[string]
preparedCmd.Dir = shell.Dir
preparedCmd.Env = mergeEnv(os.Environ(), env)
preparedCmd.Stdin = stdin
return Output(shell.Ctx, preparedCmd, &logWriterGenerator{
return Output(shell.Ctx, preparedCmd, shell.StripArgsValuesOnExitError, &logWriterGenerator{
log: shell.Logger,
})
}
func Output(ctx context.Context, c *exec.Cmd, logWriterGenerators ...*logWriterGenerator) ([]byte, error) {
func Output(ctx context.Context, c *exec.Cmd, stripArgsValuesOnExitError bool, logWriterGenerators ...*logWriterGenerator) ([]byte, error) {
if c.Stdout != nil {
return nil, errors.New("exec: Stdout already set")
}
@@ -114,7 +116,7 @@ func Output(ctx context.Context, c *exec.Cmd, logWriterGenerators ...*logWriterG
// so that helmfile could return its own exit code accordingly
waitStatus := ee.Sys().(syscall.WaitStatus)
exitStatus := waitStatus.ExitStatus()
err = newExitError(c.Path, c.Args, exitStatus, ee, stderr.String(), combined.String())
err = newExitError(c.Path, c.Args, exitStatus, ee, stderr.String(), combined.String(), stripArgsValuesOnExitError)
default:
panic(fmt.Sprintf("unexpected error: %v", err))
}
@@ -123,7 +125,7 @@ func Output(ctx context.Context, c *exec.Cmd, logWriterGenerators ...*logWriterG
return stdout.Bytes(), err
}
func LiveOutput(ctx context.Context, c *exec.Cmd, stdout io.Writer) ([]byte, error) {
func LiveOutput(ctx context.Context, c *exec.Cmd, stripArgsValuesOnExitError bool, stdout io.Writer) ([]byte, error) {
reader, writer := io.Pipe()
scannerStopped := make(chan struct{})
@@ -162,7 +164,7 @@ func LiveOutput(ctx context.Context, c *exec.Cmd, stdout io.Writer) ([]byte, err
// so that helmfile could return its own exit code accordingly
waitStatus := ee.Sys().(syscall.WaitStatus)
exitStatus := waitStatus.ExitStatus()
err = newExitError(c.Path, c.Args, exitStatus, ee, "", "")
err = newExitError(c.Path, c.Args, exitStatus, ee, "", "", stripArgsValuesOnExitError)
default:
panic(fmt.Sprintf("unexpected error: %v", err))
}
+1 -1
View File
@@ -74,7 +74,7 @@ Usage: helm template [NAME] [CHART] [flags]
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
got, err := LiveOutput(context.Background(), tt.cmd, w)
got, err := LiveOutput(context.Background(), tt.cmd, false, w)
if (err != nil) != tt.wantErr {
t.Errorf("LiveOutput() error = %v, wantErr %v", err, tt.wantErr)
return