From f9fb355c8c39a97b2714c3397f15336633586306 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 14 Dec 2024 03:02:51 -0500 Subject: [PATCH] providers/os: Add listFiles function that works like the caddy one --- providers/os/os.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/providers/os/os.go b/providers/os/os.go index e652a10..3bca063 100644 --- a/providers/os/os.go +++ b/providers/os/os.go @@ -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 }