Skip to content

Commit 5b654af

Browse files
at-watBobgy
andauthored
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 <[email protected]>
1 parent 1f7f92b commit 5b654af

File tree

7 files changed

+40
-5
lines changed

7 files changed

+40
-5
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,20 @@ github.com/client9/misspell,https://github.com/client9/misspell/blob/master/LICE
166166
github.com/golang/protobuf/proto,https://github.com/golang/protobuf/blob/master/proto/LICENSE,BSD-3-Clause
167167
```
168168

169+
## Ignoring packages
170+
171+
Use the `--ignore` global flag to specify package path prefixes to be ignored.
172+
For example, to ignore your organization's internal packages under `github.com/example-corporation`:
173+
174+
```shell
175+
$ go-licenses check \
176+
github.com/example-corporation/example-product \
177+
--ignore github.com/example-corporation
178+
```
179+
180+
Note that dependencies from the ignored packages are still resolved and checked.
181+
This flag makes effect to `check`, `csv` and `save` commands.
182+
169183
## Warnings and errors
170184
171185
The tool will log warnings and errors in some scenarios. This section provides

check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func checkMain(_ *cobra.Command, args []string) error {
4444
return err
4545
}
4646

47-
libs, err := licenses.Libraries(context.Background(), classifier, args...)
47+
libs, err := licenses.Libraries(context.Background(), classifier, ignore, args...)
4848
if err != nil {
4949
return err
5050
}

csv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func csvMain(_ *cobra.Command, args []string) error {
5151
return err
5252
}
5353

54-
libs, err := licenses.Libraries(context.Background(), classifier, args...)
54+
libs, err := licenses.Libraries(context.Background(), classifier, ignore, args...)
5555
if err != nil {
5656
return err
5757
}

licenses/library.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (e PackagesError) Error() string {
5959
// A library is a collection of one or more packages covered by the same license file.
6060
// Packages not covered by a license will be returned as individual libraries.
6161
// Standard library packages will be ignored.
62-
func Libraries(ctx context.Context, classifier Classifier, importPaths ...string) ([]*Library, error) {
62+
func Libraries(ctx context.Context, classifier Classifier, ignoredPaths []string, importPaths ...string) ([]*Library, error) {
6363
cfg := &packages.Config{
6464
Context: ctx,
6565
Mode: packages.NeedImports | packages.NeedDeps | packages.NeedFiles | packages.NeedName | packages.NeedModule,
@@ -83,6 +83,13 @@ func Libraries(ctx context.Context, classifier Classifier, importPaths ...string
8383
// No license requirements for the Go standard library.
8484
return false
8585
}
86+
for _, i := range ignoredPaths {
87+
if strings.HasPrefix(p.PkgPath, i) {
88+
// Marked to be ignored.
89+
return true
90+
}
91+
}
92+
8693
if len(p.OtherFiles) > 0 {
8794
glog.Warningf("%q contains non-Go code that can't be inspected for further dependencies:\n%s", p.PkgPath, strings.Join(p.OtherFiles, "\n"))
8895
}

licenses/library_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestLibraries(t *testing.T) {
4141
desc string
4242
importPath string
4343
goflags string
44+
ignore []string
4445
wantLibs []string
4546
}{
4647
{
@@ -60,6 +61,17 @@ func TestLibraries(t *testing.T) {
6061
"github.com/google/go-licenses/licenses/testdata/indirect",
6162
},
6263
},
64+
{
65+
desc: "Ignores a package path",
66+
importPath: "github.com/google/go-licenses/licenses/testdata",
67+
ignore: []string{
68+
"github.com/google/go-licenses/licenses/testdata/direct",
69+
},
70+
wantLibs: []string{
71+
"github.com/google/go-licenses/licenses/testdata",
72+
"github.com/google/go-licenses/licenses/testdata/indirect",
73+
},
74+
},
6375
{
6476
desc: "Build tagged package",
6577
importPath: "github.com/google/go-licenses/licenses/testdata/tags",
@@ -75,7 +87,7 @@ func TestLibraries(t *testing.T) {
7587
os.Setenv("GOFLAGS", test.goflags)
7688
defer os.Unsetenv("GOFLAGS")
7789
}
78-
gotLibs, err := Libraries(context.Background(), classifier, test.importPath)
90+
gotLibs, err := Libraries(context.Background(), classifier, test.ignore, test.importPath)
7991
if err != nil {
8092
t.Fatalf("Libraries(_, %q) = (_, %q), want (_, nil)", test.importPath, err)
8193
}

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Prerequisites:
3838

3939
// Flags shared between subcommands
4040
confidenceThreshold float64
41+
ignore []string
4142
packageHelp = `
4243
4344
Typically, specify the Go package that builds your Go binary.
@@ -63,6 +64,7 @@ func init() {
6364
os.Exit(1)
6465
}
6566
rootCmd.PersistentFlags().Float64Var(&confidenceThreshold, "confidence_threshold", 0.9, "Minimum confidence required in order to positively identify a license.")
67+
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.")
6668
}
6769

6870
func main() {

save.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func saveMain(_ *cobra.Command, args []string) error {
7575
return err
7676
}
7777

78-
libs, err := licenses.Libraries(context.Background(), classifier, args...)
78+
libs, err := licenses.Libraries(context.Background(), classifier, ignore, args...)
7979
if err != nil {
8080
return err
8181
}

0 commit comments

Comments
 (0)