From c5fef8e42d536f70cf724efd5ff83ddf9d578f52 Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Sat, 28 Oct 2023 21:48:54 +0100 Subject: [PATCH] AppendHeaders --- config.json | 3 ++- config/command.go | 13 ++++++++++++- example.sh | 2 +- main.go | 6 +++--- readme.md | 3 ++- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/config.json b/config.json index 60301cf..dc3b532 100644 --- a/config.json +++ b/config.json @@ -4,7 +4,8 @@ "test": { "Script": { "Program": "./example.sh", - "AppendPayload": true + "AppendPayload": true, + "AppendHeaders": true }, "DisableSignatureVerification": true, "Tests": [ diff --git a/config/command.go b/config/command.go index 32311f8..b9f529b 100644 --- a/config/command.go +++ b/config/command.go @@ -2,22 +2,33 @@ package config import ( "fmt" + "net/http" "os/exec" + "strings" ) type Command struct { Program string Arguments []string AppendPayload bool + AppendHeaders bool } -func (c Command) Execute(payload string) ([]byte, error) { +func (c Command) Execute(payload string, header http.Header) ([]byte, error) { arguments := make([]string, 0) copy(c.Arguments, arguments) + if c.AppendPayload { arguments = append(arguments, payload) } + if c.AppendHeaders { + var header_builder strings.Builder; + header.Write(&header_builder); + + arguments = append(arguments, header_builder.String()) + } + return exec.Command(c.Program, arguments...).Output() } diff --git a/example.sh b/example.sh index 2216ed7..8ff8543 100755 --- a/example.sh +++ b/example.sh @@ -1,3 +1,3 @@ #!/usr/bin/bash date >> test_output -echo "$1" >> test_output +echo "$1" "$2" >> test_output diff --git a/main.go b/main.go index c30b2ac..1e81aca 100644 --- a/main.go +++ b/main.go @@ -84,15 +84,15 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) { } // Run tests and script as goroutine to prevent timing out - go func(){ + go func() { // Run tests, immediately stop if one fails for _, test := range service.Tests { - if _, err := test.Execute(payload); err != nil { + if _, err := test.Execute(payload, r.Header); err != nil { fmt.Printf("Test failed(%v) for service %v\n", test, serviceName) return } } - stdout, err := service.Script.Execute(payload) + stdout, err := service.Script.Execute(payload, r.Header) fmt.Println(string(stdout)) if err != nil { fmt.Println(err.Error()) diff --git a/readme.md b/readme.md index f45063c..8364b23 100644 --- a/readme.md +++ b/readme.md @@ -41,7 +41,8 @@ You can disable signature verification for all services by setting environment v gohookr doesn't care what the command is as long as the `Program` is executable. You can specify extra arguments with the `Arguments` field. -You can ask it to put the payload as the last argument by setting `AppendPayload` to true. +You can ask it to put the payload as the last (or second to last if `AppendHeaders` is set) argument by setting `AppendPayload` to true. +You can ask it to put the request headers as the last argument by setting `AppendHeaders` to true. ### Writing Tests