From aa7516286fb069fd4186a7c4065666d0d9729672 Mon Sep 17 00:00:00 2001 From: Cedric BAIL Date: Wed, 21 Dec 2022 15:34:45 -0700 Subject: [PATCH] Add ability to exclude specific version of a repository. This can be useful when a repository is mostly abandonned but still used with a License that is not properly set. --- license.go | 6 +++++- main.go | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/license.go b/license.go index af56fb2..e86aabe 100644 --- a/license.go +++ b/license.go @@ -64,7 +64,7 @@ func getGoModCache() string { return filepath.Join(gopath, "pkg", "mod") } -func getLicenses(gomodcache string, info moduleInfo, patterns []string) ([]license, error) { +func getLicenses(gomodcache string, info moduleInfo, patterns []string, excluded map[string]struct{}) ([]license, error) { licenses := []license{} for _, v := range info.Require { @@ -77,6 +77,10 @@ func getLicenses(gomodcache string, info moduleInfo, patterns []string) ([]licen return nil, err } + if _, ok := excluded[modpath]; ok { + continue + } + licenseFiles, err := findLicenses(filepath.Join(gomodcache, modpath), patterns) if err != nil { return nil, fmt.Errorf("could not scan for licenses: %w", err) diff --git a/main.go b/main.go index ffc3258..06da737 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ type options struct { listLicenseNames bool output string version bool + excluded string } func main() { @@ -31,6 +32,7 @@ Default is to search for a go.mod file into the current directory. Options: -a, --allowed comma separated list of allowed licenses (i.e. MIT, BSD-3-Clause). Default to all + -e, --excluded comma separated list of repository with version excluded from the licenses check. Default to none -d, --download download dependencies to local cache --dump dump all licenses -h, --help show this help message @@ -53,6 +55,8 @@ Options: flag.BoolVar(&opts.version, "version", false, "") flag.BoolVar(&opts.listLicenseNames, "list-names", false, "") flag.BoolVar(&opts.listLicenses, "list-licenses", false, "") + flag.StringVar(&opts.excluded, "e", "", "") + flag.StringVar(&opts.excluded, "excluded", "", "") flag.Parse() if opts.version { @@ -88,8 +92,15 @@ Options: } } + excluded := splitCommaSeparatedFlag(opts.excluded) + + excludedExist := map[string]struct{}{} + for _, e := range excluded { + excludedExist[e] = struct{}{} + } + gomodcache := getGoModCache() - licenses, err := getLicenses(gomodcache, mi, licenseNames) + licenses, err := getLicenses(gomodcache, mi, licenseNames, excludedExist) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) @@ -136,15 +147,7 @@ func report(w io.Writer, licenses []license, opts options) error { // the table header th := "License\tDependency\tFile\tpkg.go.dev URL" - var allowed []string - if opts.allowed != "" { - for _, v := range strings.Split(opts.allowed, ",") { - v := strings.TrimSpace(v) - if v != "" { - allowed = append(allowed, v) - } - } - } + allowed := splitCommaSeparatedFlag(opts.allowed) if len(allowed) > 0 { th = "Allowed\t" + th } @@ -206,3 +209,18 @@ func version() string { } return "(unknown)" } + +func splitCommaSeparatedFlag(s string) []string { + if s == "" { + return []string{} + } + + var r []string + for _, v := range strings.Split(s, ",") { + v := strings.TrimSpace(v) + if v != "" { + r = append(r, v) + } + } + return r +}