refactor(M7): cmp.Or for editor env, share hooks.Executable (ponytail) ♻️
This commit is contained in:
parent
bf40d209c1
commit
6e3ddee1e6
2 changed files with 8 additions and 19 deletions
|
|
@ -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 ""
|
||||
}
|
||||
|
|
|
|||
|
|
@ -137,16 +137,18 @@ func (r *Runner) Run(event string, changed []string, snapshot string) error {
|
|||
// or an executable <event> (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
|
||||
|
|
|
|||
Loading…
Reference in a new issue