+
Skip to content

Add ability to exclude specific version of a repository. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion license.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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)
Expand Down
38 changes: 28 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type options struct {
listLicenseNames bool
output string
version bool
excluded string
}

func main() {
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载