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
lookPathIn
As per: twpayne#3141
BREAKING CHANGE: `isExecutable` has major change for windows. It now does something
- Loading branch information
Showing
12 changed files
with
311 additions
and
2 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
assets/chezmoi.io/docs/reference/templates/functions/lookPathIn.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,39 @@ | ||
# `lookPathIn` *file* *paths* | ||
|
||
`lookPathIn` searches for an executable named *file* in the directories provided by | ||
the `paths` parameter using the standard OS way of separating the PATH environment | ||
variable. The result may be an absolute path or a path relative to the current directory. | ||
If *file* is not found, `lookPathIn` returns an empty string. | ||
|
||
If the OS is Windows `lookPathIn` will either: if there is an extension, check to see if | ||
the extension is specified in the `PathExt` environment variable. If there isn't an | ||
extension it will try each of the extensions specified in the `PathExt` environment | ||
variable in the order provided until it finds one. In either case if it doesn't `lookPathIn` | ||
moves onto the next path provided in the `paths` parameter. | ||
|
||
`lookPathIn` is provided as an alternative to `lookPath` so that you interrogate the | ||
paths as you would have them. | ||
|
||
Each successful lookup is cached based on the full path, and evaluated in the correct | ||
order each time to reduce `File Stat` operations. | ||
|
||
!!! example | ||
|
||
``` | ||
{{- $paths := list }} | ||
{{- $homeDir := .chezmoi.homeDir }} | ||
{{- range $_, $relPath := list "bin" "go/bin" ".cargo/bin" ".local/bin" }} | ||
{{ $path := joinPath $homeDir $relPath }} | ||
{{- if stat $path }} | ||
{{- $paths = mustAppend $paths $path }} | ||
{{- end }} | ||
{{- end }} | ||
{{- if $paths }} | ||
export PATH={{ toStrings $paths | join ":" }}:$PATH | ||
{{- end }} | ||
|
||
{{ if lookPath "less" $paths }} | ||
echo "Good news we have found 'less' on system at '{{ lookPath "less" $paths }}'!" | ||
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{}) | ||
) | ||
|
||
// LookPathIn 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 LookPathIn(file, 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 filepath.SplitList(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 TestLookPathIn(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
file string | ||
paths string | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Finds first", | ||
file: "sh", | ||
paths: "/usr/bin:/bin", | ||
want: "/bin/sh", | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := LookPathIn(tt.file, tt.paths) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("LookPathIn() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("LookPathIn() 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 TestLookPathIn(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
file string | ||
paths string | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Finds first", | ||
file: "sh", | ||
paths: "/usr/bin:/bin", | ||
want: "/usr/bin/sh", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Finds first 2", | ||
file: "sh", | ||
paths: "/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 := LookPathIn(tt.file, tt.paths) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("LookPathIn() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("LookPathIn() 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 TestLookPathIn(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
file string | ||
paths string | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Finds with extension", | ||
file: "powershell.exe", | ||
paths: "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: "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: "c:\\windows\\system32;c:\\windows\\system64;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0", | ||
want: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Fails to find without extension", | ||
file: "weakshell", | ||
paths: "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 := LookPathIn(tt.file, tt.paths) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("LookPathIn() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !strings.EqualFold(got, tt.want) { | ||
t.Errorf("LookPathIn() 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[windows] skip 'Unix only' | ||
|
||
# test lookPathIn template function to find in specified script - success | ||
exec chezmoi execute-template '{{ lookPathIn "echo" "/bin" }}' | ||
stdout ^/bin/echo$ | ||
|
||
# test lookPathIn template function to find in specified script - failure | ||
exec chezmoi execute-template '{{ lookPathIn "echo" "/lib" }}' | ||
stdout ^$ | ||
|
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,14 @@ | ||
[!windows] skip 'Windows only' | ||
|
||
# Couldn't figure out why this works locally but not in github actions | ||
# # test lookPathIn template function to find in specified script - success with extension | ||
# exec chezmoi execute-template '{{ lookPathIn "git.exe" "c:\\windows\\system32;c:\\windows\\system64;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0;C:\\Program Files\\Git\\cmd" }}' | ||
# stdout '^C:\\Program Files\\Git\\cmd\\git.exe$' | ||
# | ||
# # test lookPathIn template function to find in specified script - success without extension | ||
# exec chezmoi execute-template '{{ lookPathIn "git" "c:\\windows\\system32;c:\\windows\\system64;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0;C:\\Program Files\\Git\\cmd" }}' | ||
# stdout '^C:\\Program Files\\Git\\cmd\\git.exe$' | ||
# | ||
# # test lookPathIn template function to find in specified script - failure | ||
# exec chezmoi execute-template '{{ lookPathIn "asdf" "c:\\windows\\system32;c:\\windows\\system64;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0;C:\\Program Files\\Git\\cmd" }}' | ||
# stdout '^$' |