Allow specifying the paths to search in Which()

This commit is contained in:
Kovid Goyal 2023-02-20 17:56:39 +05:30
parent 97b9572bec
commit 06bfa671d9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -13,15 +13,18 @@ import (
var _ = fmt.Print
func Which(cmd string) string {
func Which(cmd string, paths ...string) string {
if strings.Contains(cmd, string(os.PathSeparator)) {
return ""
}
path := os.Getenv("PATH")
if path == "" {
return ""
if len(paths) == 0 {
path := os.Getenv("PATH")
if path == "" {
return ""
}
paths = strings.Split(path, string(os.PathListSeparator))
}
for _, dir := range strings.Split(path, string(os.PathListSeparator)) {
for _, dir := range paths {
q := filepath.Join(dir, cmd)
if unix.Access(q, unix.X_OK) == nil {
s, err := os.Stat(q)