commit db10e121a0ef1912052258113873baeedd830034
parent d1d5bfa7cc12c125cc8066f9a77a8294cd304db2
Author: gearsix <gearsix@tuta.io>
Date: Sun, 3 Oct 2021 14:13:04 +0100
tidyup refactors
Diffstat:
1 file changed, 12 insertions(+), 11 deletions(-)
diff --git a/template.go b/template.go
@@ -97,23 +97,15 @@ func LoadTemplateFilepath(rootPath string, partialPaths ...string) (t Template,
return
}
- openf := func(path string) *os.File {
- var f *os.File
- if f, e = os.Open(path); e != nil {
- return nil
- }
- defer f.Close()
- return f
- }
-
lang := strings.TrimPrefix(filepath.Ext(rootPath), ".")
rootName := filepath.Base(rootPath)
var root *os.File
- if root = openf(rootPath); root == nil {
+ if root, e = os.Open(rootPath); e != nil {
return
}
+ defer root.Close()
partials := make(map[string]io.Reader)
for _, path := range partialPaths {
@@ -121,9 +113,18 @@ func LoadTemplateFilepath(rootPath string, partialPaths ...string) (t Template,
if lang == "mst" {
name = strings.TrimSuffix(name, filepath.Ext(name))
}
- if partials[name] = openf(path); partials[name] == nil {
+
+ var p *os.File
+ if stat, e = os.Stat(path); e != nil {
+ return
+ } else if stat.IsDir() {
+ }
+
+ if p, e = os.Open(path); e != nil {
return
}
+ defer p.Close()
+ partials[name] = p
}
return LoadTemplate(lang, rootName, root, partials)