| 1 | package legatus |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strings" |
| 6 | |
| 7 | "go.bigb.es/auxilia/culpa" |
| 8 | ) |
| 9 | |
| 10 | // Output runs cmd over conn, requires a clean run (exit 0), and returns the |
| 11 | // trimmed stdout. It is the "exec and capture" convenience over Conn.Exec: a |
| 12 | // non-zero exit is a hard error carrying the trimmed combined output, with no |
| 13 | // silent fallback to a partial result. For a probe where a non-zero exit is a |
| 14 | // signal rather than a failure (e.g. `test -e`), call Exec directly and inspect |
| 15 | // ExecResult.ExitCode. |
| 16 | func Output(ctx context.Context, conn Conn, cmd Cmd) (string, error) { |
| 17 | res, err := conn.Exec(ctx, cmd) |
| 18 | if err != nil { |
| 19 | return "", culpa.WithCode(culpa.Wrapf(err, "output: exec %s", cmd.Path), "OUTPUT") |
| 20 | } |
| 21 | if res.ExitCode != 0 { |
| 22 | return "", culpa.WithCode( |
| 23 | culpa.Errorf("output: %s: exit %d: %s", |
| 24 | cmd.Path, res.ExitCode, strings.TrimSpace(string(res.Combined()))), |
| 25 | "OUTPUT") |
| 26 | } |
| 27 | return strings.TrimSpace(string(res.Stdout)), nil |
| 28 | } |
| 29 | |