From 6a5fe7f348d20e55d2965c99a19ab07c82b1f8c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Ch=C3=A1vez?= Date: Sat, 30 Sep 2023 23:49:47 +0200 Subject: [PATCH] chore: changes the flag from include to restrict to express the intention. --- cmd/porto/main.go | 29 ++++++++++------------------- import.go | 6 +++--- import_test.go | 10 +++++----- 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/cmd/porto/main.go b/cmd/porto/main.go index 648bf36..e2a4a81 100644 --- a/cmd/porto/main.go +++ b/cmd/porto/main.go @@ -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] - -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 @@ -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) } @@ -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 diff --git a/import.go b/import.go index 7156762..385dc8c 100644 --- a/import.go +++ b/import.go @@ -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) } @@ -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 diff --git a/import_test.go b/import_test.go index d47815b..d41f2aa 100644 --- a/import_test.go +++ b/import_test.go @@ -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`)}, }, ) @@ -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`)}, }, )