From e3770adc87db3b59c5d9ffadca638cd2b0a64f6d Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Sun, 29 Oct 2023 15:55:39 -0700 Subject: [PATCH] Add ParseGlob functions --- parse.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/parse.go b/parse.go index 2e8c7c6..9532c7c 100644 --- a/parse.go +++ b/parse.go @@ -5,6 +5,7 @@ import ( "io" "io/fs" "os" + "path/filepath" "reflect" "strings" @@ -57,6 +58,24 @@ func (t *Namespace) ParseFile(path string) (*Template, error) { return t.Parse(fl) } +// ParseGlob parses all the files that were matched by the given glob +// nd adds them to the namespace. +func (t *Namespace) ParseGlob(glob string) error { + matches, err := filepath.Glob(glob) + if err != nil { + return err + } + + for _, match := range matches { + _, err := t.ParseFile(match) + if err != nil { + return err + } + } + + return nil +} + // ParseFile parses a file at the given path in a filesystem. It uses the path as the name. func (t *Namespace) ParseFS(fsys fs.FS, path string) (*Template, error) { fl, err := fsys.Open(path) @@ -67,6 +86,24 @@ func (t *Namespace) ParseFS(fsys fs.FS, path string) (*Template, error) { return t.ParseWithName(path, fl) } +// ParseGlob parses all the files in the filesystem that were matched by the given glob +// and adds them to the namespace. +func (t *Namespace) ParseFSGlob(fsys fs.FS, glob string) error { + matches, err := fs.Glob(fsys, glob) + if err != nil { + return err + } + + for _, match := range matches { + _, err := t.ParseFS(fsys, match) + if err != nil { + return err + } + } + + return nil +} + // ParseString parses a string using the given filename. func (t *Namespace) ParseString(filename, tmpl string) (*Template, error) { return t.ParseWithName(filename, strings.NewReader(tmpl))