providers/os: Add listFiles function that works like the caddy one

This commit is contained in:
Sasha Koshka 2024-12-14 03:02:51 -05:00
parent fa8e359ec0
commit f9fb355c8c

View File

@ -31,6 +31,7 @@ func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap {
"statFile": stat.funcStatFile,
"readFile": stat.funcReadFile,
"readDir": stat.funcReadDir,
"listFiles": stat.funcListFiles,
"writeFile": stat.funcWriteFile,
"appendFile": stat.funcAppendFile,
"renameFile": stat.funcRenameFile,
@ -92,6 +93,18 @@ func (this *state) funcReadDir (name string) ([]fs.DirEntry, error) {
return os.ReadDir(name)
}
func (this *state) funcListFiles (name string) ([]string, error) {
name, err := this.document.Rel(name)
if err != nil { return nil, err }
entries, err := os.ReadDir(name)
if err != nil { return nil, err }
strings := make([]string, len(entries))
for index, entry := range entries {
strings[index] = entry.Name()
}
return strings, nil
}
func (this *state) funcWriteFile (name, content string) error {
name, err := this.document.Rel(name)
if err != nil { return err }