Skip to content

Commit

Permalink
feat: crawling path script dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
d-led committed Jan 4, 2024
1 parent d16491f commit c8d6dfb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
10 changes: 10 additions & 0 deletions common/nix_candidate_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func NewNixCandidateSource(fs Filesystem, key string) CandidateSource {
}
res.crawlKnownPaths()
res.crawlPathLists()
res.crawlPathScriptDirs()
return res
}

Expand Down Expand Up @@ -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 {

Expand Down
51 changes: 51 additions & 0 deletions common/script_list_source.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit c8d6dfb

Please sign in to comment.