From 93f9da6ba9f89e98662c47e90fcd3d2991872564 Mon Sep 17 00:00:00 2001 From: Atsushi Watanabe Date: Thu, 26 Nov 2020 15:24:55 +0900 Subject: [PATCH] Add --ignore option Packages beginning with the specified path will be ignored. Imported packages by ignored packages are still checked. --- check.go | 2 +- csv.go | 2 +- licenses/library.go | 9 ++++++++- licenses/library_test.go | 14 +++++++++++++- main.go | 2 ++ pathlist.go | 37 +++++++++++++++++++++++++++++++++++++ save.go | 2 +- 7 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 pathlist.go diff --git a/check.go b/check.go index 83b33fe..407bfad 100644 --- a/check.go +++ b/check.go @@ -42,7 +42,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 c3fa44a..adb8f72 100644 --- a/csv.go +++ b/csv.go @@ -50,7 +50,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 fec6b28..1bd0538 100644 --- a/licenses/library.go +++ b/licenses/library.go @@ -65,7 +65,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, @@ -88,6 +88,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 e082fe3..101b5cf 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 c1b9d30..57d151e 100644 --- a/main.go +++ b/main.go @@ -29,10 +29,12 @@ var ( // Flags shared between subcommands confidenceThreshold float64 + ignore pathList ) func init() { rootCmd.PersistentFlags().Float64Var(&confidenceThreshold, "confidence_threshold", 0.9, "Minimum confidence required in order to positively identify a license.") + rootCmd.PersistentFlags().Var(&ignore, "ignore", "Module path prefixes to be ignored. Can be specified multiple times.") } func main() { diff --git a/pathlist.go b/pathlist.go new file mode 100644 index 0000000..22f7703 --- /dev/null +++ b/pathlist.go @@ -0,0 +1,37 @@ +// Copyright 2019 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "fmt" +) + +type pathList []string + +func (l *pathList) String() string { + if len(*l) == 0 { + return "" + } + return fmt.Sprintf("%v", *l) +} + +func (l *pathList) Set(v string) error { + *l = append(*l, v) + return nil +} + +func (l *pathList) Type() string { + return "string" +} diff --git a/save.go b/save.go index 9a838c5..77bb8c3 100644 --- a/save.go +++ b/save.go @@ -81,7 +81,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 }