feat: brain/index.go shebang; mail import is not a brain write (D14). (#12)
Commands live at bin/brain/{index,get,stats,eval,watch}.go and
bin/mail/import.go, bin/markdown/import.go, bin/postgres/query.go.
Python remains the Ladybug write worker. index_mail is a deprecation
shim that rebuilds via --with-mail.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package cmdbin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Root is the 2dph checkout (KB_ROOT, or walk up for .git / var).
|
||||
func Root() string {
|
||||
if v := os.Getenv("KB_ROOT"); v != "" {
|
||||
return v
|
||||
}
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "."
|
||||
}
|
||||
for i := 0; i < 10; i++ {
|
||||
if _, err := os.Stat(filepath.Join(wd, ".git")); err == nil {
|
||||
return wd
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(wd, "var")); err == nil {
|
||||
return wd
|
||||
}
|
||||
parent := filepath.Dir(wd)
|
||||
if parent == wd {
|
||||
break
|
||||
}
|
||||
wd = parent
|
||||
}
|
||||
return "."
|
||||
}
|
||||
|
||||
// ExecFile runs repo-relative path (python/bash shebang scripts) with stdio.
|
||||
func ExecFile(rel string, args []string) int {
|
||||
path := filepath.Join(Root(), filepath.FromSlash(rel))
|
||||
cmd := exec.Command(path, args...)
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Dir = Root()
|
||||
if err := cmd.Run(); err != nil {
|
||||
if ee, ok := err.(*exec.ExitError); ok {
|
||||
return ee.ExitCode()
|
||||
}
|
||||
if errors.Is(err, os.ErrNotExist) || strings.Contains(err.Error(), "no such file") {
|
||||
return 127
|
||||
}
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package cmdbin
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRootHonorsKBROOT(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
t.Setenv("KB_ROOT", dir)
|
||||
if got := Root(); got != dir {
|
||||
t.Fatalf("Root() = %q, want %q", got, dir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecFileMissingIs127(t *testing.T) {
|
||||
t.Setenv("KB_ROOT", t.TempDir())
|
||||
if code := ExecFile("no/such-tool", nil); code != 127 {
|
||||
t.Fatalf("exit = %d, want 127", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecFileRuns(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
script := filepath.Join(root, "echo.sh")
|
||||
if err := os.WriteFile(script, []byte("#!/bin/sh\nexit 3\n"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("KB_ROOT", root)
|
||||
if code := ExecFile("echo.sh", nil); code != 3 {
|
||||
t.Fatalf("exit = %d, want 3", code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user