Skip to content

Commit

Permalink
Add --ignore option
Browse files Browse the repository at this point in the history
Packages beginning with the specified path will be ignored.
Imported packages by ignored packages are still checked.
  • Loading branch information
at-wat committed Nov 26, 2020
1 parent 73411c8 commit 93f9da6
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 5 deletions.
2 changes: 1 addition & 1 deletion check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion licenses/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"))
}
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 @@ -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() {
Expand Down
37 changes: 37 additions & 0 deletions pathlist.go
Original file line number Diff line number Diff line change
@@ -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"
}
2 changes: 1 addition & 1 deletion save.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 93f9da6

Please sign in to comment.