diff --git a/internal/hook/hook.go b/internal/hook/hook.go index d8a41fb..f0afb79 100644 --- a/internal/hook/hook.go +++ b/internal/hook/hook.go @@ -818,7 +818,9 @@ type Hooks []Hook // LoadFromFile attempts to load hooks from the specified file, which // can be either JSON or YAML. The asTemplate parameter causes the file // contents to be parsed as a Go text/template prior to unmarshalling. -func (h *Hooks) LoadFromFile(path string, asTemplate bool) error { +// The delimsStr parameter is a comma-separated pair of the left and right +// template delimiters, or an empty string to use the default '{{,}}'. +func (h *Hooks) LoadFromFile(path string, asTemplate bool, delimsStr string) error { if path == "" { return nil } @@ -832,8 +834,12 @@ func (h *Hooks) LoadFromFile(path string, asTemplate bool) error { if asTemplate { funcMap := template.FuncMap{"getenv": getenv} + left, right, found := strings.Cut(delimsStr, ",") + if !found && delimsStr != "" { + return fmt.Errorf("invalid delimiters %q - should be left and right delimiters separated by a comma", delimsStr) + } - tmpl, err := template.New("hooks").Funcs(funcMap).Parse(string(file)) + tmpl, err := template.New("hooks").Funcs(funcMap).Delims(strings.TrimSpace(left), strings.TrimSpace(right)).Parse(string(file)) if err != nil { return err } diff --git a/internal/hook/hook_test.go b/internal/hook/hook_test.go index f6c087d..aeb5131 100644 --- a/internal/hook/hook_test.go +++ b/internal/hook/hook_test.go @@ -433,7 +433,7 @@ func TestHooksLoadFromFile(t *testing.T) { for _, tt := range hooksLoadFromFileTests { h := &Hooks{} - err := h.LoadFromFile(tt.path, tt.asTemplate) + err := h.LoadFromFile(tt.path, tt.asTemplate, "") if (err == nil) != tt.ok { t.Errorf(err.Error()) } @@ -450,7 +450,7 @@ func TestHooksTemplateLoadFromFile(t *testing.T) { } h := &Hooks{} - err := h.LoadFromFile(tt.path, tt.asTemplate) + err := h.LoadFromFile(tt.path, tt.asTemplate, "") if (err == nil) != tt.ok { t.Errorf(err.Error()) continue diff --git a/webhook.go b/webhook.go index 9ed793c..7273665 100644 --- a/webhook.go +++ b/webhook.go @@ -39,6 +39,7 @@ var ( hooksURLPrefix = flag.String("urlprefix", "hooks", "url prefix to use for served hooks (protocol://yourserver:port/PREFIX/:hook-id)") secure = flag.Bool("secure", false, "use HTTPS instead of HTTP") asTemplate = flag.Bool("template", false, "parse hooks file as a Go template") + templateDelimiters = flag.String("template-delims", "", "a comma-separated pair of delimiters, e.g. '((,))' or '[[,]]' to use instead of the standard '{{,}}' when parsing hooks file as a template, to avoid clashing with any \"source\": \"template\" arguments") cert = flag.String("cert", "cert.pem", "path to the HTTPS certificate pem file") key = flag.String("key", "key.pem", "path to the HTTPS certificate private key pem file") justDisplayVersion = flag.Bool("version", false, "display webhook version and quit") @@ -204,7 +205,7 @@ func main() { newHooks := hook.Hooks{} - err := newHooks.LoadFromFile(hooksFilePath, *asTemplate) + err := newHooks.LoadFromFile(hooksFilePath, *asTemplate, *templateDelimiters) if err != nil { log.Printf("couldn't load hooks from file! %+v\n", err) @@ -670,7 +671,7 @@ func reloadHooks(hooksFilePath string) { // parse and swap log.Printf("attempting to reload hooks from %s\n", hooksFilePath) - err := hooksInFile.LoadFromFile(hooksFilePath, *asTemplate) + err := hooksInFile.LoadFromFile(hooksFilePath, *asTemplate, *templateDelimiters) if err != nil { log.Printf("couldn't load hooks from file! %+v\n", err)