step/providers/os/os.go

163 lines
4.3 KiB
Go

package os
import "os"
import "io"
import "time"
import "sort"
import "io/fs"
import "html/template"
import "git.tebibyte.media/sashakoshka/step"
var _ step.FuncProviderFor = new(Provider)
// Provider provides OS functions.
type Provider struct {
}
// Package fulfills the step.Provider interface.
func (this *Provider) Package () string {
return "os"
}
// FuncMapFor fulfills the step.FuncProviderFor interface.
func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap {
stat := &state {
document: document,
}
return template.FuncMap {
"env": stat.funcEnv,
"exists": stat.funcExists,
"isFile": stat.funcIsFile,
"isDir": stat.funcIsDir,
"statFile": stat.funcStatFile,
"readFile": stat.funcReadFile,
"readDir": stat.funcReadDir,
"listFiles": stat.funcListFiles,
"listFilesDate": stat.funcListFilesDate,
"writeFile": stat.funcWriteFile,
"appendFile": stat.funcAppendFile,
"renameFile": stat.funcRenameFile,
"removeFile": stat.funcRemoveFile,
}
}
type state struct {
document *step.Document
}
func (this *state) funcEnv (name string) string {
return os.Getenv(name)
}
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 (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, nil }
return info.Mode().IsRegular(),nil
}
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, nil }
return info.IsDir(), nil
}
func (this *state) funcStatFile (name string) (fs.FileInfo, error) {
name, err := this.document.Rel(name)
if err != nil { return nil, err }
return os.Stat(name)
}
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()
buffer, err := io.ReadAll(file)
if err != nil { return "", err }
return string(buffer), nil
}
func (this *state) funcReadDir (name string) ([]fs.DirEntry, error) {
name, err := this.document.Rel(name)
if err != nil { return nil, err }
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) funcListFilesDate (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 }
sort.Slice(entries, func(left, right int) bool{
var leftTime time.Time
if leftInfo, err := entries[left].Info(); err == nil {
leftTime = leftInfo.ModTime()
}
var rightTime time.Time
if rightInfo, err := entries[right].Info(); err == nil {
rightTime = rightInfo.ModTime()
}
return leftTime.Before(rightTime)
})
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 }
file, err := os.Create(name)
if err != nil { return err }
_, err = io.WriteString(file, content)
return err
}
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, 0644)
if err != nil { return err }
_, err = io.WriteString(file, content)
return err
}
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 (this *state) funcRemoveFile (name string) error {
name, err := this.document.Rel(name)
if err != nil { return err }
return os.Remove(name)
}