Result¶
The Result struct contains the outcome of running a command.
Structure¶
type Result struct {
StdOut string // Captured standard output
StdErr string // Captured standard error
Combined string // Interleaved stdout and stderr
Err error // Error if command failed
Attempts int // Number of attempts made
ExitCode int // Exit code of the command
}
Fields¶
StdOut¶
Captured standard output.
Empty if WithCollectAllOutput(false) was set.
StdErr¶
Captured standard error.
result := cmder.New("ls", "/nonexistent").Run(ctx)
fmt.Println(result.StdErr) // "ls: /nonexistent: No such file..."
Empty if WithCollectAllOutput(false) was set.
Combined¶
Interleaved stdout and stderr in order received.
result := cmder.New("sh", "-c", "echo out; echo err >&2; echo out2").Run(ctx)
fmt.Println(result.Combined)
// out
// err
// out2
Useful when order matters.
Err¶
Error if the command failed.
result := cmder.New("false").Run(ctx) // 'false' always exits 1
if result.Err != nil {
fmt.Printf("Command failed: %v\n", result.Err)
}
Common error types:
- context.DeadlineExceeded - Timeout
- Wrapped exec errors - Command not found, etc.
Attempts¶
Number of execution attempts.
result := cmder.New("flaky").
WithRetries(5).
WithRetryFilter(func(err error, isTimeout bool) bool { return true }).
Run(ctx)
fmt.Printf("Took %d attempts\n", result.Attempts)
// 1 = success on first try
// 2-6 = needed retries
ExitCode¶
The command's exit code.
Values:
- 0 - Success
- 1-255 - Command-defined failure code
- -1 - Process didn't start or state unavailable
Usage Patterns¶
Check Success¶
result := cmder.New("make", "test").Run(ctx)
if result.Err != nil {
log.Fatalf("Tests failed: %v\n%s", result.Err, result.Combined)
}
Handle Specific Exit Codes¶
result := cmder.New("grep", "pattern", "file.txt").Run(ctx)
switch result.ExitCode {
case 0:
fmt.Printf("Found: %s", result.StdOut)
case 1:
fmt.Println("Pattern not found")
case 2:
fmt.Printf("Error: %s", result.StdErr)
}
Log Failures with Context¶
result := cmder.New("deploy.sh").
WithRetries(3).
Run(ctx)
if result.Err != nil {
log.Printf("Deploy failed after %d attempts\n", result.Attempts)
log.Printf("Exit code: %d\n", result.ExitCode)
log.Printf("Stdout:\n%s", result.StdOut)
log.Printf("Stderr:\n%s", result.StdErr)
}