Skip to content

Commit

Permalink
chore: Tidy up implementation if isExecutable on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Aug 6, 2023
1 parent 8c9905c commit b1434c8
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions internal/chezmoi/chezmoi_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,28 @@ import (
"os"
"path/filepath"
"strings"

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

const nativeLineEnding = "\r\n"

var pathExt []string = nil
var pathExts = strings.Split(os.Getenv("PATHEXT"), string(filepath.ListSeparator))

// getPathExt a singleton that obtains the PathExt environment variable and splits it up using the OS `ListSeparator`
func getPathExt() []string {
if pathExt == nil {
pathExt = strings.Split(os.Getenv("PathExt"), string(filepath.ListSeparator))
}
return pathExt
}

// isExecutable checks if the file has an extension listed in the `PathExt` variable as per:
// https://www.nextofwindows.com/what-is-pathext-environment-variable-in-windows then checks to see if it's regular file
// 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 {
foundPathExt := false
cmdExt := filepath.Ext(fileInfo.Name())
if cmdExt != "" {
for _, ext := range getPathExt() {
if strings.EqualFold(cmdExt, ext) {
foundPathExt = true
break
}
}
if !fileInfo.Mode().IsRegular() {
return false
}
ext := filepath.Ext(fileInfo.Name())
if ext == "" {
return false
}
return foundPathExt && fileInfo.Mode().IsRegular()
return slices.ContainsFunc(pathExts, func(pathExt string) bool {
return strings.EqualFold(pathExt, ext)
})
}

// isPrivate returns false on Windows.
Expand Down

0 comments on commit b1434c8

Please sign in to comment.