Add zoa fmt
Jes Olson j3s@c3f.net
Tue, 22 Nov 2022 22:50:35 -0800
5 files changed,
50 insertions(+),
8 deletions(-)
M
go.sum
→
go.sum
@@ -1,6 +1,8 @@
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI= github.com/frankban/quicktest v1.14.0 h1:+cqqvzZV87b4adx/5ayVOaYZ2CrvM4ejQvUdBzPPUss= github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= +github.com/google/renameio v1.0.1 h1:Lh/jXZmvZxb0BBeSY5VKEfidcbcbenKjZFzM/q0fSeU= +github.com/google/renameio v1.0.1/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
M
main.go
→
main.go
@@ -13,7 +13,6 @@ "mvdan.cc/sh/v3/interp"
) func printUsageExit() { - // TODO: -v -h fmt.Printf(`usage: zoa <command> [arguments] commands:@@ -45,10 +44,16 @@ command := os.Args[1]
switch command { case "run": zoaRun() + case "fmt": + if len(os.Args) <= 2 { + fmt.Println("fmt requires 1 argument: the path to a posix shell script") + os.Exit(1) + } + shell.ZoaFmt(os.Args[2]) case "cp": var mode os.FileMode if len(os.Args) < 4 { - fmt.Println("zoa cp requires at least 2 arguments") + fmt.Println("cp requires at least 2 arguments: source and destination") os.Exit(1) } if len(os.Args) == 4 {
M
shell/shell.go
→
shell/shell.go
@@ -1,6 +1,7 @@
package shell import ( + "bytes" "context" "fmt" "log"@@ -10,6 +11,7 @@
"j3s.sh/zoa/color" "j3s.sh/zoa/utils" + maybeio "github.com/google/renameio/maybe" "mvdan.cc/sh/v3/interp" "mvdan.cc/sh/v3/syntax" )@@ -56,6 +58,35 @@ }
defer f.Close() result, err = syntax.NewParser().Parse(f, "") return result, err +} + +func ZoaFmt(script string) { + f, err := os.Open(script) + if err != nil { + log.Fatal(err) + } + defer f.Close() + + p := syntax.NewParser() + syntax.KeepComments(true)(p) + syntax.Variant(syntax.LangPOSIX)(p) + node, err := p.Parse(f, "") + if err != nil { + return + } + var writeBuf bytes.Buffer + syntax.NewPrinter().Print(&writeBuf, node) + res := writeBuf.Bytes() + + info, err := os.Lstat(script) + if err != nil { + log.Fatal(err) + } + perm := info.Mode().Perm() + // TODO: remove maybeio when golang implements atomic writing + if err := maybeio.WriteFile(script, res, perm); err != nil { + log.Fatal(err) + } } // ZoaCopy copies a file from src to dst, optionally modifying its mode.
M
test/main
→
test/main
@@ -1,7 +1,7 @@
#!/bin/sh # test function inheretence println() { - printf "%s\n" "$1" + printf "%s\n" "$1" } # hewo@@ -9,17 +9,18 @@ uname -a
PASSTHROUGH=bananas # test waiting for output, printing a set var -sleep 2; printf '%s\n' "$PASSTHROUGH" +sleep 2 +printf '%s\n' "$PASSTHROUGH" # test if statements if [ "$(uname -n)" = "nostromo" ]; then - printf "%s\n" NOSTROMO + printf "%s\n" NOSTROMO fi -printf "%s\n" "$NODENAME" > /tmp/hi +printf "%s\n" "$NODENAME" >/tmp/hi # ensure that suggestions work in an if statement if true; then - echo hi + echo hi fi # test zoa cp