Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --include-internal flag #11

Merged
merged 4 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ If you want to ignore files (e.g. proto generated files), pass the `--skip-files
```bash
porto --skip-files ".*\\.pb\\.go$" path/to/library
```

If you want to check `internal` folders too

```bash
porto --check-internal path/to/library
```
9 changes: 6 additions & 3 deletions cmd/porto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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")
flagCheckInternal := flag.Bool("check-internal", false, "check internal folders")
flag.Parse()

baseDir := flag.Arg(0)
Expand All @@ -24,9 +25,10 @@ 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
-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
--check-internal Check internal folders
mx-psi marked this conversation as resolved.
Show resolved Hide resolved

Examples:

Expand Down Expand Up @@ -61,6 +63,7 @@ Add import path to a folder
WriteResultToFile: *flagWriteOutputToFile,
ListDiffFiles: *flagListDiff,
SkipFilesRegexes: skipFilesRegex,
CheckInternal: *flagCheckInternal,
})
if err != nil {
log.Fatal(err)
Expand Down
9 changes: 4 additions & 5 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ func isGoTestFile(filename string) bool {
return strings.HasSuffix(filename, "_test.go")
}

// isUnexportedDir checks if a dirname is a known unexported directory. Notice, we also
// ignore "internal" because it is only exported inside the codebase. This situation can
// become optional in the future.
func isUnexportedDir(dirname string) bool {
return dirname == "testdata" || dirname == "internal"
// isUnexportedDir checks if a dirname is a known unexported directory.
// If checkInternal is false, we also ignore "internal".
func isUnexportedDir(dirname string, checkInternal bool) bool {
return dirname == "testdata" || (!checkInternal && dirname == "internal")
}

// writeContentToFile writes the content in bytes to a given file.
Expand Down
10 changes: 6 additions & 4 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func isUnexportedModule(moduleName string) bool {
}

func findAndAddVanityImportForModuleDir(workingDir, absDir string, moduleName string, opts Options) (int, error) {
if isUnexportedModule(moduleName) {
if !opts.CheckInternal && isUnexportedModule(moduleName) {
mx-psi marked this conversation as resolved.
Show resolved Hide resolved
return 0, nil
}

Expand All @@ -83,7 +83,7 @@ func findAndAddVanityImportForModuleDir(workingDir, absDir string, moduleName st
c int
err error
)
if isUnexportedDir(dirName) {
if isUnexportedDir(dirName, opts.CheckInternal) {
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
Expand Down Expand Up @@ -165,7 +165,7 @@ func findAndAddVanityImportForNonModuleDir(workingDir, absDir string, opts Optio
}

dirName := f.Name()
if isUnexportedDir(dirName) {
if isUnexportedDir(dirName, opts.CheckInternal) {
continue
}

Expand Down Expand Up @@ -199,6 +199,8 @@ type Options struct {
ListDiffFiles bool
// Set of regex for matching files to be skipped
SkipFilesRegexes []*regexp.Regexp
// Check internal files
mx-psi marked this conversation as resolved.
Show resolved Hide resolved
CheckInternal bool
}

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

dirName := f.Name()
if isUnexportedDir(dirName) {
if isUnexportedDir(dirName, opts.CheckInternal) {
continue
}

Expand Down