Initial commit

This commit is contained in:
2024-12-05 13:15:22 -05:00
commit 91e72ac49e
17 changed files with 1517 additions and 0 deletions

13
providers/all.go Normal file
View File

@@ -0,0 +1,13 @@
package providers
import "git.tebibyte.media/sashakoshka/step"
import fpos "git.tebibyte.media/sashakoshka/step/providers/os"
import fpsprig "git.tebibyte.media/sashakoshka/step/providers/sprig"
// All returns a slice of all providers defined in sub-packages.
func All () []step.FuncProvider {
return []step.FuncProvider {
new(fpsprig.Provider),
new(fpos.Provider),
}
}

40
providers/math/math.go Normal file
View File

@@ -0,0 +1,40 @@
package math
import "math"
import "html/template"
import "git.tebibyte.media/sashakoshka/step"
var _ step.FuncProvider = new(Provider)
// Provider provides math functions.
type Provider struct {
}
// FuncMap fulfills the step.FuncProvider interface.
func (this *Provider) FuncMap () template.FuncMap {
return template.FuncMap {
"add": funcAdd,
"sub": funcSub,
"mul": funcMul,
"div": funcDiv,
"pow": math.Pow,
"sqrt": math.Sqrt,
}
}
func funcAdd (a, b float64) float64 {
return a + b
}
func funcSub (a, b float64) float64 {
return a - b
}
func funcMul (a, b float64) float64 {
return a * b
}
func funcDiv (a, b float64) float64 {
return a / b
}

64
providers/os/os.go Normal file
View File

@@ -0,0 +1,64 @@
package os
import "os"
import "io"
import "html/template"
import "git.tebibyte.media/sashakoshka/step"
var _ step.FuncProvider = new(Provider)
// Provider provides OS functions.
type Provider struct {
}
// FuncMap fulfills the step.FuncProvider interface.
func (this *Provider) FuncMap () template.FuncMap {
return template.FuncMap {
"fileExists": funcFileExists,
"readFile": funcReadFile,
"readDir": funcReadDir,
"writeFile": funcWriteFile,
"renameFile": funcRenameFile,
"removeFile": funcRemoveFile,
}
}
func funcFileExists (name string) (bool) {
_, err := os.Stat(name)
return err == nil
}
func funcReadFile (name string) (string, error) {
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 funcReadDir (name string) ([]string, error) {
entries, err := os.ReadDir(name)
if err != nil { return nil, err }
names := make([]string, len(entries))
for index, entry := range entries {
names[index] = entry.Name()
}
return names, nil
}
func funcWriteFile (name, content string) error {
file, err := os.Create(name)
if err != nil { return err }
_, err = io.WriteString(file, content)
return err
}
func funcRenameFile (name, newName string) error {
return os.Rename(name, newName)
}
func funcRemoveFile (name string) error {
return os.Remove(name)
}

17
providers/sprig/sprig.go Normal file
View File

@@ -0,0 +1,17 @@
package sprig
import "html/template"
import "github.com/Masterminds/sprig/v3"
import "git.tebibyte.media/sashakoshka/step"
var _ step.FuncProvider = new(Provider)
// Provider provides all Sprig functions.
type Provider struct {
}
// FuncMap fulfills the step.FuncProvider interface.
func (this *Provider) FuncMap () template.FuncMap {
return sprig.FuncMap()
}