Skip to content

Commit

Permalink
add -print-stderr flag to print to stderr; log delivery error if prin…
Browse files Browse the repository at this point in the history
…ting to stdout/err fails
  • Loading branch information
cdzombak committed Dec 10, 2023
1 parent cef9b21 commit 7cc474b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ If you plan to use the `RUNNER_OUTFD_PID` and `RUNNER_OUTFD_STD[OUT|ERR]` variab
- Can also be set by the `RUNNER_LOG_DIR` environment variable; this flag overrides the environment variable.
- `-print-if-match value`: Print/mail output if the given (**case-sensitive**) string appears in the program's output, even if it was a healthy exit. May be specified multiple times.
- `-print-if-not-match value`: Print/mail output if the given (**case-sensitive**) string does not appear in the program's output, even if it was a healthy exit. May be specified multiple times.
- `-print-stderr`: Print output to stderr instead of stdout (if this flag is not given, output is printed to stdout).
- `-retries int`: If the command fails, retry it this many times. (default: `0`)
- `-retry-delay int`: If the command fails, wait this many seconds before retrying. (default: `0`)
- `-version`: Print version and exit.
Expand Down
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func main() {
flag.Var(&printIfNotMatch, "print-if-not-match", "Print/mail output if the given (case-sensitive) string does not appear in the program's output, even if it was a healthy exit. "+
"May be specified multiple times.")
alwaysPrint := flag.Bool("always-print", false, "Always print/mail the program's output, sidestepping exit code and -print-if[-not]-match checks.")
printToStderr := flag.Bool("print-stderr", false, "Print output to stderr instead of stdout (if this flag is not given, output is printed to stdout).")
jobName := flag.String("job-name", "", "Job name used in failure notifications and log file name. (default: program name, without path)")
hideEnv := flag.Bool("hide-env", false, "Hide the process's environment, which is normally printed & logged as part of the output.")
logDir := flag.String("log-dir", "", "The directory to write run logs to. "+
Expand Down Expand Up @@ -420,8 +421,16 @@ func main() {
var deliveryErrs []error

if runOut.shouldPrint {
fmt.Print(runOut.output)
deliveryErrs = executeDeliveries(deliveryCfg, runOut)

to := os.Stdout
if *printToStderr {
to = os.Stderr
}
_, err := fmt.Fprint(to, runOut.output)
if err != nil {
deliveryErrs = append(deliveryErrs, fmt.Errorf("failed to print output: %w", err))
}
}

if runOut.succeeded && *successNotifyURL != "" {
Expand Down

0 comments on commit 7cc474b

Please sign in to comment.