Skip to content

Commit

Permalink
chore: changes the flag from include to restrict to express the inten…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
jcchavezs committed Sep 30, 2023
1 parent 4ac7c25 commit 6a5fe7f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
29 changes: 10 additions & 19 deletions cmd/porto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,20 @@ import (
)

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")
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")
flagIncludeFiles := flag.String("include-files", "", "Regexps of files to include")
flagSkipDefaultDirs := flag.Bool("skip-dirs-use-default", true, "Use default skip directory list")
flagIncludeInternal := flag.Bool("include-internal", false, "Include internal folders")
restrictToFiles := flag.String("restrict-to-files", "", "Regexps of files to restrict the inspection on. It takes precedence over -skip-files and -skip-dirs")
flag.Parse()

baseDir := flag.Arg(0)

if len(flag.Args()) == 0 {
flag.Usage()
fmt.Println(`
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
--skip-dirs Regexps of directories to skip
--skip-dirs-use-default Use default skip directory list (default: true)
--include-internal Include internal folders
--include-files Regexps of files to include. It takes precedence over --skip-files
Examples:
Add import path to a folder
Expand All @@ -58,7 +49,7 @@ Add import path to a folder
log.Fatalf("failed to build files regexes to exclude: %v", err)
}

includeFilesRegex, err := porto.GetRegexpList(*flagIncludeFiles)
restrictToFilesRegex, err := porto.GetRegexpList(*restrictToFiles)
if err != nil {
log.Fatalf("failed to build files regexes to include: %v", err)
}
Expand All @@ -79,8 +70,8 @@ Add import path to a folder
IncludeInternal: *flagIncludeInternal,
}

if len(includeFilesRegex) > 0 {
opts.IncludeFilesRegexes = includeFilesRegex
if len(restrictToFilesRegex) > 0 {
opts.RestrictToFilesRegexes = restrictToFilesRegex
} else {
opts.SkipFilesRegexes = skipFilesRegex
opts.SkipDirsRegexes = skipDirsRegex
Expand Down
6 changes: 3 additions & 3 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func findAndAddVanityImportForModuleDir(workingDir, absDir string, moduleName st
gc += c
} else if fileName := f.Name(); isGoFile(fileName) && !isGoTestFile(fileName) {
shouldEvaluate := true
if len(opts.IncludeFilesRegexes) > 0 {
shouldEvaluate = matchesAny(opts.IncludeFilesRegexes, fileName)
if len(opts.RestrictToFilesRegexes) > 0 {
shouldEvaluate = matchesAny(opts.RestrictToFilesRegexes, fileName)
} else if len(opts.SkipFilesRegexes) > 0 {
shouldEvaluate = !matchesAny(opts.SkipFilesRegexes, fileName)
}
Expand Down Expand Up @@ -243,7 +243,7 @@ type Options struct {
// Include internal packages
IncludeInternal bool
// Set of regex for matching files to be included
IncludeFilesRegexes []*regexp.Regexp
RestrictToFilesRegexes []*regexp.Regexp
}

// FindAndAddVanityImportForDir scans all files in a folder and based on go.mod files
Expand Down
10 changes: 5 additions & 5 deletions import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func TestFindFilesWithVanityImport(t *testing.T) {
cwd+"/testdata/leftpad",
"github.com/jcchavezs/porto-integration-leftpad",
Options{
ListDiffFiles: true,
IncludeFilesRegexes: []*regexp.Regexp{regexp.MustCompile(`other\.go`)},
ListDiffFiles: true,
RestrictToFilesRegexes: []*regexp.Regexp{regexp.MustCompile(`other\.go`)},
},
)

Expand All @@ -108,9 +108,9 @@ func TestFindFilesWithVanityImport(t *testing.T) {
cwd+"/testdata/leftpad",
"github.com/jcchavezs/porto-integration-leftpad",
Options{
ListDiffFiles: true,
IncludeFilesRegexes: []*regexp.Regexp{regexp.MustCompile(`other\.go`)},
SkipFilesRegexes: []*regexp.Regexp{regexp.MustCompile(`leftpad\.go`)},
ListDiffFiles: true,
RestrictToFilesRegexes: []*regexp.Regexp{regexp.MustCompile(`other\.go`)},
SkipFilesRegexes: []*regexp.Regexp{regexp.MustCompile(`leftpad\.go`)},
},
)

Expand Down

0 comments on commit 6a5fe7f

Please sign in to comment.