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 4, 2023
1 parent 390547f commit db6183b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
recursiveArg = "recursive"
useCacheArg = "use-cache"
applyToGeneratedFiles = "apply-to-generated-files"
excludesArg = "excludes"

// Deprecated options
localArg = "local"
Expand All @@ -55,7 +56,7 @@ var (
)

var (
projectName, companyPkgPrefixes, output, importsOrder string
projectName, companyPkgPrefixes, output, importsOrder, excludes string

// Deprecated
localPkgPrefixes, filePath string
Expand All @@ -69,6 +70,13 @@ func init() {
"Deprecated. Put file name as an argument(last item) of command line.",
)

flag.StringVar(
&excludes,
excludesArg,
"",
"Exclude files or dirs, example: '.git/,proto/*.go'.",
)

flag.StringVar(
&projectName,
projectNameArg,
Expand Down Expand Up @@ -259,7 +267,7 @@ func main() {
close(deprecatedMessagesCh)

if _, ok := reviser.IsDir(originPath); ok {
err := reviser.NewSourceDir(originProjectName, originPath, *isRecursive).Fix(options...)
err := reviser.NewSourceDir(originProjectName, originPath, *isRecursive, excludes).Fix(options...)
if err != nil {
log.Fatalf("Failed to fix directory: %+v\n", err)
}
Expand Down
48 changes: 41 additions & 7 deletions reviser/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"path/filepath"
"strings"

"golang.org/x/exp/slices"
)
Expand All @@ -26,16 +27,35 @@ var (

// SourceDir to validate and fix import
type SourceDir struct {
projectName string
dir string
isRecursive bool
projectName string
dir string
isRecursive bool
excludePatterns []string // see filepath.Match
hasExcludes bool
}

func NewSourceDir(projectName string, path string, isRecursive bool) *SourceDir {
func NewSourceDir(projectName string, path string, isRecursive bool, excludes string) *SourceDir {
if path == recursivePath {
isRecursive = true
}
return &SourceDir{projectName: projectName, dir: path, isRecursive: isRecursive}
absPath, err := filepath.Abs(path)
patterns := strings.Split(excludes, ",")
if err == nil {

Check warning on line 43 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L41-L43

Added lines #L41 - L43 were not covered by tests
for i := 0; i < len(patterns); i++ {
patterns[i] = strings.TrimSpace(patterns[i])
if !filepath.IsAbs(patterns[i]) {
patterns[i] = filepath.Join(absPath, patterns[i])

Check warning on line 47 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L45-L47

Added lines #L45 - L47 were not covered by tests
}
}
}

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

Check warning on line 57 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L52-L57

Added lines #L52 - L57 were not covered by tests
}
}

func (d *SourceDir) Fix(options ...SourceFileOption) error {
Expand All @@ -44,7 +64,6 @@ func (d *SourceDir) Fix(options ...SourceFileOption) error {
if !ok {
return ErrPathIsNotDir
}

err := filepath.WalkDir(d.dir, d.walk(options...))
if err != nil {
return fmt.Errorf("failed to walk dif: %w", err)
Expand All @@ -58,7 +77,10 @@ func (d *SourceDir) walk(options ...SourceFileOption) fs.WalkDirFunc {
if !d.isRecursive && dirEntry.IsDir() && filepath.Base(d.dir) != dirEntry.Name() {
return filepath.SkipDir
}
if isGoFile(path) && !dirEntry.IsDir() {
if dirEntry.IsDir() && d.isExcluded(path) {
return filepath.SkipDir

Check warning on line 81 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L80-L81

Added lines #L80 - L81 were not covered by tests
}
if isGoFile(path) && !dirEntry.IsDir() && !d.isExcluded(path) {

Check warning on line 83 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L83

Added line #L83 was not covered by tests
content, hasChange, err := NewSourceFile(d.projectName, path).Fix(options...)
if err != nil {
return fmt.Errorf("failed to fix: %w", err)
Expand All @@ -73,6 +95,18 @@ func (d *SourceDir) walk(options ...SourceFileOption) fs.WalkDirFunc {
}
}

func (d *SourceDir) isExcluded(path string) bool {
if d.hasExcludes {

Check warning on line 99 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L99

Added line #L99 was not covered by tests
for _, pattern := range d.excludePatterns {
matched, err := filepath.Match(pattern, path)
if err == nil && matched {
return true

Check warning on line 103 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L101-L103

Added lines #L101 - L103 were not covered by tests
}
}
}
return false

Check warning on line 107 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L107

Added line #L107 was not covered by tests
}

func IsDir(path string) (string, bool) {
if path == recursivePath || slices.Contains(currentPaths, path) {
var err error
Expand Down

0 comments on commit db6183b

Please sign in to comment.