From cf10f53789d0dd770ac2e636b41b191677e948af Mon Sep 17 00:00:00 2001 From: Mike Pountney Date: Wed, 23 Mar 2016 21:22:02 +0000 Subject: [PATCH] Include templates from a system path I've found that several of the built-in templates need a bit of work to be useful with our JIRA installation (eg #24), so this allows for admins to make a site-specific set of defaults easily. --- cli.go | 38 +++++++++++++++++++------------------- util.go | 1 + 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/cli.go b/cli.go index 767c328..5c3ee62 100644 --- a/cli.go +++ b/cli.go @@ -213,33 +213,33 @@ func (c *Cli) GetTemplate(name string) string { return c.getTemplate(name) } +func getLookedUpTemplate(name string, dflt string) string { + if file, err := FindClosestParentPath(fmt.Sprintf(".jira.d/templates/%s", name)); err == nil { + return readFile(file) + } + if _, err := os.Stat(fmt.Sprintf("/etc/go-jira/templates/%s", name)); err == nil { + file := fmt.Sprintf("/etc/go-jira/templates/%s", name) + return readFile(file) + } + return dflt +} + func (c *Cli) getTemplate(name string) string { if override, ok := c.opts["template"].(string); ok { if _, err := os.Stat(override); err == nil { return readFile(override) } else { - if file, err := FindClosestParentPath(fmt.Sprintf(".jira.d/templates/%s", override)); err == nil { - return readFile(file) - } - if dflt, ok := all_templates[override]; ok { - return dflt + if t := getLookedUpTemplate(override, all_templates[override]); t != "" { + return t } } } - if file, err := FindClosestParentPath(fmt.Sprintf(".jira.d/templates/%s", name)); err != nil { - // create-bug etc are special, if we dont find it in the path - // then just return a generic create template - if strings.HasPrefix(name, "create-") { - if file, err := FindClosestParentPath(".jira.d/templates/create"); err != nil { - return all_templates["create"] - } else { - return readFile(file) - } - } - return all_templates[name] - } else { - return readFile(file) + // create-bug etc are special, if we dont find it in the path + // then just return the create template + if strings.HasPrefix(name, "create-") { + return getLookedUpTemplate(name, c.getTemplate("create")) } + return getLookedUpTemplate(name, all_templates[name]) } type NoChangesFound struct{} @@ -512,4 +512,4 @@ func (c *Cli) expansions() []string { expansions = strings.Split(x, ",") } return expansions -} \ No newline at end of file +} diff --git a/util.go b/util.go index a42129a..f1fc250 100644 --- a/util.go +++ b/util.go @@ -57,6 +57,7 @@ func FindClosestParentPath(fileName string) (string, error) { func readFile(file string) string { var bytes []byte var err error + log.Debugf("readFile: reading %q", file) if bytes, err = ioutil.ReadFile(file); err != nil { log.Errorf("Failed to read file %s: %s", file, err) os.Exit(1)