Skip to content

Commit

Permalink
chore: adds --include-files flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs committed Sep 20, 2023
1 parent 571fdb7 commit 3ed885d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
20 changes: 14 additions & 6 deletions cmd/porto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func main() {
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")
flag.Parse()

baseDir := flag.Arg(0)
Expand All @@ -32,6 +33,7 @@ Options:
--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:
Expand All @@ -53,7 +55,12 @@ Add import path to a folder

skipFilesRegex, err := porto.GetRegexpList(*flagSkipFiles)
if err != nil {
log.Fatalf("failed to build files regexes: %v", err)
log.Fatalf("failed to build files regexes to exclude: %v", err)
}

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

var skipDirsRegex []*regexp.Regexp
Expand All @@ -67,11 +74,12 @@ Add import path to a folder
skipDirsRegex = append(skipDirsRegex, userSkipDirsRegex...)

diffCount, err := porto.FindAndAddVanityImportForDir(workingDir, baseAbsDir, porto.Options{
WriteResultToFile: *flagWriteOutputToFile,
ListDiffFiles: *flagListDiff,
SkipFilesRegexes: skipFilesRegex,
SkipDirsRegexes: skipDirsRegex,
IncludeInternal: *flagIncludeInternal,
WriteResultToFile: *flagWriteOutputToFile,
ListDiffFiles: *flagListDiff,
SkipFilesRegexes: skipFilesRegex,
SkipDirsRegexes: skipDirsRegex,
IncludeInternal: *flagIncludeInternal,
IncludeFilesRegexes: includeFilesRegex,
})
if err != nil {
log.Fatal(err)
Expand Down
14 changes: 8 additions & 6 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -56,7 +56,7 @@ func addImportPath(absFilepath string, module string) (bool, []byte, error) {
return false, nil, errGenerated
}

content, err := ioutil.ReadFile(absFilepath)
content, err := os.ReadFile(absFilepath)

Check failure on line 59 in import.go

View workflow job for this annotation

GitHub Actions / test (1.13)

undefined: os.ReadFile
if err != nil {
return false, nil, fmt.Errorf("failed to parse the file %q: %v", absFilepath, err)
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func findAndAddVanityImportForModuleDir(workingDir, absDir string, moduleName st
return 0, nil
}

files, err := ioutil.ReadDir(absDir)
files, err := os.ReadDir(absDir)
if err != nil {
return 0, fmt.Errorf("failed to read the content of %q: %v", absDir, err)
}
Expand Down Expand Up @@ -127,7 +127,7 @@ func findAndAddVanityImportForModuleDir(workingDir, absDir string, moduleName st
}

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

hasChanged, newContent, err := addImportPath(absDir+pathSeparator+fileName, moduleName)
Expand Down Expand Up @@ -181,7 +181,7 @@ func matchesAny(regexes []*regexp.Regexp, str string) bool {
}

func findAndAddVanityImportForNonModuleDir(workingDir, absDir string, opts Options) (int, error) {
files, err := ioutil.ReadDir(absDir)
files, err := os.ReadDir(absDir)
if err != nil {
return 0, fmt.Errorf("failed to read %q: %v", absDir, err)
}
Expand Down Expand Up @@ -231,6 +231,8 @@ type Options struct {
SkipDirsRegexes []*regexp.Regexp
// Include internal packages
IncludeInternal bool
// Set of regex for matching files to be included
IncludeFilesRegexes []*regexp.Regexp
}

// FindAndAddVanityImportForDir scans all files in a folder and based on go.mod files
Expand All @@ -240,7 +242,7 @@ func FindAndAddVanityImportForDir(workingDir, absDir string, opts Options) (int,
return findAndAddVanityImportForModuleDir(workingDir, absDir, moduleName, opts)
}

files, err := ioutil.ReadDir(absDir)
files, err := os.ReadDir(absDir)
if err != nil {
return 0, fmt.Errorf("failed to read the content of %q: %v", absDir, err)
}
Expand Down

0 comments on commit 3ed885d

Please sign in to comment.