Commit 7ff668de
7ff668dea0f9Merged
Vigilans <[email protected]> ยท 11 months ago ยท 1 file ยท ~1 modified
Description
feat: print stdout and stderr on command error in exec template function
Signed-off-by: Vigilans [email protected]
Details
- Commit
- 7ff668dea0f9
- Parent
- 620c716fc201
pkg/template/extra.gomodifiedView file
@@ -185,8 +185,28 @@
185185
186186
bs, err := cmd.Output()187187
if err != nil {188-
wg.ErrChan() <- fmt.Errorf("failed to get command output: %w", err)189-
188+
var exitErr *exec.ExitError189+
if errors.As(err, &exitErr) {190+
// Combine stdout and stderr, only include non-empty parts191+
var combinedOutput string192+
stdout := strings.TrimSpace(string(bs))193+
stderr := strings.TrimSpace(string(exitErr.Stderr))194+
switch {195+
case stdout != "" && stderr != "":196+
combinedOutput = stdout + "\n" + stderr197+
case stderr != "":198+
combinedOutput = stderr199+
case stdout != "":200+
combinedOutput = stdout201+
}202+
if combinedOutput != "" {203+
wg.ErrChan() <- fmt.Errorf("command failed: %w: %s", err, combinedOutput)204+
} else {205+
wg.ErrChan() <- fmt.Errorf("command failed: %w", err)206+
}207+
} else {208+
wg.ErrChan() <- fmt.Errorf("failed to run command: %w", err)209+
}190210
return191211
}192212