providers/os: Add function for creating temporary files

This commit is contained in:
Sasha Koshka 2025-03-27 20:03:58 -04:00
parent 8c5e69fcdf
commit c623c4f35e

View File

@ -5,6 +5,7 @@ import "io"
import "time"
import "sort"
import "io/fs"
import "path/filepath"
import "html/template"
import "git.tebibyte.media/sashakoshka/step"
@ -43,7 +44,7 @@ func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap {
"createDir": stat.funcCreateDir,
"createDirAll": stat.funcCreateDirAll,
"chmod": stat.funcChmod,
// TODO: temporary files, dirs
"createTemp": stat.funcCreateTemp,
}
}
@ -185,6 +186,15 @@ func (this *state) funcChmod(name string, mode fs.FileMode) error {
return os.Chmod(name, mode)
}
func (this *state) funcCreateTemp(dir, pattern string) (string, error) {
dir, err := this.document.Rel(dir)
if err != nil { return "", err }
file, err := os.CreateTemp(dir, pattern)
if err != nil { return "", err }
defer file.Close()
return filepath.Join(dir, file.Name()), nil
}
func sortDirEntriesByDate(entries []fs.DirEntry) {
sort.Slice(entries, func(left, right int) bool{
var leftTime time.Time