leo
122ea638c9
All checks were successful
continuous-integration/drone/push Build is passing
* create pkg 'modules/template' * move template rendering code from 'handlers' to 'modules/template' * update call sites * walk the 'templates' dir to discover nested hierarchies * solidify LiveMode handling (vs embedded assets) * break out funcMap to it's own file * general clean-up
72 lines
1.3 KiB
Go
72 lines
1.3 KiB
Go
package template
|
|
|
|
import (
|
|
"io/fs"
|
|
"path/filepath"
|
|
)
|
|
|
|
// globDir lists files inside a dir that match the suffix provided.
|
|
func globDir(dir, suffix string) ([]string, error) {
|
|
files := []string{}
|
|
|
|
err := filepath.WalkDir(dir, func(path string, de fs.DirEntry, err error) error {
|
|
if de.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
if filepath.Ext(path) == suffix {
|
|
files = append(files, path)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
return files, err
|
|
}
|
|
|
|
// globFS lists files inside a dir of a fs that match the suffix provided.
|
|
func globFS(fspls fs.FS, dir, suffix string) ([]string, error) {
|
|
files := []string{}
|
|
|
|
err := fs.WalkDir(fspls, dir, func(path string, de fs.DirEntry, err error) error {
|
|
if de.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
if filepath.Ext(path) == suffix {
|
|
files = append(files, path)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
return files, err
|
|
}
|
|
|
|
// listAll returns template file names as []string and panics on error.
|
|
// uses filepath.WalkDir via glob* funcs. TODO: don't panic.
|
|
func listAll(pattern string) []string {
|
|
files := make([]string, 0)
|
|
|
|
switch {
|
|
case !setting.IsLive():
|
|
f, err := globFS(tmplFS, ".", pattern)
|
|
if err != nil {
|
|
log.Errorf("error: %q", err)
|
|
panic(err)
|
|
}
|
|
|
|
files = f
|
|
|
|
case setting.IsLive():
|
|
f, err := globDir(tmplPath, pattern)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
files = f
|
|
}
|
|
|
|
return files
|
|
}
|