From c70c21fda7b0a9c9b6a452a1c60ef3ede7166673 Mon Sep 17 00:00:00 2001 From: Adnan Hajdarevic Date: Tue, 13 Jan 2015 00:59:18 +0100 Subject: [PATCH] added flags to specify the hooks json file path, ip and port the webhook should serve on --- .gitignore | 0 hooks.json => hooks.json.example | 0 hooks/hooks.go | 4 ++++ webhook.go | 24 +++++++++++++++--------- 4 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 .gitignore rename hooks.json => hooks.json.example (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/hooks.json b/hooks.json.example similarity index 100% rename from hooks.json rename to hooks.json.example diff --git a/hooks/hooks.go b/hooks/hooks.go index 0c7d70d..24509f1 100644 --- a/hooks/hooks.go +++ b/hooks/hooks.go @@ -103,6 +103,10 @@ func (h *Hook) UnmarshalJSON(j []byte) error { func New(hookFile string) (*Hooks, error) { h := &Hooks{fileName: hookFile} + if hookFile == "" { + return h, nil + } + // parse hook file for hooks file, e := ioutil.ReadFile(hookFile) diff --git a/webhook.go b/webhook.go index dd075e8..89aed6b 100644 --- a/webhook.go +++ b/webhook.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "flag" "fmt" "net/http" "os/exec" @@ -14,23 +15,28 @@ import ( const ( version string = "1.0.0" - ip string = "" - port int = 9000 ) var ( - webhooks *hooks.Hooks - appStart time.Time + webhooks *hooks.Hooks + appStart time.Time + ip = flag.String("ip", "", "ip the webhook server should listen on") + port = flag.Int("port", 9000, "port the webhook server should listen on") + hooksFilename = flag.String("hooks", "hooks.json", "path to the json file containing defined hooks the webhook should serve") ) +func init() { + flag.Parse() +} + func main() { appStart = time.Now() var e error - webhooks, e = hooks.New("hooks.json") + webhooks, e = hooks.New(*hooksFilename) if e != nil { - fmt.Printf("Error while loading hooks from hooks.json:\n\t>>> %s\n", e) + fmt.Printf("Error while loading hooks from %s:\n\t>>> %s\n", *hooksFilename, e) } web := martini.Classic() @@ -39,9 +45,9 @@ func main() { web.Get("/hook/:id", hookHandler) web.Post("/hook/:id", hookHandler) - fmt.Printf("Starting go-webhook with %d hook(s)\n\n", webhooks.Count()) + fmt.Printf("Starting go-webhook with %d hook(s)\n", webhooks.Count()) - web.RunOnAddr(fmt.Sprintf("%s:%d", ip, port)) + web.RunOnAddr(fmt.Sprintf("%s:%d", *ip, *port)) } func rootHandler() string { @@ -49,7 +55,6 @@ func rootHandler() string { } func hookHandler(req *http.Request, params martini.Params) string { - decoder := json.NewDecoder(req.Body) decoder.UseNumber() @@ -68,5 +73,6 @@ func hookHandler(req *http.Request, params martini.Params) string { fmt.Printf("Command output for %v >>> %s\n", hook, out) } }(params["id"], p) + return "Got it, thanks. :-)" }