github.com/anchore/syft@v1.38.2/internal/spdxlicense/license.go (about)

     1  package spdxlicense
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  // https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/#license-short-name
     8  // License generated in license_list.go uses a regular expression to help resolve cases where
     9  // x.0.0 and x are supplied as version numbers. For SPDX compatibility, versions with trailing
    10  // dot-zeroes are considered to be equivalent to versions without (e.g., “2.0.0” is considered equal to “2.0” and “2”).
    11  // EX: gpl-2+ ---> GPL-2.0+
    12  // EX: gpl-2.0.0-only ---> GPL-2.0-only
    13  // See the debian link for more details on the spdx license differences
    14  
    15  const (
    16  	LicenseRefPrefix = "LicenseRef-" // prefix for non-standard licenses
    17  )
    18  
    19  //go:generate go run ./generate
    20  
    21  // ID returns the canonical license ID for the given license ID
    22  // Note: this function is only concerned with returning a best match of an SPDX license ID
    23  // SPDX Expressions will be handled by a parent package which will call this function
    24  func ID(id string) (value string, exists bool) {
    25  	// first look for a canonical license
    26  	if value, exists := licenseIDs[cleanLicenseID(id)]; exists {
    27  		return value, exists
    28  	}
    29  	// we did not find, so treat it as a separate license
    30  	return "", false
    31  }
    32  
    33  func cleanLicenseID(id string) string {
    34  	id = strings.TrimSpace(id)
    35  	id = strings.ToLower(id)
    36  	return strings.ReplaceAll(id, "-", "")
    37  }
    38  
    39  // LicenseInfo contains license ID and name
    40  type LicenseInfo struct {
    41  	ID string
    42  }
    43  
    44  // LicenseByURL returns the license ID and name for a given URL from the SPDX license list
    45  // The URL should match one of the URLs in the seeAlso field of an SPDX license
    46  func LicenseByURL(url string) (LicenseInfo, bool) {
    47  	url = strings.TrimSpace(url)
    48  	if id, exists := urlToLicense[url]; exists {
    49  		return LicenseInfo{
    50  			ID: id,
    51  		}, true
    52  	}
    53  	return LicenseInfo{}, false
    54  }