small pixel drawing of a pufferfish zoa

it do be alive doe
Jes Olson j3s@c3f.net
Tue, 27 Sep 2022 02:19:34 -0500
commit

fb0a048223c1176b3d30d06d40157501ac5eb91b

parent

b5a1767f1d3688892e2a2b418154babe1a2b4ae9

A .gitignore

@@ -0,0 +1,1 @@

+zoa
M go.modgo.mod

@@ -1,3 +1,10 @@

module git.j3s.sh/zoa go 1.19 + +require ( + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect + golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect + golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect + mvdan.cc/sh/v3 v3.5.1 // indirect +)
A go.sum

@@ -0,0 +1,8 @@

+golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +mvdan.cc/sh/v3 v3.5.1 h1:hmP3UOw4f+EYexsJjFxvU38+kn+V/s2CclXHanIBkmQ= +mvdan.cc/sh/v3 v3.5.1/go.mod h1:1JcoyAKm1lZw/2bZje/iYKWicU/KMd0rsyJeKHnsK4E=
M main.gomain.go

@@ -1,9 +1,101 @@

package main import ( + "bytes" + "context" "fmt" + "log" + "os" + "path/filepath" + "strings" + + "mvdan.cc/sh/v3/interp" + "mvdan.cc/sh/v3/syntax" ) + +var ctx = context.Background() +var rootDir = "test/" func main() { - fmt.Println("zoa zoa zoa") + // TODO: this writer is responsible for the random stdout + // maybe save the stdout for debug mode somehow + r, err := interp.New(interp.StdIO(nil, os.Stdout, os.Stderr)) + if err != nil { + log.Fatal(err) + } + + entrypoint := filepath.Join(rootDir, "main") + runCommands(entrypoint, r) } + +// this is used to detect when the script +// name changes from run to run, which allows +// us to prettily-print +var lastScriptPath string + +func runCommands(scriptPath string, r *interp.Runner) { + script, err := parseFile(scriptPath) + if err != nil { + log.Fatal(err) + } + + // execute every statement individually, decorating + // each with ->, and doing some speshul logicks against + // certain strings + for _, stmt := range script.Stmts { + cmdName := commandName(stmt) + command, after, _ := strings.Cut(cmdName, " ") + if command == "zoa-script" { + // recursion detected!! :3 + subScriptPath := filepath.Join(rootDir + "scripts/" + after) + runCommands(subScriptPath, r) + continue + } + + // if the script name changed between runs, + // print it + if scriptPath != lastScriptPath { + fmt.Println("\t" + scriptPath) + lastScriptPath = scriptPath + } + + fmt.Printf("\t$ %s\n", cmdName) + err = r.Run(ctx, stmt) + if err != nil { + os.Exit(1) + } + } +} + +func runCommand(c context.Context, s *syntax.Stmt, r *interp.Runner) { + name := commandName(s) + fmt.Printf(" -> %s\n", name) + err := r.Run(c, s) + if err != nil { + os.Exit(1) + } +} + +func commandName(statement *syntax.Stmt) string { + b := new(bytes.Buffer) + syntax.NewPrinter().Print(b, statement) + return b.String() +} + +func parseFile(filename string) (*syntax.File, error) { + var result = &syntax.File{} + f, err := os.Open(filename) + if err != nil { + return result, err + } + result, err = syntax.NewParser().Parse(f, "") + return result, err +} + +// runStatements takes a file & runs individual +// commands from that file, prepending the decorator +// and returning the first error +// func runScript(file *syntax.File) error { +// fmt.Printf("%s%s\n", decorator, output) +// return nil +// }
A test/main

@@ -0,0 +1,32 @@

+# hewo +ls + +PASSTHROUGH=bananas + +# fix perms +uname -a + +# test invalid code and print it +# ls /asfj + +# test another script inclusion +# todo + +zoa-script 2-call-me-from-anotha +# . ./test/2-call-me-from-anotha + +echo i am main + +if [ "$(uname -n)" = "nostromo" ]; then + echo NOSTROMO +fi +echo $HOSTNAME + +zoa-script 3-more + +# copy a file from files/ +# zoa-file main.go /tmp/garbagecan.go + +cat >/tmp/trashcan <<EOF +hello i am a trash can man :3 +EOF
A test/scripts/2-call-me-from-anotha

@@ -0,0 +1,7 @@

+echo "$PASSTHROUGH" + +uname -a + +ls -l + +zoa-script 3-more
A test/scripts/3-more

@@ -0,0 +1,1 @@

+uname
A test/scripts/zoa-run

@@ -0,0 +1,3 @@

+ls -l +apk update +apk add cowsay