Skip to content

Commit

Permalink
feat: exclude files and dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
tk103331 committed Aug 16, 2023
1 parent 8c0ae30 commit 6eda96f
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions reviser/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,36 @@ type SourceDir struct {
dir string
isRecursive bool
excludePatterns []string // see filepath.Match
hasExcludes bool
}

func NewSourceDir(projectName string, path string, isRecursive bool, excludes string) *SourceDir {
if path == recursivePath {
isRecursive = true
}
patterns := make([]string, 0)
segs := strings.Split(excludes, ",")
for _, seg := range segs {
p := strings.TrimSpace(seg)
if p != "" {
if !filepath.IsAbs(p) {
p = filepath.Join(path, p)
// get the absolute path
absPath, err := filepath.Abs(path)
if err == nil {
segs := strings.Split(excludes, ",")
for _, seg := range segs {
p := strings.TrimSpace(seg)
if p != "" {
if !filepath.IsAbs(p) {
// resolve the absolute path
p = filepath.Join(absPath, p)
}
// Check pattern is well-formed.
if _, err = filepath.Match(p, ""); err == nil {
patterns = append(patterns, p)
}
}
patterns = append(patterns, p)
}
}

return &SourceDir{
projectName: projectName,
dir: path,
dir: absPath,
isRecursive: isRecursive,
excludePatterns: patterns,
hasExcludes: len(patterns) > 0,
}
}

Expand Down Expand Up @@ -97,12 +102,16 @@ func (d *SourceDir) walk(options ...SourceFileOption) fs.WalkDirFunc {
}

func (d *SourceDir) isExcluded(path string) bool {
if d.hasExcludes {
for _, pattern := range d.excludePatterns {
matched, err := filepath.Match(pattern, path)
if err == nil && matched {
return true
}
var absPath string
if filepath.IsAbs(path) {
absPath = path
} else {
absPath = filepath.Join(d.dir, path)

Check warning on line 109 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L109

Added line #L109 was not covered by tests
}
for _, pattern := range d.excludePatterns {
matched, err := filepath.Match(pattern, absPath)
if err == nil && matched {
return true
}
}
return false
Expand Down

0 comments on commit 6eda96f

Please sign in to comment.