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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cmp"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -779,7 +780,7 @@ func hookEditCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
_ = os.Chmod(path, 0o755) // ensure executable so the runner picks it up
|
_ = 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 == "" {
|
if editor == "" {
|
||||||
fmt.Fprintf(out, "✏️ edit it: %s (set $EDITOR to open automatically)\n", path)
|
fmt.Fprintf(out, "✏️ edit it: %s (set $EDITOR to open automatically)\n", path)
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -815,7 +816,7 @@ func hookListCmd() *cobra.Command {
|
||||||
fmt.Fprintf(out, "hooks dir: %s\n", hookDir)
|
fmt.Fprintf(out, "hooks dir: %s\n", hookDir)
|
||||||
for _, e := range hooks.AllEvents() {
|
for _, e := range hooks.AllEvents() {
|
||||||
glyph := "·"
|
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 = "✅"
|
glyph = "✅"
|
||||||
}
|
}
|
||||||
fmt.Fprintf(out, " %s %s\n", glyph, e)
|
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).
|
// or an executable <event> (bash).
|
||||||
func (r *Runner) find(event string) (script, interp string, ok bool) {
|
func (r *Runner) find(event string) (script, interp string, ok bool) {
|
||||||
dir := Dir(r.Root)
|
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
|
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 base, "bash", true
|
||||||
}
|
}
|
||||||
return "", "", false
|
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)
|
info, err := os.Stat(path)
|
||||||
if err != nil || info.IsDir() {
|
if err != nil || info.IsDir() {
|
||||||
return false
|
return false
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue