dir-feat: Make loading from directory recursively possible

The commit adds the following modifications:
 * Possibility to load hooks recursively from a directory
 * Extraction of the loadinghook method in webhook.go
 * Tests done on a fairly complex tree of directory

Allowing hooks to be loaded from a directory is a nice feature, as it
allows a better organisation/readibility of the hooks that we have.

The LoadFromDir method rely on LoadFromFile and is meant to be very
permissive, and will return errors only when no hooks have been loaded.
Otherwise it will simply return 'nil' and the potential issues/warnings
that happen during the loading of hooks (invalid file, invalid path,
issue opening, etc).
This commit is contained in:
Hugo Rosnet 2016-06-13 15:32:19 +02:00
parent 18b0573bc4
commit 1db3be532b
7 changed files with 345 additions and 18 deletions

View file

@ -12,6 +12,7 @@ import (
"regexp"
"strconv"
"strings"
"os"
)
// Constants used to specify the parameter source
@ -400,6 +401,48 @@ func (h *Hooks) LoadFromFile(path string) error {
return e
}
// LoadFromDir attempts to load hooks from specified Dir
func (h *Hooks) LoadFromDir(path string) ([]string, error) {
warnings := []string{}
loaded := fmt.Errorf("no single hooks loaded from directory %s", path)
if _, e := os.Stat(path); os.IsNotExist(e) {
if path == "" {return []string{"path '" + path + "' is unspecified"}, nil}
return []string{"path '" + path + "' is invalid"}, loaded
}
files, e := ioutil.ReadDir(path)
if e != nil {
return []string{path + " issue while readdir"}, e
} else if len(files) == 0 {
return []string{path + " is empty"}, loaded
}
// add '/' to path if missing
if path[len(path)-1] != '/' {path += "/"}
for _, file := range files {
tmp_hooks := Hooks{}
if file.IsDir() == true {
msg, e := tmp_hooks.LoadFromDir(path + file.Name())
if len(msg) != 0 {
warnings = append(warnings, msg...)
}
if e == nil {
*h = append(*h, tmp_hooks...)
}
} else {
e := tmp_hooks.LoadFromFile(path + file.Name())
if e != nil {
warnings = append(warnings, fmt.Sprintf("%s%s (%+v)", path, file.Name(), e))
} else {
*h = append(*h, tmp_hooks...)
}
}
}
if len(*h) == 0 {return warnings, loaded}
return warnings, 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 {