Skip to content

Commit

Permalink
👔 up: sysutil - SearchPath will limit ext search on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 23, 2024
1 parent c1f379f commit e7da787
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions sysutil/sysenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"syscall"

"github.com/gookit/goutil/internal/checkfn"
"github.com/gookit/goutil/internal/comfunc"
"golang.org/x/term"
)
Expand Down Expand Up @@ -144,6 +145,12 @@ func EnvPaths() []string {
return filepath.SplitList(os.Getenv("PATH"))
}

// SearchPathOption settings for SearchPath
type SearchPathOption struct {
// 限制的扩展名
LimitExt []string
}

// SearchPath search executable files in the system $PATH
//
// Usage:
Expand All @@ -154,6 +161,9 @@ func SearchPath(keywords string, limit int) []string {
ptn := "*" + keywords + "*"
list := make([]string, 0)

// if windows, will limit with .exe, .bat, .cmd
isWindows := IsWindows()
winExts := []string{".exe", ".bat", ".cmd"}
checked := make(map[string]bool)
for _, dir := range filepath.SplitList(path) {
// Unix shell semantics: path element "" means "."
Expand All @@ -169,10 +179,21 @@ func SearchPath(keywords string, limit int) []string {
checked[dir] = true
matches, err := filepath.Glob(filepath.Join(dir, ptn))
if err == nil && len(matches) > 0 {
list = append(list, matches...)
size := len(list)
if isWindows {
// if windows, will limit with .exe, .bat, .cmd
for _, fPath := range matches {
fExt := filepath.Ext(fPath)
if checkfn.StringsContains(winExts, fExt) {
continue
}
list = append(list, fPath)
}
} else {
list = append(list, matches...)
}

// limit result size
size := len(list)
if limit > 0 && size >= limit {
list = list[:limit]
break
Expand Down

0 comments on commit e7da787

Please sign in to comment.