providers/os: Add countFiles function

This commit is contained in:
Sasha Koshka 2025-04-29 09:21:09 -04:00
parent b974fde7e4
commit e72523c036

View File

@ -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 }