chore(): add hook_id example and doc

This commit is contained in:
Nicolas Carlier 2021-05-07 07:29:53 +00:00
parent d3777a7fcd
commit 25fe46c731
5 changed files with 26 additions and 9 deletions

View File

@ -150,6 +150,7 @@ The script:
```bash
#!/bin/bash
echo "Hook information: name=$hook_name, id=$hook_id"
echo "Query parameter: foo=$foo"
echo "Header parameter: user-agent=$user_agent"
echo "Script parameters: $1"
@ -159,6 +160,7 @@ The result:
```bash
$ curl --data @test.json http://localhost:8080/echo?foo=bar
Hook information: name=echo, id=1
Query parameter: foo=bar
Header parameter: user-agent=curl/7.52.1
Script parameter: {"foo": "bar"}

View File

@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path"
"strconv"
"strings"
"sync"
"sync/atomic"
@ -62,6 +63,14 @@ func NewWorkRequest(name, script, payload, output string, args []string, timeout
return w
}
// Meta return work request meta
func (wr *WorkRequest) Meta() []string {
return []string{
"hook_id=" + strconv.FormatUint(wr.ID, 10),
"hook_name=" + wr.Name,
}
}
// Terminate set work request as terminated
func (wr *WorkRequest) Terminate(err error) error {
wr.mutex.Lock()

View File

@ -2,7 +2,6 @@ package worker
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
@ -36,11 +35,12 @@ func Run(work *model.WorkRequest) error {
return work.Terminate(err)
}
// Exec script with args...
// Exec script with parameter...
cmd := exec.Command(binary, work.Payload)
// with env variables...
workEnv := append(os.Environ(), fmt.Sprintf("hook_id=%d", work.ID))
cmd.Env = append(workEnv, work.Args...)
// with env variables and hook arguments...
cmd.Env = append(os.Environ(), work.Args...)
// and hook meta...
cmd.Env = append(cmd.Env, work.Meta()...)
// using a process group...
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}

View File

@ -2,7 +2,9 @@
# Usage: http POST :8080/echo msg==hello foo=bar
echo "Echo script:"
echo "This is a simple echo hook."
echo "Hook information: name=$hook_name, id=$hook_id"
echo "Command result: hostname=`hostname`"

View File

@ -4,12 +4,16 @@ const os = require('os')
// Usage: http POST :8080/examples/echo.js msg==hello foo=bar
console.log("Echo script:")
console.log("This is a simple echo hook using NodeJS.")
const { hook_name, hook_id , user_agent, msg} = process.env
console.log(`Hook information: name=${hook_name}, id=${hook_id}`)
console.log("Hostname=", os.hostname())
console.log("User-Agent=", process.env["user_agent"])
console.log(`User-Agent=${user_agent}`)
console.log("msg=", process.env["msg"])
console.log(`msg=${msg}`)
console.log("body=", process.argv)