From 7e5e7e97f8fad3c13bc7f5684a3b5195ef59af3f Mon Sep 17 00:00:00 2001 From: Arpad Ryszka Date: Tue, 26 Aug 2025 05:01:56 +0200 Subject: [PATCH] wand exec fixes --- docreflect.gen.go | 2 +- notes.txt | 6 +++++- output.go | 2 ++ tools/tools.go | 16 ++++++++-------- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/docreflect.gen.go b/docreflect.gen.go index 12f9364..22e08ab 100644 --- a/docreflect.gen.go +++ b/docreflect.gen.go @@ -50,7 +50,7 @@ docreflect.Register("code.squareroundforest.org/arpio/wand/tools.ExecOptions.NoC docreflect.Register("code.squareroundforest.org/arpio/wand/tools.ExecOptions.PurgeCache", "") docreflect.Register("code.squareroundforest.org/arpio/wand/tools.Man", "\nfunc(out, commandDir)") docreflect.Register("code.squareroundforest.org/arpio/wand/tools.Markdown", "\nfunc(out, commandDir)") -docreflect.Register("code.squareroundforest.org/arpio/wand/tools.copyGomod", "\nfunc(fn, dst, src)") +docreflect.Register("code.squareroundforest.org/arpio/wand/tools.copyGomod", "\nfunc(mn, dst, src)") docreflect.Register("code.squareroundforest.org/arpio/wand/tools.execCommandDir", "\nfunc(out, commandDir, env)") docreflect.Register("code.squareroundforest.org/arpio/wand/tools.execInternal", "\nfunc(command, args)") docreflect.Register("code.squareroundforest.org/arpio/wand/tools.execTransparent", "\nfunc(command, args)") diff --git a/notes.txt b/notes.txt index 870483d..4302a6b 100644 --- a/notes.txt +++ b/notes.txt @@ -1,6 +1,10 @@ -fix go.mod file path in exec +speed up wand exec with smarter caching test starting from the most referenced file to the least referenced one run only the related test when testing a file +fix notation: .build/wand net/http.Get https://squareroundforest.org | less +fix wand exec: .build/wand -- 'func(a struct{Foo string}) string { return a.Foo }' --foo bar +allow in wand exec: .build/wand 'regexp.MustCompile("a").MatchString' aaa +considering that the possible input comes from limited sources, it may make sense to not escape just every dot in markdown reflect command diff --git a/output.go b/output.go index 29ef935..caa0d3b 100644 --- a/output.go +++ b/output.go @@ -27,6 +27,8 @@ func printOutput(w io.Writer, o []any) error { if _, err := fmt.Fprintln(w, oi); err != nil { return wraperr(err) } + + continue } t = unpack(t, reflect.Pointer) diff --git a/tools/tools.go b/tools/tools.go index 63e3ec3..95557c9 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -80,8 +80,8 @@ func splitFunction(function string) (pkg string, expression string, err error) { if len(sparts) == 1 { expression = sparts[0] } else { - pkg = strings.Join(append(gopath, sparts[0]), "/") expression = parts[len(parts)-1] + pkg = strings.Join(append(gopath, sparts[0]), "/") } return @@ -91,7 +91,7 @@ func functionHash(function string) (string, error) { h := fnv.New128() h.Write([]byte(function)) buf := bytes.NewBuffer(nil) - b64 := base64.NewEncoder(base64.URLEncoding, buf) + b64 := base64.NewEncoder(base64.RawURLEncoding, buf) if _, err := b64.Write(h.Sum(nil)); err != nil { return "", fmt.Errorf("failed to encode function: %w", err) } @@ -100,7 +100,7 @@ func functionHash(function string) (string, error) { return "", fmt.Errorf("failed to complete encoding of function: %w", err) } - return buf.String(), nil + return strings.TrimPrefix(buf.String(), "_"), nil } func findGomod(wd string) (string, bool) { @@ -120,7 +120,7 @@ func findGomod(wd string) (string, bool) { } } -func copyGomod(fn, dst, src string) error { +func copyGomod(mn, dst, src string) error { srcf, err := os.Open(src) if err != nil { return fmt.Errorf("failed to open file: %s; %w", src, err) @@ -142,7 +142,7 @@ func copyGomod(fn, dst, src string) error { ss := strings.Split(s, "\n") for i := range ss { if strings.HasPrefix(ss[i], "module ") { - ss[i] = fmt.Sprintf("module %s", fn) + ss[i] = fmt.Sprintf("module %s", mn) break } } @@ -218,7 +218,6 @@ func Exec(o ExecOptions, function string, args ...string) error { } goGet := func(pkg string) error { - println("go get", pkg) if err := execInternal("go get", pkg); err != nil { return fmt.Errorf("failed to get go module: %w", err) } @@ -233,7 +232,7 @@ func Exec(o ExecOptions, function string, args ...string) error { defer os.Chdir(wd) gomodPath, hasGomod := findGomod(wd) if hasGomod { - if err := copyGomod(expression, path.Join(functionDir, "go.mod"), gomodPath); err != nil { + if err := copyGomod(functionHash, path.Join(functionDir, "go.mod"), gomodPath); err != nil { return err } } else { @@ -242,7 +241,8 @@ func Exec(o ExecOptions, function string, args ...string) error { } } - if pkg != "" { + // non-robust way of avoiding importing standard library packages: + if strings.Contains(pkg, ".") { if err := goGet(pkg); err != nil { return err }