diff --git a/README.md b/README.md index ad1e703..282528e 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/main.go b/main.go index c9c0e8d..a1bef56 100644 --- a/main.go +++ b/main.go @@ -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. "+ @@ -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 != "" {