From e72523c03640372a6ce0266a1df262c967c11292 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 29 Apr 2025 09:21:09 -0400 Subject: [PATCH] providers/os: Add countFiles function --- providers/os/os.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/providers/os/os.go b/providers/os/os.go index 57f5f30..1ebb924 100644 --- a/providers/os/os.go +++ b/providers/os/os.go @@ -37,6 +37,7 @@ func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap { "readDirDate": stat.funcReadDirDate, "listFiles": stat.funcListFiles, "listFilesDate": stat.funcListFilesDate, + "countFiles": stat.countFiles, "writeFile": stat.funcWriteFile, "appendFile": stat.funcAppendFile, "remove": stat.funcRemove, @@ -145,6 +146,17 @@ func (this *state) funcWriteFile (name, content string) error { return err } +func (this *state) countFiles(name string) (int, error) { + name, err := this.document.Rel(name) + if err != nil { return err } + // genuinely can't believe this is the only way to do it, we have to + // read their names and everything, all that work just to know how many + // files there are, its slightly upsetting + files, err := os.ReadDir(name) + if err != nil { return err } + return len(files), nil +} + func (this *state) funcAppendFile (name, content string) error { name, err := this.document.Rel(name) if err != nil { return err }