diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..769402d --- /dev/null +++ b/errors.go @@ -0,0 +1,9 @@ +package main + +import "git.milar.in/milarin/adverr" + +var ( + ErrInvalidTemplateSyntax = adverr.NewErrTmpl("ErrInvalidTemplateSyntax", "parsing of template failed: %s") + ErrTemplateExecutionFailed = adverr.NewErrTmpl("ErrTemplateExecutionFailed", "execution of template failed: %s") + ErrDirectoryCreationFailed = adverr.NewErrTmpl("ErrDirectoryCreationFailed", "creation of target directory failed: %s") +) diff --git a/go.mod b/go.mod index c00fb36..d31a214 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module git.milar.in/milarin/cfgen go 1.19 -require gopkg.in/yaml.v2 v2.4.0 +require ( + git.milar.in/milarin/adverr v1.1.0 + gopkg.in/yaml.v2 v2.4.0 +) diff --git a/go.sum b/go.sum index dd0bc19..1d5040d 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +git.milar.in/milarin/adverr v1.1.0 h1:jD9WnOvs40lfMhvqQ7cllOaRJNBMWr1f07/s9jAadp0= +git.milar.in/milarin/adverr v1.1.0/go.mod h1:joU9sBb7ySyNv4SpTXB0Z4o1mjXsArBw4N27wjgzj9E= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/handle_template.go b/handle_template.go index 969f903..8656771 100644 --- a/handle_template.go +++ b/handle_template.go @@ -13,7 +13,7 @@ import ( func HandleTemplate(tmpl *template.Template) error { b := &bytes.Buffer{} if err := tmpl.Execute(b, ConfigFile.Globals); err != nil { - return err + return ErrTemplateExecutionFailed.Wrap(err, tmpl.Name()) } if len(strings.TrimSpace(b.String())) == 0 { diff --git a/main.go b/main.go index 448f64d..431538d 100644 --- a/main.go +++ b/main.go @@ -3,9 +3,11 @@ package main import ( "errors" "flag" + "fmt" "os" "text/template" + "git.milar.in/milarin/adverr" "gopkg.in/yaml.v2" ) @@ -17,21 +19,22 @@ func main() { flag.Parse() if err := ReadConfigFile(); err != nil { - panic(err) + fmt.Fprintln(os.Stderr, "no config file provided") + os.Exit(1) } tmplMap, err := ParseTemplates(template.New("template"), ConfigFile.TemplateDir) if err != nil { - panic(err) + adverr.Fatalln(err, 2) } if err := os.MkdirAll(ConfigFile.TargetDir, 0755); err != nil { - panic(err) + adverr.Fatalln(ErrDirectoryCreationFailed.Wrap(err, ConfigFile.TargetDir), 3) } for _, tmpl := range tmplMap { if err := HandleTemplate(tmpl); err != nil { - panic(err) + adverr.Fatalln(err, 4) } } } diff --git a/parse_templates.go b/parse_templates.go index c564c83..100a168 100644 --- a/parse_templates.go +++ b/parse_templates.go @@ -48,7 +48,7 @@ func parseTemplates(tmpl *template.Template, tmplMap map[string]*template.Templa tmpl, err = tmpl.New(tmplName).Parse(string(data)) if err != nil { - return err + return ErrInvalidTemplateSyntax.Wrap(err, tmplName) } tmplMap[tmplName] = tmpl