Started work on multi file hooks loading

This commit is contained in:
Adnan Hajdarevic 2016-12-05 21:22:34 +01:00
parent 07f166616c
commit 8207c6cf12
3 changed files with 116 additions and 45 deletions

View file

@ -273,7 +273,7 @@ func (h *ResponseHeaders) String() string {
result[idx] = fmt.Sprintf("%s=%s", responseHeader.Name, responseHeader.Value)
}
return fmt.Sprint(strings.Join(result, ", "))
return strings.Join(result, ", ")
}
// Set method appends new Header object from header=value notation
@ -288,6 +288,23 @@ func (h *ResponseHeaders) Set(value string) error {
return nil
}
// HooksFiles is a slice of String
type HooksFiles []string
func (h *HooksFiles) String() string {
if len(*h) == 0 {
return "hooks.json"
}
return strings.Join(*h, ", ")
}
// Set method appends new string
func (h *HooksFiles) Set(value string) error {
*h = append(*h, value)
return nil
}
// Hook type is a structure containing details for a single hook
type Hook struct {
ID string `json:"id,omitempty"`
@ -426,6 +443,19 @@ func (h *Hooks) LoadFromFile(path string) error {
return e
}
// Append appends hooks unless the new hooks contain a hook with an ID that already exists
func (h *Hooks) Append(other *Hooks) error {
for _, hook := range *other {
if h.Match(hook.ID) != nil {
return fmt.Errorf("hook with ID %s is already defined", hook.ID)
}
*h = append(*h, hook)
}
return nil
}
// Match iterates through Hooks and returns first one that matches the given ID,
// if no hook matches the given ID, nil is returned
func (h *Hooks) Match(id string) *Hook {