github.com/cilium/cilium@v1.16.2/tools/licensecheck/main.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package main
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"os"
    10  
    11  	"github.com/google/go-licenses/licenses"
    12  )
    13  
    14  var includeTests bool
    15  
    16  func main() {
    17  	if len(os.Args) < 2 {
    18  		fmt.Fprintf(os.Stderr, "Usage: %s <package1> [<package2> ...]", os.Args[0])
    19  		os.Exit(1)
    20  	}
    21  
    22  	fmt.Println("Starting licenses check for third party Go dependencies...")
    23  	unauthorized, err := check(os.Args[1:]...)
    24  	if err != nil {
    25  		fmt.Printf("Failed to check licenses: %v\n", err)
    26  		os.Exit(1)
    27  	}
    28  	if len(unauthorized) > 0 {
    29  		fmt.Println("\nThe third party dependencies below are not allowed under the CNCF's IP policy:")
    30  		for _, f := range unauthorized {
    31  			fmt.Printf("  - %s\n", f)
    32  		}
    33  		fmt.Println("\nProjects under the following licenses are generally accepted:")
    34  		for _, l := range allowedLicenses {
    35  			fmt.Printf("  - %s\n", l)
    36  		}
    37  		fmt.Println("\nUnder certain circumstances, exceptions can be made for projects using a different license. For more information, please consult the following resources:\nhttps://github.com/cncf/foundation/blob/main/allowed-third-party-license-policy.md\nhttps://github.com/cncf/foundation/tree/main/license-exceptions")
    38  		os.Exit(1)
    39  	}
    40  	fmt.Println("Licenses check OK")
    41  }
    42  
    43  func check(path ...string) ([]string, error) {
    44  	var unauthorized []string
    45  
    46  	classifier, err := licenses.NewClassifier()
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	libs, err := licenses.Libraries(context.Background(), classifier, includeTests, pkgExceptions, path...)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	for _, lib := range libs {
    56  		if lib.LicenseFile == "" {
    57  			unauthorized = append(unauthorized, fmt.Sprintf("%s (NO LICENSE FOUND)", lib.Name()))
    58  			continue
    59  		}
    60  		for _, license := range lib.Licenses {
    61  			if !isAllowedLicense(license.Name) {
    62  				unauthorized = append(unauthorized, fmt.Sprintf("%s (%s)", lib.Name(), license.Name))
    63  			}
    64  		}
    65  	}
    66  	return unauthorized, nil
    67  }
    68  
    69  func isAllowedLicense(name string) bool {
    70  	for _, allowed := range allowedLicenses {
    71  		if allowed == name {
    72  			return true
    73  		}
    74  	}
    75  	return false
    76  }