Skip to content

Commit

Permalink
Fix symlink handling
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Aug 9, 2022
1 parent bbea235 commit 1eadced
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
19 changes: 19 additions & 0 deletions cmd/pint/tests/0092_symlinks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
mkdir rules
mkdir rules/src
exec ln -s src rules/dst
exec ln -s rules linked

pint.ok -l debug --no-color lint rules linked rules/src/rule.yaml
! stdout .
cmp stderr stderr.txt

-- stderr.txt --
level=debug msg="File parsed" path=rules/src/rule.yaml rules=1
level=debug msg="Found recording rule" lines=4-5 path=rules/src/rule.yaml record=down
level=debug msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/src/rule.yaml rule=down
-- rules/src/rule.yaml --
groups:
- name: foo
rules:
- record: down
expr: up == 0
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.28.6

### Fixed

- Fixed symlink handling when running `pint lint`.

## v0.28.5

### Fixed
Expand Down
50 changes: 40 additions & 10 deletions internal/discovery/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"regexp"

"golang.org/x/exp/slices"
)

func NewGlobFinder(patterns []string, relaxed []*regexp.Regexp) GlobFinder {
Expand All @@ -29,18 +31,14 @@ func (f GlobFinder) Find() (entries []Entry, err error) {
}

for _, path := range matches {
s, err := os.Stat(path)
subpaths, err := findFiles(path)
if err != nil {
return nil, err
}
if s.IsDir() {
subpaths, err := walkDir(path)
if err != nil {
return nil, err
for _, subpath := range subpaths {
if !slices.Contains(paths, subpath) {
paths = append(paths, subpath)
}
paths = append(paths, subpaths...)
} else {
paths = append(paths, path)
}
}
}
Expand All @@ -65,18 +63,50 @@ func (f GlobFinder) Find() (entries []Entry, err error) {
return entries, nil
}

func findFiles(path string) (paths []string, err error) {
s, err := os.Stat(path)
if err != nil {
return nil, err
}

if s.IsDir() {
subpaths, err := walkDir(path)
if err != nil {
return nil, err
}
paths = append(paths, subpaths...)
} else {
paths = append(paths, path)
}

return paths, nil
}

func walkDir(dirname string) (paths []string, err error) {
err = filepath.WalkDir(dirname,
func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
// nolint: exhaustive
switch d.Type() {
case fs.ModeDir:
return nil
case fs.ModeSymlink:
dest, err := filepath.EvalSymlinks(path)
if err != nil {
return err
}
subpaths, err := findFiles(dest)
if err != nil {
return err
}
paths = append(paths, subpaths...)
default:
paths = append(paths, path)
}

paths = append(paths, path)
return nil
})

Expand Down

0 comments on commit 1eadced

Please sign in to comment.