forked from twpayne/chezmoi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create
findExecutable
which searches multiple paths for an ex…
…ecutable of a particular name and returns the found path. As per: twpayne#3141
- Loading branch information
Showing
13 changed files
with
345 additions
and
1 deletion.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
assets/chezmoi.io/docs/reference/templates/functions/findExecutable.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# `findExecutable` *file* *...paths* | ||
|
||
`findExecutable` searches for an executable named *file* in the directories provided by | ||
the varargs `paths` parameter. In the case of Windows it will return the correct extension | ||
if none was provided based on `PathExt`. | ||
|
||
The input to `findExecutable` is flattened recursively. It can be a variable parameter array such as: | ||
``` | ||
{{ findExecutable "less" "bin" "go/bin" ".cargo/bin" ".local/bin" }} | ||
``` | ||
Or a slice: | ||
``` | ||
{{ findExecutable "less" (list "bin" "go/bin" ".cargo/bin" ".local/bin") }} | ||
``` | ||
|
||
`findExecutable` is provided as an alternative to `lookPath` so that you interrogate the | ||
paths as you would have them after deployment of the RC script. | ||
|
||
Each successful lookup is cached based on the full path, and evaluated in the correct | ||
order each time to reduce `File` `Stat` operations. | ||
|
||
!!! example | ||
|
||
``` | ||
{{ if findExecutable "less" "bin" "go/bin" ".cargo/bin" ".local/bin" }} | ||
echo "Good news we have found 'less' on system at '{{ findExecutable "less" "bin" "go/bin" ".cargo/bin" ".local/bin" }}'!" | ||
export DIFFTOOL=less | ||
{{ end }} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package chezmoi | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
) | ||
|
||
var ( | ||
foundExecutableCacheMutex sync.Mutex | ||
foundExecutableCache = make(map[string]struct{}) | ||
) | ||
|
||
// FindExecutable is like lookPath except that you can specify the paths rather than just using the current `$PATH`. This | ||
// makes it useful for the resulting path of rc/profile files. | ||
func FindExecutable(file string, paths ...string) (string, error) { | ||
foundExecutableCacheMutex.Lock() | ||
defer foundExecutableCacheMutex.Unlock() | ||
|
||
// stolen from: /usr/lib/go-1.20/src/os/exec/lp_unix.go:52 | ||
for _, dir := range paths { | ||
if dir == "" { | ||
continue | ||
} | ||
p := filepath.Join(dir, file) | ||
for _, path := range findExecutableExtensions(p) { | ||
if _, ok := foundExecutableCache[path]; ok { | ||
return path, nil | ||
} | ||
f, err := os.Stat(path) | ||
if err != nil { | ||
continue | ||
} | ||
m := f.Mode() | ||
// isExecutable doesn't care if it's a directory | ||
if m.IsDir() { | ||
continue | ||
} | ||
if IsExecutable(f) { | ||
foundExecutableCache[path] = struct{}{} | ||
return path, nil | ||
} | ||
} | ||
} | ||
|
||
return "", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//go:build darwin | ||
|
||
package chezmoi | ||
|
||
import "testing" | ||
|
||
func TestFindExecutable(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
file string | ||
paths []string | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Finds first", | ||
file: "sh", | ||
paths: []string{"/usr/bin", "/bin"}, | ||
want: "/bin/sh", | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := FindExecutable(tt.file, tt.paths...) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("FindExecutable() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("FindExecutable() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//go:build !windows && !darwin | ||
|
||
package chezmoi | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestFindExecutableVararg(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
file string | ||
paths []string | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Finds first", | ||
file: "sh", | ||
paths: []string{"/usr/bin", "/bin"}, | ||
want: "/usr/bin/sh", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Finds first 2", | ||
file: "sh", | ||
paths: []string{"/bin", "/usr/bin"}, | ||
want: "/bin/sh", | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.want != "" { | ||
if _, err := os.Stat(tt.want); err != nil { | ||
t.Skip("Alpine doesn't have a symlink for sh") | ||
} | ||
} | ||
got, err := FindExecutable(tt.file, tt.paths...) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("FindExecutable() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("FindExecutable() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//go:build windows | ||
|
||
package chezmoi | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestFindExecutable(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
file string | ||
paths []string | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Finds with extension", | ||
file: "powershell.exe", | ||
paths: []string{"c:\\windows\\system32", "c:\\windows\\system64", "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0"}, | ||
want: "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Finds without extension", | ||
file: "powershell", | ||
paths: []string{"c:\\windows\\system32", "c:\\windows\\system64", "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0"}, | ||
want: "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Fails to find with extension", | ||
file: "weakshell.exe", | ||
paths: []string{"c:\\windows\\system32", "c:\\windows\\system64", "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0"}, | ||
want: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Fails to find without extension", | ||
file: "weakshell", | ||
paths: []string{"c:\\windows\\system32", "c:\\windows\\system64", "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0"}, | ||
want: "", | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := FindExecutable(tt.file, tt.paths...) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("FindExecutable() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !strings.EqualFold(got, tt.want) { | ||
t.Errorf("FindExecutable() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.