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.
- Loading branch information
Showing
8 changed files
with
152 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,48 @@ | ||
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 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 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(file) | ||
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
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