forked from twpayne/chezmoi
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: find[One]Executable in user-supplied paths
Implemented `findExecutable` with strong caching, extended from twpayne#3162. Also implemented `findOneExecutable` to search for any one executable from a list. Resolves: twpayne#3141 Closes: twpayne#3162 Co-authored-by: Arran Ubels <arran4@gmail.com>
- Loading branch information
1 parent
c96a4b7
commit 8cb3715
Showing
14 changed files
with
510 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* *path-list* | ||
|
||
`findExecutable` searches for an executable named *file* in directories | ||
identified by *path-list*. The result will be the executable file concatenated | ||
with the matching path. If an executable *file* cannot be found in *path-list*, | ||
`findExecutable` returns an empty string. | ||
|
||
`findExecutable` is provided as an alternative to | ||
[`lookPath`](/reference/templates/functions/lookPath) so that you can | ||
interrogate the system PATH as it would be configured after `chezmoi apply`. | ||
Like `lookPath`, `findExecutable` is not hermetic: its return value depends on | ||
the state of the filesystem at the moment the template is executed. Exercise | ||
caution when using it in your templates. | ||
|
||
The return value of the first successful call to `findExecutable` is cached, and | ||
future calls to `findExecutable` with the same parameters will return this path. | ||
|
||
!!! info | ||
|
||
On Windows, the resulting path will contain the first found executable | ||
extension as identified by the environment variable `%PathExt%`. | ||
|
||
!!! example | ||
|
||
``` | ||
{{ if findExecutable "rtx" (list "bin" "go/bin" ".cargo/bin" ".local/bin") }} | ||
# $HOME/.cargo/bin/rtx exists and will probably be in $PATH after apply | ||
{{ end }} | ||
``` |
32 changes: 32 additions & 0 deletions
32
assets/chezmoi.io/docs/reference/templates/functions/findOneExecutable.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,32 @@ | ||
# `findOneExecutable` *file-list* *path-list* | ||
|
||
`findOneExecutable` searches for an executable from *file-list* in directories | ||
identified by *path-list*, finding the first matching executable in the first | ||
matching directory (each directory is searched for matching executables in | ||
turn). The result will be the executable file concatenated with the matching | ||
path. If an executable from *file-list* cannot be found in *path-list*, | ||
`findOneExecutable` returns an empty string. | ||
|
||
`findOneExecutable` is provided as an alternative to | ||
[`lookPath`](/reference/templates/functions/lookPath) so that you can | ||
interrogate the system PATH as it would be configured after `chezmoi apply`. | ||
Like `lookPath`, `findOneExecutable` is not hermetic: its return value depends | ||
on the state of the filesystem at the moment the template is executed. Exercise | ||
caution when using it in your templates. | ||
|
||
The return value of the first successful call to `findOneExecutable` is cached, | ||
and future calls to `findOneExecutable` with the same parameters will return | ||
this path. | ||
|
||
!!! info | ||
|
||
On Windows, the resulting path will contain the first found executable | ||
extension as identified by the environment variable `%PathExt%`. | ||
|
||
!!! example | ||
|
||
``` | ||
{{ if findOneExecutable (list "eza" "exa") (list "bin" "go/bin" ".cargo/bin" ".local/bin") }} | ||
# $HOME/.cargo/bin/exa exists and will probably be in $PATH after apply | ||
{{ 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,66 @@ | ||
package chezmoi | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
var ( | ||
foundExecutableCacheMutex sync.Mutex | ||
foundExecutableCache = make(map[string]string) | ||
) | ||
|
||
// FindExecutable is like LookPath except that: | ||
// | ||
// - You can specify the needle as `string`, `[]string`, or `[]interface{}` | ||
// (that converts to `[]string`). | ||
// - You specify the haystack instead of relying on `$PATH`/`%PATH%`. | ||
// | ||
// This makes it useful for the resulting path of shell configurations | ||
// managed by chezmoi. | ||
func FindExecutable(files, paths []string) (string, error) { | ||
foundExecutableCacheMutex.Lock() | ||
defer foundExecutableCacheMutex.Unlock() | ||
|
||
key := strings.Join(files, "\x00") + "\x01" + strings.Join(paths, "\x00") | ||
|
||
if path, ok := foundExecutableCache[key]; ok { | ||
return path, nil | ||
} | ||
|
||
var candidates []string | ||
|
||
for _, file := range files { | ||
candidates = append(candidates, findExecutableExtensions(file)...) | ||
} | ||
|
||
// based on /usr/lib/go-1.20/src/os/exec/lp_unix.go:52 | ||
for _, candidatePath := range paths { | ||
if candidatePath == "" { | ||
continue | ||
} | ||
|
||
for _, candidate := range candidates { | ||
path := filepath.Join(candidatePath, candidate) | ||
|
||
info, err := os.Stat(path) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
// isExecutable doesn't care if it's a directory | ||
if info.Mode().IsDir() { | ||
continue | ||
} | ||
|
||
if IsExecutable(info) { | ||
foundExecutableCache[key] = path | ||
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,62 @@ | ||
package chezmoi | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
) | ||
|
||
func TestFindExecutable(t *testing.T) { | ||
tests := []struct { | ||
files []string | ||
paths []string | ||
expected string | ||
}{ | ||
{ | ||
files: []string{"sh"}, | ||
paths: []string{"/usr/bin", "/bin"}, | ||
expected: "/bin/sh", | ||
}, | ||
{ | ||
files: []string{"sh"}, | ||
paths: []string{"/bin", "/usr/bin"}, | ||
expected: "/bin/sh", | ||
}, | ||
{ | ||
files: []string{"chezmoish"}, | ||
paths: []string{"/bin", "/usr/bin"}, | ||
expected: "", | ||
}, | ||
|
||
{ | ||
files: []string{"chezmoish", "sh"}, | ||
paths: []string{"/usr/bin", "/bin"}, | ||
expected: "/bin/sh", | ||
}, | ||
{ | ||
files: []string{"chezmoish", "sh"}, | ||
paths: []string{"/bin", "/usr/bin"}, | ||
expected: "/bin/sh", | ||
}, | ||
{ | ||
files: []string{"chezmoish", "chezvoush"}, | ||
paths: []string{"/bin", "/usr/bin"}, | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
name := fmt.Sprintf( | ||
"FindExecutable %#v in %#v as %#v", | ||
test.files, | ||
test.paths, | ||
test.expected, | ||
) | ||
t.Run(name, func(t *testing.T) { | ||
actual, err := FindExecutable(test.files, test.paths) | ||
assert.NoError(t, err) | ||
assert.Equal(t, test.expected, actual) | ||
}) | ||
} | ||
} |
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,63 @@ | ||
//go:build !windows && !darwin | ||
|
||
package chezmoi | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
) | ||
|
||
func TestFindExecutable(t *testing.T) { | ||
tests := []struct { | ||
files []string | ||
paths []string | ||
expected string | ||
}{ | ||
{ | ||
files: []string{"yes"}, | ||
paths: []string{"/usr/bin", "/bin"}, | ||
expected: "/usr/bin/yes", | ||
}, | ||
{ | ||
files: []string{"sh"}, | ||
paths: []string{"/bin", "/usr/bin"}, | ||
expected: "/bin/sh", | ||
}, | ||
{ | ||
files: []string{"chezmoish"}, | ||
paths: []string{"/bin", "/usr/bin"}, | ||
expected: "", | ||
}, | ||
{ | ||
files: []string{"chezmoish", "yes"}, | ||
paths: []string{"/usr/bin", "/bin"}, | ||
expected: "/usr/bin/yes", | ||
}, | ||
{ | ||
files: []string{"chezmoish", "sh"}, | ||
paths: []string{"/bin", "/usr/bin"}, | ||
expected: "/bin/sh", | ||
}, | ||
{ | ||
files: []string{"chezmoish", "chezvoush"}, | ||
paths: []string{"/bin", "/usr/bin"}, | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
name := fmt.Sprintf( | ||
"FindExecutable %#v in %#v as %#v", | ||
test.files, | ||
test.paths, | ||
test.expected, | ||
) | ||
t.Run(name, func(t *testing.T) { | ||
actual, err := FindExecutable(test.files, test.paths) | ||
assert.NoError(t, err) | ||
assert.Equal(t, test.expected, actual) | ||
}) | ||
} | ||
} |
Oops, something went wrong.