diff --git a/cmd/devbox/main.go b/cmd/devbox/main.go index 1cb464f..0830696 100644 --- a/cmd/devbox/main.go +++ b/cmd/devbox/main.go @@ -2,6 +2,7 @@ package main import ( + "cmp" "context" "fmt" "net/http" @@ -779,7 +780,7 @@ func hookEditCmd() *cobra.Command { } _ = os.Chmod(path, 0o755) // ensure executable so the runner picks it up - editor := firstNonEmpty(os.Getenv("VISUAL"), os.Getenv("EDITOR")) + editor := cmp.Or(os.Getenv("VISUAL"), os.Getenv("EDITOR")) if editor == "" { fmt.Fprintf(out, "✏️ edit it: %s (set $EDITOR to open automatically)\n", path) return nil @@ -815,7 +816,7 @@ func hookListCmd() *cobra.Command { fmt.Fprintf(out, "hooks dir: %s\n", hookDir) for _, e := range hooks.AllEvents() { glyph := "·" - if isExecutable(filepath.Join(hookDir, e)) || isExecutable(filepath.Join(hookDir, e+".ps1")) { + if hooks.Executable(filepath.Join(hookDir, e)) || hooks.Executable(filepath.Join(hookDir, e+".ps1")) { glyph = "✅" } fmt.Fprintf(out, " %s %s\n", glyph, e) @@ -824,17 +825,3 @@ func hookListCmd() *cobra.Command { }, } } - -func isExecutable(path string) bool { - info, err := os.Stat(path) - return err == nil && !info.IsDir() && info.Mode().Perm()&0o111 != 0 -} - -func firstNonEmpty(vals ...string) string { - for _, v := range vals { - if v != "" { - return v - } - } - return "" -} diff --git a/internal/hooks/hooks.go b/internal/hooks/hooks.go index 98dbe76..8ec7d64 100644 --- a/internal/hooks/hooks.go +++ b/internal/hooks/hooks.go @@ -137,16 +137,18 @@ func (r *Runner) Run(event string, changed []string, snapshot string) error { // or an executable (bash). func (r *Runner) find(event string) (script, interp string, ok bool) { dir := Dir(r.Root) - if ps := filepath.Join(dir, event+".ps1"); executable(ps) { + if ps := filepath.Join(dir, event+".ps1"); Executable(ps) { return ps, "pwsh", true } - if base := filepath.Join(dir, event); executable(base) { + if base := filepath.Join(dir, event); Executable(base) { return base, "bash", true } return "", "", false } -func executable(path string) bool { +// Executable reports whether path is a runnable hook script (a non-dir file with +// an exec bit; on Windows the exec bit doesn't exist, so any file qualifies). +func Executable(path string) bool { info, err := os.Stat(path) if err != nil || info.IsDir() { return false