Skip to content

Commit 64600d0

Browse files
committed
feat: crawl /etc/paths.d for more sources
1 parent 573d565 commit 64600d0

File tree

3 files changed

+73
-24
lines changed

3 files changed

+73
-24
lines changed

common/nix_candidate_source.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func NewNixCandidateSource(fs Filesystem, key string) CandidateSource {
2525
sources: map[string]*PathSetIn{},
2626
key: key,
2727
}
28-
res.precompute()
28+
res.crawlKnownPaths()
29+
res.crawlPathLists()
2930
return res
3031
}
3132

@@ -37,7 +38,7 @@ func (s *NixCandidateSource) WhereSet(somePath string) *PathSetIn {
3738
return s.sources[normalizedPath]
3839
}
3940

40-
func (s *NixCandidateSource) precompute() {
41+
func (s *NixCandidateSource) crawlKnownPaths() {
4142
ForEachKnownPath(func(originalSource, expandedSource string) {
4243
// try getting the contents
4344
input, err := readAllText(expandedSource)
@@ -58,23 +59,7 @@ func (s *NixCandidateSource) precompute() {
5859
if s.key == x.Name.Value {
5960
harvestedPaths := s.harvestPaths(input[x.Value.Pos().Offset():x.Value.End().Offset()])
6061
for _, harvestedPath := range harvestedPaths {
61-
foundNormalizedPath := s.fs.GetAbsolutePath(harvestedPath)
62-
sourcesForPath := s.sources[foundNormalizedPath]
63-
if sourcesForPath == nil {
64-
sourcesForPath = &PathSetIn{
65-
What: Location{harvestedPath, foundNormalizedPath},
66-
WhereSet: []Location{},
67-
}
68-
}
69-
sourcesForPath.WhereSet = appendIfNotInSlice(
70-
sourcesForPath.WhereSet,
71-
Location{
72-
originalSource,
73-
expandedSource,
74-
}, func(a, b Location) bool {
75-
return a.Expanded == b.Expanded && a.Original == b.Original
76-
})
77-
s.sources[foundNormalizedPath] = sourcesForPath
62+
s.tryUpdatePathMap(harvestedPath, originalSource, expandedSource)
7863
}
7964
}
8065
}
@@ -83,6 +68,35 @@ func (s *NixCandidateSource) precompute() {
8368
})
8469
}
8570

71+
func (s *NixCandidateSource) tryUpdatePathMap(harvestedPath string, originalSource string, expandedSource string) {
72+
foundNormalizedPath := s.fs.GetAbsolutePath(harvestedPath)
73+
sourcesForPath := s.sources[foundNormalizedPath]
74+
if sourcesForPath == nil {
75+
sourcesForPath = &PathSetIn{
76+
What: Location{harvestedPath, foundNormalizedPath},
77+
WhereSet: []Location{},
78+
}
79+
}
80+
sourcesForPath.WhereSet = appendIfNotInSlice(
81+
sourcesForPath.WhereSet,
82+
Location{
83+
originalSource,
84+
expandedSource,
85+
}, func(a, b Location) bool {
86+
return a.Expanded == b.Expanded && a.Original == b.Original
87+
})
88+
s.sources[foundNormalizedPath] = sourcesForPath
89+
}
90+
91+
func (s *NixCandidateSource) crawlPathLists() {
92+
if runtime.GOOS == "windows" {
93+
return
94+
}
95+
ForEachPathsDPath(func(source, path string) {
96+
s.tryUpdatePathMap(path, source, source)
97+
})
98+
}
99+
86100
// input is some path definition
87101
func (s *NixCandidateSource) harvestPaths(input string) []string {
88102
input = strings.TrimSpace(input)

common/paths_f_source.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package common
2+
3+
import (
4+
"log"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
)
9+
10+
const pathListsDir = "/etc/paths.d/"
11+
12+
func ForEachPathsDPath(fn func(source string, path string)) {
13+
_ = filepath.Walk(pathListsDir,
14+
func(path string, info os.FileInfo, err error) error {
15+
if err != nil || info.IsDir() {
16+
return nil
17+
}
18+
input, err := readAllText(path)
19+
if err != nil {
20+
log.Print(err)
21+
return nil
22+
}
23+
24+
lines := strings.Split(string(input), "\n")
25+
for _, line := range lines {
26+
trimmedPath := strings.TrimSpace(line)
27+
if trimmedPath == "" {
28+
continue
29+
}
30+
fn(path, trimmedPath)
31+
}
32+
return nil
33+
},
34+
)
35+
}

common/results_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func Test_empty_results_cannot_be_summarized(t *testing.T) {
13-
sut := NewResultsCalculator(&mockFilesystem{}, &mockValueSource{})
13+
sut := NewCustomResultsCalculator(&mockFilesystem{}, &mockValueSource{}, &mockCandidateSource{})
1414
_, err := sut.CalculateResults()
1515
assert.Error(t, err)
1616
}
@@ -22,10 +22,10 @@ func Test_duplicates_have_correct_ids(t *testing.T) {
2222
mockFs.On("GetAbsolutePath", mock.Anything).Return("a")
2323
mockFs.On("PathStatus", mock.Anything).Return(true, true)
2424

25-
sut := NewResultsCalculator(mockFs, &mockValueSource{values_: []string{
25+
sut := NewCustomResultsCalculator(mockFs, &mockValueSource{values_: []string{
2626
"a",
2727
"a",
28-
}})
28+
}}, &mockCandidateSource{})
2929
rows, err := sut.CalculateResults()
3030
require.NoError(t, err)
3131
require.Len(t, rows, 2)
@@ -54,10 +54,10 @@ func Test_no_duplicates(t *testing.T) {
5454
mockFs.On("GetAbsolutePath", "b").Return("b")
5555
mockFs.On("PathStatus", mock.Anything).Return(true, true)
5656

57-
sut := NewResultsCalculator(mockFs, &mockValueSource{values_: []string{
57+
sut := NewCustomResultsCalculator(mockFs, &mockValueSource{values_: []string{
5858
"a",
5959
"b",
60-
}})
60+
}}, &mockCandidateSource{})
6161
rows, _ := sut.CalculateResults()
6262
assert.Empty(t, rows[0].Duplicates)
6363
assert.Empty(t, rows[1].Duplicates)

0 commit comments

Comments
 (0)