From 61fb9a45256bd4ce37f7d7d2f088443ca6d39c1c Mon Sep 17 00:00:00 2001 From: Takatoshi MATSUO Date: Thu, 25 Aug 2022 21:18:17 +0900 Subject: [PATCH] add WEBHOOK_OUTPUT_STDOUT WEBHOOK_OUTPUT_STDERR Env --- README.md | 4 ++++ webhook.go | 45 ++++++++++++++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 112bb0f..ebe2a40 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ It receives webhook and call specified command. * WEBHOOK_CMD (required) * command +* WEBHOOK_OUTPUT_STDOUT (default:false) + * Output stdout of command to client +* WEBHOOK_OUTPUT_STDERR (default:false) + * Output stderr of command to client if command does not exit with 0 * WEBHOOK_PORT (default:22999) * Listen Port * WEBHOOK_BIND (default:0.0.0.0) diff --git a/webhook.go b/webhook.go index 0b89b6b..49326c3 100644 --- a/webhook.go +++ b/webhook.go @@ -25,18 +25,20 @@ import ( ) var goenv struct { - Cmd string `required:"true"` - Port int `default:"22999"` - Bind string `default:"0.0.0.0"` - Path string `default:"/"` - Debug bool `default:"false"` - Log_prefix string `default:"[webhook]"` - No_alog bool `default:"false"` - Timeout int `default:"300"` - Workdir string `default:"/tmp"` - Tls bool `default:"false"` - Server_crt string `default:""` - Server_key string `default:""` + Cmd string `required:"true"` + Output_stdout bool `default:"false"` + Output_stderr bool `default:"false"` + Port int `default:"22999"` + Bind string `default:"0.0.0.0"` + Path string `default:"/"` + Debug bool `default:"false"` + Log_prefix string `default:"[webhook]"` + No_alog bool `default:"false"` + Timeout int `default:"300"` + Workdir string `default:"/tmp"` + Tls bool `default:"false"` + Server_crt string `default:""` + Server_key string `default:""` } var log_info *log.Logger @@ -101,6 +103,8 @@ func handleEnv() { } log_info.Printf("Command is %s\n", goenv.Cmd) + log_info.Printf("Output stdout is %t\n", goenv.Output_stdout) + log_info.Printf("Output stderr is %t\n", goenv.Output_stderr) log_info.Printf("Workdir is %s\n", goenv.Workdir) if goenv.Tls == false { log_info.Printf("Listening on http://%s:%d%s\n", goenv.Bind, goenv.Port, goenv.Path) @@ -154,15 +158,30 @@ func handleReq(w http.ResponseWriter, r *http.Request) { "WEBHOOK_PATH="+path, ) cmd.Dir = goenv.Workdir + + var stdout bytes.Buffer + var stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr err := cmd.Run() if err != nil { log_err.Printf("Internal Server Error. %s\n", err) w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Internal Server Error\n") + if goenv.Output_stderr { + fmt.Fprintf(w, "\n############### stderr ###############\n%s\n", stderr.String()) + } + if goenv.Output_stdout { + fmt.Fprintf(w, "\n############### stdout ###############\n%s\n", stdout.String()) + } } else { w.WriteHeader(http.StatusOK) - fmt.Fprintf(w, "OK\n") + if goenv.Output_stdout { + fmt.Fprintf(w, "%s", stdout.String()) + } else { + fmt.Fprintf(w, "OK\n") + } } default: log_access.Printf("%s %s Method Not Allowed\n", alog_format, r.Method)