mirror of
https://github.com/adnanh/webhook.git
synced 2025-08-01 15:30:30 +00:00
Merge 16fed53a7b
into a617b1a6ac
This commit is contained in:
commit
73e9043623
1 changed files with 43 additions and 3 deletions
46
webhook.go
46
webhook.go
|
@ -1,9 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -12,6 +15,7 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/adnanh/webhook/hook"
|
"github.com/adnanh/webhook/hook"
|
||||||
|
@ -404,10 +408,46 @@ func handleHook(h *hook.Hook, rid string, headers, query, payload *map[string]in
|
||||||
|
|
||||||
log.Printf("[%s] executing %s (%s) with arguments %q and environment %s using %s as cwd\n", rid, h.ExecuteCommand, cmd.Path, cmd.Args, envs, cmd.Dir)
|
log.Printf("[%s] executing %s (%s) with arguments %q and environment %s using %s as cwd\n", rid, h.ExecuteCommand, cmd.Path, cmd.Args, envs, cmd.Dir)
|
||||||
|
|
||||||
out, err := cmd.CombinedOutput()
|
stdout, err := cmd.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
stderr, err := cmd.StderrPipe()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("[%s] command output: %s\n", rid, out)
|
var out bytes.Buffer
|
||||||
|
|
||||||
|
teeout := io.TeeReader(stdout, &out)
|
||||||
|
teeerr := io.TeeReader(stderr, &out)
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(2)
|
||||||
|
|
||||||
|
logOut := func(out io.Reader, rid string) {
|
||||||
|
in := bufio.NewScanner(out)
|
||||||
|
|
||||||
|
for in.Scan() {
|
||||||
|
outln := in.Text()
|
||||||
|
log.Printf("[%s] command output: %s\n", rid, outln)
|
||||||
|
}
|
||||||
|
if err := in.Err(); err != nil {
|
||||||
|
log.Printf("[%s] error occurred: %+v\n", rid, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
go logOut(teeout, rid)
|
||||||
|
go logOut(teeerr, rid)
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
err = cmd.Wait()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[%s] error occurred: %+v\n", rid, err)
|
log.Printf("[%s] error occurred: %+v\n", rid, err)
|
||||||
}
|
}
|
||||||
|
@ -424,7 +464,7 @@ func handleHook(h *hook.Hook, rid string, headers, query, payload *map[string]in
|
||||||
|
|
||||||
log.Printf("[%s] finished handling %s\n", rid, h.ID)
|
log.Printf("[%s] finished handling %s\n", rid, h.ID)
|
||||||
|
|
||||||
return string(out), err
|
return out.String(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeHttpResponseCode(w http.ResponseWriter, rid string, hookId string, responseCode int) {
|
func writeHttpResponseCode(w http.ResponseWriter, rid string, hookId string, responseCode int) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue