Skip to content

Commit

Permalink
feat: add --ignore option (#46)
Browse files Browse the repository at this point in the history
* Add --ignore option

Packages beginning with the specified path will be ignored.
Imported packages by ignored packages are still checked.

* Use pflag.StringSlice instead of custom type

* Fix option description

* Clarify dependencies from the ignored packages are still checked

* Add README section

Co-authored-by: Yuan (Bob) Gong <[email protected]>
  • Loading branch information
at-wat and Bobgy committed Apr 13, 2022
1 parent 1f7f92b commit 5b654af
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ github.com/client9/misspell,https://github.com/client9/misspell/blob/master/LICE
github.com/golang/protobuf/proto,https://github.com/golang/protobuf/blob/master/proto/LICENSE,BSD-3-Clause
```
## Ignoring packages
Use the `--ignore` global flag to specify package path prefixes to be ignored.
For example, to ignore your organization's internal packages under `github.com/example-corporation`:
```shell
$ go-licenses check \
github.com/example-corporation/example-product \
--ignore github.com/example-corporation
```
Note that dependencies from the ignored packages are still resolved and checked.
This flag makes effect to `check`, `csv` and `save` commands.
## Warnings and errors
The tool will log warnings and errors in some scenarios. This section provides
Expand Down
2 changes: 1 addition & 1 deletion check.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func checkMain(_ *cobra.Command, args []string) error {
return err
}

libs, err := licenses.Libraries(context.Background(), classifier, args...)
libs, err := licenses.Libraries(context.Background(), classifier, ignore, args...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func csvMain(_ *cobra.Command, args []string) error {
return err
}

libs, err := licenses.Libraries(context.Background(), classifier, args...)
libs, err := licenses.Libraries(context.Background(), classifier, ignore, args...)
if err != nil {
return err
}
Expand Down
9 changes: 8 additions & 1 deletion licenses/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (e PackagesError) Error() string {
// A library is a collection of one or more packages covered by the same license file.
// Packages not covered by a license will be returned as individual libraries.
// Standard library packages will be ignored.
func Libraries(ctx context.Context, classifier Classifier, importPaths ...string) ([]*Library, error) {
func Libraries(ctx context.Context, classifier Classifier, ignoredPaths []string, importPaths ...string) ([]*Library, error) {
cfg := &packages.Config{
Context: ctx,
Mode: packages.NeedImports | packages.NeedDeps | packages.NeedFiles | packages.NeedName | packages.NeedModule,
Expand All @@ -83,6 +83,13 @@ func Libraries(ctx context.Context, classifier Classifier, importPaths ...string
// No license requirements for the Go standard library.
return false
}
for _, i := range ignoredPaths {
if strings.HasPrefix(p.PkgPath, i) {
// Marked to be ignored.
return true
}
}

if len(p.OtherFiles) > 0 {
glog.Warningf("%q contains non-Go code that can't be inspected for further dependencies:\n%s", p.PkgPath, strings.Join(p.OtherFiles, "\n"))
}
Expand Down
14 changes: 13 additions & 1 deletion licenses/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestLibraries(t *testing.T) {
desc string
importPath string
goflags string
ignore []string
wantLibs []string
}{
{
Expand All @@ -60,6 +61,17 @@ func TestLibraries(t *testing.T) {
"github.com/google/go-licenses/licenses/testdata/indirect",
},
},
{
desc: "Ignores a package path",
importPath: "github.com/google/go-licenses/licenses/testdata",
ignore: []string{
"github.com/google/go-licenses/licenses/testdata/direct",
},
wantLibs: []string{
"github.com/google/go-licenses/licenses/testdata",
"github.com/google/go-licenses/licenses/testdata/indirect",
},
},
{
desc: "Build tagged package",
importPath: "github.com/google/go-licenses/licenses/testdata/tags",
Expand All @@ -75,7 +87,7 @@ func TestLibraries(t *testing.T) {
os.Setenv("GOFLAGS", test.goflags)
defer os.Unsetenv("GOFLAGS")
}
gotLibs, err := Libraries(context.Background(), classifier, test.importPath)
gotLibs, err := Libraries(context.Background(), classifier, test.ignore, test.importPath)
if err != nil {
t.Fatalf("Libraries(_, %q) = (_, %q), want (_, nil)", test.importPath, err)
}
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Prerequisites:

// Flags shared between subcommands
confidenceThreshold float64
ignore []string
packageHelp = `
Typically, specify the Go package that builds your Go binary.
Expand All @@ -63,6 +64,7 @@ func init() {
os.Exit(1)
}
rootCmd.PersistentFlags().Float64Var(&confidenceThreshold, "confidence_threshold", 0.9, "Minimum confidence required in order to positively identify a license.")
rootCmd.PersistentFlags().StringSliceVar(&ignore, "ignore", nil, "Package path prefixes to be ignored. Dependencies from the ignored packages are still checked. Can be specified multiple times.")
}

func main() {
Expand Down
2 changes: 1 addition & 1 deletion save.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func saveMain(_ *cobra.Command, args []string) error {
return err
}

libs, err := licenses.Libraries(context.Background(), classifier, args...)
libs, err := licenses.Libraries(context.Background(), classifier, ignore, args...)
if err != nil {
return err
}
Expand Down

0 comments on commit 5b654af

Please sign in to comment.