Skip to content

Instantly share code, notes, and snippets.

@LeKovr
Created December 21, 2015 09:38
Show Gist options
  • Save LeKovr/9b079a413e429758e53d to your computer and use it in GitHub Desktop.
Save LeKovr/9b079a413e429758e53d to your computer and use it in GitHub Desktop.
Hook response parsing example
package main
/*
hookex.go - Hook response parsing example
Use:
echo '{"output":"Hook start\nProcessing event (push)\nError fired by secret\n","error":"exit status 1"}' | go run hookex.go
*/
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
func main() {
bytes, err := ioutil.ReadAll(os.Stdin)
checkErr(err)
mapData := make(map[string]interface{})
err = json.Unmarshal(bytes, &mapData)
checkErr(err)
if out, ok := mapData["output"]; ok {
delete(mapData, "output")
fmt.Println("*** Output:\n", out)
}
data, err := json.MarshalIndent(mapData, "", " ")
checkErr(err)
fmt.Println("*** Payload:\n", string(data))
}
func checkErr(err error) {
if err != nil {
panic(err.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment