From 5b654af5dcd3ef8090baaceae6009c20d75a87e8 Mon Sep 17 00:00:00 2001 From: Atsushi Watanabe Date: Wed, 13 Apr 2022 20:58:51 +0900 Subject: [PATCH] feat: add --ignore option (#46) * 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 <4957653+Bobgy@users.noreply.github.com> --- README.md | 14 ++++++++++++++ check.go | 2 +- csv.go | 2 +- licenses/library.go | 9 ++++++++- licenses/library_test.go | 14 +++++++++++++- main.go | 2 ++ save.go | 2 +- 7 files changed, 40 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b7d34e1..68ceec1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/check.go b/check.go index 0640628..1f4daea 100644 --- a/check.go +++ b/check.go @@ -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 } diff --git a/csv.go b/csv.go index 6676d53..8ecf82c 100644 --- a/csv.go +++ b/csv.go @@ -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 } diff --git a/licenses/library.go b/licenses/library.go index af792bc..c209224 100644 --- a/licenses/library.go +++ b/licenses/library.go @@ -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, @@ -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")) } diff --git a/licenses/library_test.go b/licenses/library_test.go index 4f86c06..614f789 100644 --- a/licenses/library_test.go +++ b/licenses/library_test.go @@ -41,6 +41,7 @@ func TestLibraries(t *testing.T) { desc string importPath string goflags string + ignore []string wantLibs []string }{ { @@ -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", @@ -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) } diff --git a/main.go b/main.go index 8837b9a..3a22b03 100644 --- a/main.go +++ b/main.go @@ -38,6 +38,7 @@ Prerequisites: // Flags shared between subcommands confidenceThreshold float64 + ignore []string packageHelp = ` Typically, specify the Go package that builds your Go binary. @@ -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() { diff --git a/save.go b/save.go index ff7271b..934cd98 100644 --- a/save.go +++ b/save.go @@ -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 }