Skip to content

Commit

Permalink
Merge pull request #15 from mx-psi/mx-psi/skip-dirs
Browse files Browse the repository at this point in the history
Add options for skipping directories
  • Loading branch information
jcchavezs authored Oct 27, 2021
2 parents c81805b + 257a160 commit e0ba097
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ linters-settings:
lll:
line-length: 170
gocyclo:
min-complexity: 20
min-complexity: 25
golint:
min-confidence: 0.85
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,22 @@ If you just want to list the files that porto would change vanity import, run:
```bash
porto -l path/to/library
```
## Inclusion/exclusion rules

If you want to ignore files (e.g. proto generated files), pass the `--skip-files` flag:
`porto` skips autogenerated, internal, third party and vendored files by default. You can customize what files get included using some flags:

- If you want to ignore files (e.g. proto generated files), pass the `--skip-files` flag:

```bash
porto --skip-files ".*\\.pb\\.go$" path/to/library
```
- If you want to ignore directories (e.g. tooling directories), pass the `--skip-dirs` flag:

If you want to include `internal` folders too
```bash
porto --skip-dirs "^tools$" path/to/library
```
- If you want to include `internal` folders and directories skipped by default:

```bash
porto --include-internal path/to/library
porto --include-internal --skip-dirs-use-default=false path/to/library
```
24 changes: 20 additions & 4 deletions cmd/porto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"path/filepath"
"regexp"

"github.com/jcchavezs/porto"
)
Expand All @@ -14,6 +15,8 @@ func main() {
flagWriteOutputToFile := flag.Bool("w", false, "write result to (source) file instead of stdout")
flagListDiff := flag.Bool("l", false, "list files whose vanity import differs from porto's")
flagSkipFiles := flag.String("skip-files", "", "Regexps of files to skip")
flagSkipDirs := flag.String("skip-dirs", "", "Regexps of directories to skip")
flagSkipDefaultDirs := flag.Bool("skip-dirs-use-default", true, "use default skip directory list")
flagIncludeInternal := flag.Bool("include-internal", false, "include internal folders")
flag.Parse()

Expand All @@ -23,10 +26,12 @@ func main() {
usage: porto [options] <target-path>
Options:
-w Write result to (source) file instead of stdout (default: false)
-l List files whose vanity import differs from porto's (default: false)
--skip-files Regexps of files to skip
--include-internal Include internal folders
-w Write result to (source) file instead of stdout (default: false)
-l List files whose vanity import differs from porto's (default: false)
--skip-files Regexps of files to skip
--skip-dirs Regexps of directories to skip
--skip-dirs-use-default Use default skip directory list (default: true)
--include-internal Include internal folders
Examples:
Expand All @@ -51,10 +56,21 @@ Add import path to a folder
log.Fatalf("failed to build files regexes: %v", err)
}

var skipDirsRegex []*regexp.Regexp
if *flagSkipDefaultDirs {
skipDirsRegex = append(skipDirsRegex, porto.StdExcludeDirRegexps...)
}
userSkipDirsRegex, err := porto.GetRegexpList(*flagSkipDirs)
if err != nil {
log.Fatalf("failed to build directories regexes: %v", err)
}
skipDirsRegex = append(skipDirsRegex, userSkipDirsRegex...)

diffCount, err := porto.FindAndAddVanityImportForDir(workingDir, baseAbsDir, porto.Options{
WriteResultToFile: *flagWriteOutputToFile,
ListDiffFiles: *flagListDiff,
SkipFilesRegexes: skipFilesRegex,
SkipDirsRegexes: skipDirsRegex,
IncludeInternal: *flagIncludeInternal,
})
if err != nil {
Expand Down
12 changes: 7 additions & 5 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func findAndAddVanityImportForModuleDir(workingDir, absDir string, moduleName st

gc := 0
for _, f := range files {
if isDir, dirName := f.IsDir(), f.Name(); isDir {
if isDir, dirName := f.IsDir(), f.Name(); isDir && !matchesAny(opts.SkipDirsRegexes, dirName) {
var (
c int
err error
Expand All @@ -127,7 +127,7 @@ func findAndAddVanityImportForModuleDir(workingDir, absDir string, moduleName st
}

gc += c
} else if fileName := f.Name(); isGoFile(fileName) && !isGoTestFile(fileName) && !isIgnoredFile(opts.SkipFilesRegexes, fileName) {
} else if fileName := f.Name(); isGoFile(fileName) && !isGoTestFile(fileName) && !matchesAny(opts.SkipFilesRegexes, fileName) {
absFilepath := absDir + pathSeparator + fileName

hasChanged, newContent, err := addImportPath(absDir+pathSeparator+fileName, moduleName)
Expand Down Expand Up @@ -170,9 +170,9 @@ func findAndAddVanityImportForModuleDir(workingDir, absDir string, moduleName st
return gc, nil
}

func isIgnoredFile(fileRegexes []*regexp.Regexp, filename string) bool {
for _, fr := range fileRegexes {
if matched := fr.MatchString(filename); matched {
func matchesAny(regexes []*regexp.Regexp, str string) bool {
for _, fr := range regexes {
if matched := fr.MatchString(str); matched {
return true
}
}
Expand Down Expand Up @@ -227,6 +227,8 @@ type Options struct {
ListDiffFiles bool
// Set of regex for matching files to be skipped
SkipFilesRegexes []*regexp.Regexp
// Set of regex for matching directories to be skipped
SkipDirsRegexes []*regexp.Regexp
// Include internal packages
IncludeInternal bool
}
Expand Down
14 changes: 11 additions & 3 deletions import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,30 @@ func TestFindFilesWithVanityImport(t *testing.T) {

}

func TestIsIgnoredFile(t *testing.T) {
func TestMatchesAny(t *testing.T) {
assert.True(
t,
isIgnoredFile(
matchesAny(
[]*regexp.Regexp{regexp.MustCompile(".*\\.pb\\.go$")},
"myfile.pb.go",
),
)

assert.False(
t,
isIgnoredFile(
matchesAny(
[]*regexp.Regexp{},
"myfile.pb.go",
),
)

assert.True(
t,
matchesAny(
[]*regexp.Regexp{regexp.MustCompile("^third_party$")},
"third_party",
),
)
}

func TestIsUnexportedModule(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions regexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ import (
"strings"
)

// StdExcludeDirRegexps is the standard directory exclusion list from golangci-lint.
// See https://github.com/golangci/golangci-lint/blob/master/pkg/packages/skip.go.
var StdExcludeDirRegexps = []*regexp.Regexp{
regexp.MustCompile("^vendor$"),
regexp.MustCompile("^third_party$"),
regexp.MustCompile("^testdata$"),
regexp.MustCompile("^examples$"),
regexp.MustCompile("^Godeps$"),
regexp.MustCompile("^builtin$"),
}

func GetRegexpList(regexps string) ([]*regexp.Regexp, error) {
var regexes []*regexp.Regexp
if len(regexps) > 0 {
Expand Down

0 comments on commit e0ba097

Please sign in to comment.