github.com/anchore/syft@v1.38.2/syft/pkg/license_deprecated.go (about)

     1  package pkg
     2  
     3  import (
     4  	"sort"
     5  	"strings"
     6  
     7  	"github.com/scylladb/go-set/strset"
     8  
     9  	"github.com/anchore/syft/internal/log"
    10  	"github.com/anchore/syft/syft/file"
    11  	"github.com/anchore/syft/syft/license"
    12  )
    13  
    14  func NewLicense(value string) License {
    15  	return NewLicenseFromType(value, license.Declared)
    16  }
    17  
    18  func NewLicenseFromType(value string, t license.Type) License {
    19  	var (
    20  		spdxExpression string
    21  		fullText       string
    22  	)
    23  	// Check parsed value for newline character to see if it's the full license text
    24  	// License: <HERE IS THE FULL TEXT> <Expressions>
    25  	// DO we want to also submit file name when determining fulltext
    26  	if strings.Contains(strings.TrimSpace(value), "\n") {
    27  		fullText = value
    28  	} else {
    29  		var err error
    30  		spdxExpression, err = license.ParseExpression(value)
    31  		if err != nil {
    32  			log.WithFields("error", err, "expression", value).Trace("unable to parse license expression")
    33  		}
    34  	}
    35  
    36  	if fullText != "" {
    37  		return License{
    38  			Contents:  fullText,
    39  			Type:      t,
    40  			Locations: file.NewLocationSet(),
    41  		}
    42  	}
    43  
    44  	return License{
    45  		Value:          value,
    46  		SPDXExpression: spdxExpression,
    47  		Type:           t,
    48  		Locations:      file.NewLocationSet(),
    49  	}
    50  }
    51  
    52  func NewLicensesFromValues(values ...string) (licenses []License) {
    53  	for _, v := range values {
    54  		licenses = append(licenses, NewLicense(v))
    55  	}
    56  	return
    57  }
    58  
    59  func NewLicensesFromLocation(location file.Location, values ...string) (licenses []License) {
    60  	for _, v := range values {
    61  		if v == "" {
    62  			continue
    63  		}
    64  		licenses = append(licenses, NewLicenseFromLocations(v, location))
    65  	}
    66  	return licenses
    67  }
    68  
    69  func NewLicenseFromLocations(value string, locations ...file.Location) License {
    70  	l := NewLicense(value)
    71  	for _, loc := range locations {
    72  		l.Locations.Add(loc)
    73  	}
    74  	return l
    75  }
    76  
    77  func NewLicenseFromURLs(value string, urls ...string) License {
    78  	l := NewLicense(value)
    79  	s := strset.New()
    80  	for _, url := range urls {
    81  		if url != "" {
    82  			sanitizedURL, err := stripUnwantedCharacters(url)
    83  			if err != nil {
    84  				log.Tracef("unable to sanitize url=%q: %s", url, err)
    85  				continue
    86  			}
    87  			s.Add(sanitizedURL)
    88  		}
    89  	}
    90  
    91  	l.URLs = s.List()
    92  	sort.Strings(l.URLs)
    93  
    94  	return l
    95  }
    96  
    97  func NewLicenseFromFields(value, url string, location *file.Location) License {
    98  	l := NewLicense(value)
    99  	if location != nil {
   100  		l.Locations.Add(*location)
   101  	}
   102  	if url != "" {
   103  		sanitizedURL, err := stripUnwantedCharacters(url)
   104  		if err != nil {
   105  			log.Tracef("unable to sanitize url=%q: %s", url, err)
   106  		} else {
   107  			l.URLs = append(l.URLs, sanitizedURL)
   108  		}
   109  	}
   110  
   111  	return l
   112  }