From 25541dadfed0c9e726ad105cf1d4e6ad7a889e45 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 8 Dec 2024 03:28:33 -0500 Subject: [PATCH] providers/os: use Document.Rel --- providers/os/os.go | 90 +++++++++++++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 29 deletions(-) diff --git a/providers/os/os.go b/providers/os/os.go index db0b125..5c511a4 100644 --- a/providers/os/os.go +++ b/providers/os/os.go @@ -7,7 +7,7 @@ import "io/fs" import "html/template" import "git.tebibyte.media/sashakoshka/step" -var _ step.FuncProvider = new(Provider) +var _ step.FuncProviderFor = new(Provider) // FileInfo is like the fs.FileInfo interface, but it directly contains the // values instead of having getters for them. @@ -26,43 +26,63 @@ type Provider struct { // FuncMap fulfills the step.FuncProvider interface. func (this *Provider) FuncMap () template.FuncMap { + return nil +} + +// FuncMapFor fulfills the step.FuncProviderFor interface. +func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap { + stat := &state { + document: document, + } return template.FuncMap { - "env": funcEnv, - "exists": funcExists, - "isFile": funcIsFile, - "isDir": funcIsDir, - "statFile": funcStatFile, - "readFile": funcReadFile, - "readDir": funcReadDir, - "writeFile": funcWriteFile, - "appendFile": funcAppendFile, - "renameFile": funcRenameFile, - "removeFile": funcRemoveFile, + "env": stat.funcEnv, + "exists": stat.funcExists, + "isFile": stat.funcIsFile, + "isDir": stat.funcIsDir, + "statFile": stat.funcStatFile, + "readFile": stat.funcReadFile, + "readDir": stat.funcReadDir, + "writeFile": stat.funcWriteFile, + "appendFile": stat.funcAppendFile, + "renameFile": stat.funcRenameFile, + "removeFile": stat.funcRemoveFile, } } -func funcEnv (name string) string { +type state struct { + document *step.Document +} + +func (this *state) funcEnv (name string) string { return os.Getenv(name) } -func funcExists (name string) (bool) { - _, err := os.Stat(name) - return err == nil +func (this *state) funcExists (name string) (bool, error) { + name, err := this.document.Rel(name) + if err != nil { return false, err } + _, err = os.Stat(name) + return err == nil, nil } -func funcIsFile (name string) bool { +func (this *state) funcIsFile (name string) (bool, error) { + name, err := this.document.Rel(name) + if err != nil { return false, err } info, err := os.Stat(name) - if err != nil { return false } - return info.Mode().IsRegular() + if err != nil { return false, nil } + return info.Mode().IsRegular(),nil } -func funcIsDir (name string) bool { +func (this *state) funcIsDir (name string) (bool, error) { + name, err := this.document.Rel(name) + if err != nil { return false, err } info, err := os.Stat(name) - if err != nil { return false } - return info.IsDir() + if err != nil { return false, nil } + return info.IsDir(), nil } -func funcStatFile (name string) (FileInfo, error) { +func (this *state) funcStatFile (name string) (FileInfo, error) { + name, err := this.document.Rel(name) + if err != nil { return FileInfo { }, err } info, err := os.Stat(name) if err != nil { return FileInfo { }, err } return FileInfo { @@ -74,7 +94,9 @@ func funcStatFile (name string) (FileInfo, error) { }, nil } -func funcReadFile (name string) (string, error) { +func (this *state) funcReadFile (name string) (string, error) { + name, err := this.document.Rel(name) + if err != nil { return "", err } file, err := os.Open(name) if err != nil { return "", err } defer file.Close() @@ -83,7 +105,9 @@ func funcReadFile (name string) (string, error) { return string(buffer), nil } -func funcReadDir (name string) ([]string, error) { +func (this *state) funcReadDir (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 } names := make([]string, len(entries)) @@ -93,24 +117,32 @@ func funcReadDir (name string) ([]string, error) { return names, nil } -func funcWriteFile (name, content string) error { +func (this *state) funcWriteFile (name, content string) error { + name, err := this.document.Rel(name) + if err != nil { return err } file, err := os.Create(name) if err != nil { return err } _, err = io.WriteString(file, content) return err } -func funcAppendFile (name, content string) error { +func (this *state) funcAppendFile (name, content string) error { + name, err := this.document.Rel(name) + if err != nil { return err } file, err := os.OpenFile(name, os.O_CREATE | os.O_WRONLY | os.O_APPEND, 644) if err != nil { return err } _, err = io.WriteString(file, content) return err } -func funcRenameFile (name, newName string) error { +func (this *state) funcRenameFile (name, newName string) error { + name, err := this.document.Rel(name) + if err != nil { return err } return os.Rename(name, newName) } -func funcRemoveFile (name string) error { +func (this *state) funcRemoveFile (name string) error { + name, err := this.document.Rel(name) + if err != nil { return err } return os.Remove(name) }