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 all commits
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 include `internal` folders too

```bash
porto --include-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")
flagIncludeInternal := flag.Bool("include-internal", false, "include 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
--include-internal Include internal folders

Examples:

Expand Down Expand Up @@ -61,6 +63,7 @@ Add import path to a folder
WriteResultToFile: *flagWriteOutputToFile,
ListDiffFiles: *flagListDiff,
SkipFilesRegexes: skipFilesRegex,
IncludeInternal: *flagIncludeInternal,
})
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 includeInternal is false, we also ignore "internal".
func isUnexportedDir(dirname string, includeInternal bool) bool {
return dirname == "testdata" || (!includeInternal && dirname == "internal")
}

// writeContentToFile writes the content in bytes to a given file.
Expand Down
16 changes: 9 additions & 7 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ func addImportPath(absFilepath string, module string) (bool, []byte, error) {
return !bytes.Equal(content, newContent), newContent, nil
}

func isUnexportedModule(moduleName string) bool {
return strings.Contains(moduleName, "/internal/") ||
strings.HasSuffix(moduleName, "/internal")
func isUnexportedModule(moduleName string, includeInternal bool) bool {
return !includeInternal && (strings.Contains(moduleName, "/internal/") ||
strings.HasSuffix(moduleName, "/internal"))
}

func findAndAddVanityImportForModuleDir(workingDir, absDir string, moduleName string, opts Options) (int, error) {
if isUnexportedModule(moduleName) {
if isUnexportedModule(moduleName, opts.IncludeInternal) {
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.IncludeInternal) {
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.IncludeInternal) {
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
// Include internal packages
IncludeInternal 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.IncludeInternal) {
continue
}

Expand Down
7 changes: 4 additions & 3 deletions import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ func TestIsIgnoredFile(t *testing.T) {
}

func TestIsUnexportedModule(t *testing.T) {
assert.True(t, isUnexportedModule("go.opentelemetry.io/otel/internal"))
assert.True(t, isUnexportedModule("go.opentelemetry.io/otel/internal/metric"))
assert.False(t, isUnexportedModule("go.opentelemetry.io/otel/internalmetric"))
assert.True(t, isUnexportedModule("go.opentelemetry.io/otel/internal", false))
assert.True(t, isUnexportedModule("go.opentelemetry.io/otel/internal/metric", false))
assert.False(t, isUnexportedModule("go.opentelemetry.io/otel/internalmetric", false))
assert.False(t, isUnexportedModule("go.opentelemetry.io/otel/internal/metric", true))
}