Skip to content

Commit c8d6dfb

Browse files
committed
feat: crawling path script dirs
1 parent d16491f commit c8d6dfb

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

common/nix_candidate_source.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func NewNixCandidateSource(fs Filesystem, key string) CandidateSource {
2626
}
2727
res.crawlKnownPaths()
2828
res.crawlPathLists()
29+
res.crawlPathScriptDirs()
2930
return res
3031
}
3132

@@ -111,6 +112,15 @@ func (s *NixCandidateSource) crawlPathLists() {
111112
})
112113
}
113114

115+
func (s *NixCandidateSource) crawlPathScriptDirs() {
116+
if runtime.GOOS == "windows" {
117+
return
118+
}
119+
ForEachScriptsDPath(func(script, expandedScript string) {
120+
s.crawlSource(script, expandedScript)
121+
})
122+
}
123+
114124
// input is some path definition
115125
func (s *NixCandidateSource) harvestPaths(input string) []string {
116126

common/script_list_source.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package common
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/mitchellh/go-homedir"
8+
)
9+
10+
var knownScriptDirs = []string{
11+
"/etc/profile.d/",
12+
}
13+
14+
var allowedExtensions = []string{
15+
".sh",
16+
}
17+
18+
func ForEachScriptsDPath(fn func(originalSource, expandedSource string)) {
19+
for _, knownScriptDir := range knownScriptDirs {
20+
_ = filepath.Walk(knownScriptDir,
21+
func(originalSource string, info os.FileInfo, err error) error {
22+
if err != nil || info.IsDir() || fileNotAllowed(originalSource) {
23+
return nil
24+
}
25+
26+
expanded, err := homedir.Expand(originalSource)
27+
if err != nil {
28+
return nil
29+
}
30+
31+
fn(originalSource, expanded)
32+
33+
return nil
34+
},
35+
)
36+
}
37+
}
38+
39+
func fileNotAllowed(p string) bool {
40+
return !extensionAllowed(p)
41+
}
42+
43+
func extensionAllowed(p string) bool {
44+
e := filepath.Ext(p)
45+
for _, allowedExt := range allowedExtensions {
46+
if allowedExt == e {
47+
return true
48+
}
49+
}
50+
return false
51+
}

0 commit comments

Comments
 (0)