Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Exposes isExecutable to the user #3153

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `isExecutable` *file*

`isExecutable` returns true if a file is executable.

!!! example

```
{{ if isExecutable "/bin/echo" }}
`sh` is executable
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be echo is executable. You should also remove the backticks around sh as this is text or shell, not Markdown.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you're correct on both accounts. I would fix it but I think I missed the boat on that.

{{ end }}
```
1 change: 1 addition & 0 deletions assets/chezmoi.io/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ nav:
- include: reference/templates/functions/include.md
- includeTemplate: reference/templates/functions/includeTemplate.md
- ioreg: reference/templates/functions/ioreg.md
- isExecutable: reference/templates/functions/isExecutable.md
- joinPath: reference/templates/functions/joinPath.md
- jq: reference/templates/functions/jq.md
- lookPath: reference/templates/functions/lookPath.md
Expand Down
4 changes: 2 additions & 2 deletions internal/chezmoi/chezmoi_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func init() {
unix.Umask(int(Umask))
}

// isExecutable returns if fileInfo is executable.
func isExecutable(fileInfo fs.FileInfo) bool {
// IsExecutable returns if fileInfo is executable.
func IsExecutable(fileInfo fs.FileInfo) bool {
return fileInfo.Mode().Perm()&0o111 != 0
}

Expand Down
24 changes: 21 additions & 3 deletions internal/chezmoi/chezmoi_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@ package chezmoi

import (
"io/fs"
"os"
"path/filepath"
"strings"

"golang.org/x/exp/slices"
)

const nativeLineEnding = "\r\n"

// isExecutable returns false on Windows.
func isExecutable(fileInfo fs.FileInfo) bool {
return false
var pathExts = strings.Split(os.Getenv("PATHEXT"), string(filepath.ListSeparator))

// IsExecutable checks if the file is a regular file and has an extension listed
// in the PATHEXT environment variable as per
// https://www.nextofwindows.com/what-is-pathext-environment-variable-in-windows.
func IsExecutable(fileInfo fs.FileInfo) bool {
if !fileInfo.Mode().IsRegular() {
return false
}
ext := filepath.Ext(fileInfo.Name())
if ext == "" {
return false
}
return slices.ContainsFunc(pathExts, func(pathExt string) bool {
return strings.EqualFold(pathExt, ext)
})
}

// isPrivate returns false on Windows.
Expand Down
8 changes: 4 additions & 4 deletions internal/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2092,7 +2092,7 @@ func (s *SourceState) newSourceStateFileEntryFromFile(
fileAttr := FileAttr{
TargetName: fileInfo.Name(),
Encrypted: options.Encrypt,
Executable: isExecutable(fileInfo),
Executable: IsExecutable(fileInfo),
Private: isPrivate(fileInfo),
ReadOnly: isReadOnly(fileInfo),
Template: options.Template,
Expand Down Expand Up @@ -2316,7 +2316,7 @@ func (s *SourceState) readExternalArchive(
TargetName: fileInfo.Name(),
Type: SourceFileTypeFile,
Empty: fileInfo.Size() == 0,
Executable: isExecutable(fileInfo),
Executable: IsExecutable(fileInfo),
Private: isPrivate(fileInfo),
ReadOnly: isReadOnly(fileInfo),
}
Expand Down Expand Up @@ -2448,7 +2448,7 @@ func (s *SourceState) readExternalArchiveFile(
TargetName: fileInfo.Name(),
Type: SourceFileTypeFile,
Empty: fileInfo.Size() == 0,
Executable: isExecutable(fileInfo) || external.Executable,
Executable: IsExecutable(fileInfo) || external.Executable,
Private: isPrivate(fileInfo),
ReadOnly: isReadOnly(fileInfo),
}
Expand Down Expand Up @@ -2530,7 +2530,7 @@ func (s *SourceState) readExternalDir(
TargetName: fileInfo.Name(),
Type: SourceFileTypeFile,
Empty: true,
Executable: isExecutable(fileInfo),
Executable: IsExecutable(fileInfo),
Private: isPrivate(fileInfo),
ReadOnly: isReadOnly(fileInfo),
}
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ func newConfig(options ...configOption) (*Config, error) {
"include": c.includeTemplateFunc,
"includeTemplate": c.includeTemplateTemplateFunc,
"ioreg": c.ioregTemplateFunc,
"isExecutable": c.isExecutableTemplateFunc,
"joinPath": c.joinPathTemplateFunc,
"jq": c.jqTemplateFunc,
"keepassxc": c.keepassxcTemplateFunc,
Expand Down
12 changes: 12 additions & 0 deletions internal/cmd/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,18 @@ func (c *Config) lookPathTemplateFunc(file string) string {
}
}

func (c *Config) isExecutableTemplateFunc(file string) bool {
fileInfo, err := c.fileSystem.Stat(file)
switch {
case err == nil:
return chezmoi.IsExecutable(fileInfo)
case errors.Is(err, fs.ErrNotExist):
return false
default:
panic(err)
}
}

func (c *Config) lstatTemplateFunc(name string) any {
switch fileInfo, err := c.fileSystem.Lstat(name); {
case err == nil:
Expand Down
8 changes: 8 additions & 0 deletions internal/cmd/testdata/scripts/templatefuncs.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ stdout a${/}b
exec chezmoi execute-template '{{ dict "key" "value" | jq ".key" | first }}'
stdout ^value$

# test isExecutable template function positive test case
[!windows] exec chezmoi execute-template '{{ if isExecutable "/bin/echo" }}executable{{end}}'
[!windows] stdout ^executable$

# test isExecutable template function negative test case
[!windows] exec chezmoi execute-template '{{ if isExecutable "/etc/fstat" }}executable{{end}}'
[!windows] stdout ^$

# test lookPath template function to find in PATH
exec chezmoi execute-template '{{ lookPath "go" }}'
stdout go$exe
Expand Down