Skip to content

Commit

Permalink
Merge pull request #19 from jcchavezs/adds_restrict_to_dirs
Browse files Browse the repository at this point in the history
feat: adds --restrict-to-dirs feature.
  • Loading branch information
jcchavezs committed Nov 9, 2023
2 parents 6709cc4 + cb2e4d6 commit 23b2a3f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
13 changes: 12 additions & 1 deletion cmd/porto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func main() {
flagSkipDefaultDirs := flag.Bool("skip-dirs-use-default", true, "Use default skip directory list")
flagIncludeInternal := flag.Bool("include-internal", false, "Include internal folders")
flagRestrictToFiles := flag.String("restrict-to-files", "", "Regexps of files to restrict the inspection on. It takes precedence over -skip-files")
flagRestrictToDirs := flag.String("restrict-to-dirs", "", "Regexps of dirs to restrict the inspection on. It takes precedence over -skip-dirs")
flag.Parse()

baseDir := flag.Arg(0)
Expand Down Expand Up @@ -54,6 +55,11 @@ Add import path to a folder
log.Fatalf("failed to build files regexes to include: %v", err)
}

restrictToDirsRegex, err := porto.GetRegexpList(*flagRestrictToDirs)
if err != nil {
log.Fatalf("failed to build dirs regexes to include: %v", err)
}

var skipDirsRegex = []*regexp.Regexp{}
if *flagSkipDefaultDirs {
skipDirsRegex = append(skipDirsRegex, porto.StdExcludeDirRegexps...)
Expand All @@ -68,7 +74,6 @@ Add import path to a folder
WriteResultToFile: *flagWriteOutputToFile,
ListDiffFiles: *flagListDiff,
IncludeInternal: *flagIncludeInternal,
SkipDirsRegexes: skipDirsRegex,
}

if len(restrictToFilesRegex) > 0 {
Expand All @@ -77,6 +82,12 @@ Add import path to a folder
opts.SkipFilesRegexes = skipFilesRegex
}

if len(restrictToFilesRegex) > 0 {
opts.RestrictToDirsRegexes = restrictToDirsRegex
} else {
opts.SkipDirsRegexes = skipDirsRegex
}

diffCount, err := porto.FindAndAddVanityImportForDir(workingDir, baseAbsDir, opts)
if err != nil {
log.Fatal(err)
Expand Down
40 changes: 27 additions & 13 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func isUnexportedModule(moduleName string, includeInternal bool) bool {
strings.HasSuffix(moduleName, "/internal"))
}

func findAndAddVanityImportForModuleDir(workingDir, absDir string, moduleName string, opts Options) (int, error) {
func findAndAddVanityImportForModuleDir(workingDir, baseAbsDir, absDir string, moduleName string, opts Options) (int, error) {
if isUnexportedModule(moduleName, opts.IncludeInternal) {
return 0, nil
}
Expand All @@ -107,25 +107,35 @@ func findAndAddVanityImportForModuleDir(workingDir, absDir string, moduleName st
gc := 0
for _, f := range files {
if isDir, dirName := f.IsDir(), f.Name(); isDir {
if matchesAny(opts.SkipDirsRegexes, dirName) {
continue
}

var (
c int
err error
)

relDir := ""
if baseAbsDir != absDir {
relDir, err = filepath.Rel(baseAbsDir, absDir)
if err != nil {
return 0, fmt.Errorf("failed to resolve relative path: %v", err)
}
relDir += pathSeparator
}

if isUnexportedDir(dirName, opts.IncludeInternal) {
continue
} else if len(opts.RestrictToDirsRegexes) > 0 && !matchesAny(opts.RestrictToDirsRegexes, relDir+dirName) {
continue
} else if len(opts.SkipDirsRegexes) > 0 && matchesAny(opts.SkipDirsRegexes, relDir+dirName) {
continue
} else if newModuleName, ok := findGoModule(absDir + pathSeparator + dirName); ok {
// if folder contains go.mod we use it from now on to build the vanity import
c, err = findAndAddVanityImportForModuleDir(workingDir, absDir+pathSeparator+dirName, newModuleName, opts)
c, err = findAndAddVanityImportForModuleDir(workingDir, baseAbsDir, absDir+pathSeparator+dirName, newModuleName, opts)
if err != nil {
return 0, err
}
} else {
// if not, we add the folder name to the vanity import
if c, err = findAndAddVanityImportForModuleDir(workingDir, absDir+pathSeparator+dirName, moduleName+"/"+dirName, opts); err != nil {
if c, err = findAndAddVanityImportForModuleDir(workingDir, baseAbsDir, absDir+pathSeparator+dirName, moduleName+"/"+dirName, opts); err != nil {
return 0, err
}
}
Expand Down Expand Up @@ -163,6 +173,8 @@ func findAndAddVanityImportForModuleDir(workingDir, absDir string, moduleName st
if err != nil {
return 0, fmt.Errorf("failed to resolve relative path: %v", err)
}
// TODO(jcchavezs): make this pluggable to allow different output formats
// and test assertions.
fmt.Printf("%s: missing right vanity import\n", relFilepath)
gc++
} else {
Expand Down Expand Up @@ -195,7 +207,7 @@ func matchesAny(regexes []*regexp.Regexp, str string) bool {
return false
}

func findAndAddVanityImportForNonModuleDir(workingDir, absDir string, opts Options) (int, error) {
func findAndAddVanityImportForNonModuleDir(workingDir, baseAbsDir, absDir string, opts Options) (int, error) {
files, err := os.ReadDir(absDir)
if err != nil {
return 0, fmt.Errorf("failed to read %q: %v", absDir, err)
Expand All @@ -219,11 +231,11 @@ func findAndAddVanityImportForNonModuleDir(workingDir, absDir string, opts Optio

absDirName := absDir + pathSeparator + dirName
if moduleName, ok := findGoModule(absDirName); ok {
if c, err = findAndAddVanityImportForModuleDir(workingDir, dirName, moduleName, opts); err != nil {
if c, err = findAndAddVanityImportForModuleDir(workingDir, baseAbsDir, dirName, moduleName, opts); err != nil {
return 0, err
}
} else {
if c, err = findAndAddVanityImportForNonModuleDir(workingDir, absDirName, opts); err != nil {
if c, err = findAndAddVanityImportForNonModuleDir(workingDir, baseAbsDir, absDirName, opts); err != nil {
return 0, err
}
}
Expand All @@ -248,13 +260,15 @@ type Options struct {
IncludeInternal bool
// Set of regex for matching files to be included
RestrictToFilesRegexes []*regexp.Regexp
// Set of regex for matching dirs to be included
RestrictToDirsRegexes []*regexp.Regexp
}

// FindAndAddVanityImportForDir scans all files in a folder and based on go.mod files
// encountered decides wether add a vanity import or not.
func FindAndAddVanityImportForDir(workingDir, absDir string, opts Options) (int, error) {
if moduleName, ok := findGoModule(absDir); ok {
return findAndAddVanityImportForModuleDir(workingDir, absDir, moduleName, opts)
return findAndAddVanityImportForModuleDir(workingDir, absDir, absDir, moduleName, opts)
}

files, err := os.ReadDir(absDir)
Expand All @@ -281,11 +295,11 @@ func FindAndAddVanityImportForDir(workingDir, absDir string, opts Options) (int,
)
absDirName := absDir + pathSeparator + dirName
if moduleName, ok := findGoModule(absDirName); ok {
if c, err = findAndAddVanityImportForModuleDir(workingDir, dirName, moduleName, opts); err != nil {
if c, err = findAndAddVanityImportForModuleDir(workingDir, absDir, dirName, moduleName, opts); err != nil {
return 0, err
}
} else {
if c, err = findAndAddVanityImportForNonModuleDir(workingDir, absDirName, opts); err != nil {
if c, err = findAndAddVanityImportForNonModuleDir(workingDir, absDir, absDirName, opts); err != nil {
return 0, err
}
}
Expand Down
24 changes: 23 additions & 1 deletion import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestFindFilesWithVanityImport(t *testing.T) {
c, err := findAndAddVanityImportForModuleDir(
cwd,
cwd+"/testdata/leftpad",
cwd+"/testdata/leftpad",
"github.com/jcchavezs/porto-integration-leftpad",
Options{
ListDiffFiles: true,
Expand All @@ -62,6 +63,7 @@ func TestFindFilesWithVanityImport(t *testing.T) {
c, err := findAndAddVanityImportForModuleDir(
cwd,
cwd+"/testdata/nopad",
cwd+"/testdata/nopad",
"github.com/jcchavezs/porto-integration/nopad",
Options{
ListDiffFiles: true,
Expand All @@ -76,6 +78,7 @@ func TestFindFilesWithVanityImport(t *testing.T) {
c, err := findAndAddVanityImportForModuleDir(
cwd,
cwd+"/testdata/leftpad",
cwd+"/testdata/leftpad",
"github.com/jcchavezs/porto-integration-leftpad",
Options{
ListDiffFiles: true,
Expand All @@ -91,6 +94,7 @@ func TestFindFilesWithVanityImport(t *testing.T) {
c, err := findAndAddVanityImportForModuleDir(
cwd,
cwd+"/testdata",
cwd+"/testdata",
"github.com/jcchavezs/porto/integration",
Options{
ListDiffFiles: true,
Expand All @@ -103,13 +107,14 @@ func TestFindFilesWithVanityImport(t *testing.T) {
)

require.NoError(t, err)
assert.Equal(t, 1, c)
assert.Equal(t, 2, c)
})

t.Run("restrict to files", func(t *testing.T) {
c, err := findAndAddVanityImportForModuleDir(
cwd,
cwd+"/testdata/leftpad",
cwd+"/testdata/leftpad",
"github.com/jcchavezs/porto-integration-leftpad",
Options{
ListDiffFiles: true,
Expand All @@ -121,10 +126,27 @@ func TestFindFilesWithVanityImport(t *testing.T) {
assert.Equal(t, 1, c)
})

t.Run("restrict to dir", func(t *testing.T) {
c, err := findAndAddVanityImportForModuleDir(
cwd,
cwd+"/testdata",
cwd+"/testdata",
"github.com/jcchavezs/porto/integration",
Options{
ListDiffFiles: true,
RestrictToDirsRegexes: []*regexp.Regexp{regexp.MustCompile(`^withoutgomod`)},
},
)

require.NoError(t, err)
assert.Equal(t, 2, c)
})

t.Run("skip and include file", func(t *testing.T) {
c, err := findAndAddVanityImportForModuleDir(
cwd,
cwd+"/testdata/leftpad",
cwd+"/testdata/leftpad",
"github.com/jcchavezs/porto-integration-leftpad",
Options{
ListDiffFiles: true,
Expand Down
1 change: 1 addition & 0 deletions testdata/withoutgomod/more/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package more

0 comments on commit 23b2a3f

Please sign in to comment.