refactoring

This commit is contained in:
Adnan Hajdarevic 2015-02-25 00:12:24 +01:00
parent a5d79fddd9
commit f1c4415fc8
5 changed files with 130 additions and 74 deletions

View file

@ -3,7 +3,9 @@ package hooks
import (
"encoding/json"
"io/ioutil"
"net/url"
"github.com/adnanh/webhook/helpers"
"github.com/adnanh/webhook/rules"
)
@ -25,6 +27,40 @@ type Hooks struct {
list []Hook
}
// ParseFormArgs gets arguments from the Form payload that should be passed to the command
func (h *Hook) ParseFormArgs(form url.Values) []string {
var args = make([]string, len(h.Args))
args = append(args, h.Command)
for i := range h.Args {
if arg := form[h.Args[i]]; len(arg) > 0 {
args = append(args, arg[0])
} else {
args = append(args, "")
}
}
return args
}
// ParseJSONArgs gets arguments from the JSON payload that should be passed to the command
func (h *Hook) ParseJSONArgs(payload interface{}) []string {
var args = make([]string, len(h.Args))
args = append(args, h.Command)
for i := range h.Args {
if arg, ok := helpers.ExtractJSONParameter(h.Args[i], payload); ok {
args = append(args, arg)
} else {
args = append(args, "")
}
}
return args
}
// UnmarshalJSON implementation for a single hook
func (h *Hook) UnmarshalJSON(j []byte) error {
m := make(map[string]interface{})