diff --git a/common/nix_candidate_source.go b/common/nix_candidate_source.go index c98253c..a4ba02f 100644 --- a/common/nix_candidate_source.go +++ b/common/nix_candidate_source.go @@ -26,6 +26,7 @@ func NewNixCandidateSource(fs Filesystem, key string) CandidateSource { } res.crawlKnownPaths() res.crawlPathLists() + res.crawlPathScriptDirs() return res } @@ -111,6 +112,15 @@ func (s *NixCandidateSource) crawlPathLists() { }) } +func (s *NixCandidateSource) crawlPathScriptDirs() { + if runtime.GOOS == "windows" { + return + } + ForEachScriptsDPath(func(script, expandedScript string) { + s.crawlSource(script, expandedScript) + }) +} + // input is some path definition func (s *NixCandidateSource) harvestPaths(input string) []string { diff --git a/common/script_list_source.go b/common/script_list_source.go new file mode 100644 index 0000000..b42d980 --- /dev/null +++ b/common/script_list_source.go @@ -0,0 +1,51 @@ +package common + +import ( + "os" + "path/filepath" + + "github.com/mitchellh/go-homedir" +) + +var knownScriptDirs = []string{ + "/etc/profile.d/", +} + +var allowedExtensions = []string{ + ".sh", +} + +func ForEachScriptsDPath(fn func(originalSource, expandedSource string)) { + for _, knownScriptDir := range knownScriptDirs { + _ = filepath.Walk(knownScriptDir, + func(originalSource string, info os.FileInfo, err error) error { + if err != nil || info.IsDir() || fileNotAllowed(originalSource) { + return nil + } + + expanded, err := homedir.Expand(originalSource) + if err != nil { + return nil + } + + fn(originalSource, expanded) + + return nil + }, + ) + } +} + +func fileNotAllowed(p string) bool { + return !extensionAllowed(p) +} + +func extensionAllowed(p string) bool { + e := filepath.Ext(p) + for _, allowedExt := range allowedExtensions { + if allowedExt == e { + return true + } + } + return false +}