github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/internal/licenses/parser.go (about)

     1  package licenses
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/google/licensecheck"
     7  
     8  	"github.com/anchore/syft/syft/file"
     9  	"github.com/anchore/syft/syft/license"
    10  	"github.com/anchore/syft/syft/pkg"
    11  )
    12  
    13  const (
    14  	coverageThreshold  = 75
    15  	unknownLicenseType = "UNKNOWN"
    16  )
    17  
    18  // Parse scans the contents of a license file to attempt to determine the type of license it is
    19  func Parse(reader io.Reader, l file.Location) (licenses []pkg.License, err error) {
    20  	licenses = make([]pkg.License, 0)
    21  	contents, err := io.ReadAll(reader)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	cov := licensecheck.Scan(contents)
    26  	if cov.Percent < coverageThreshold {
    27  		// unknown or no licenses here?
    28  		return licenses, nil
    29  	}
    30  
    31  	for _, m := range cov.Match {
    32  		lic := pkg.NewLicenseFromLocations(m.ID, l)
    33  		lic.Type = license.Concluded
    34  
    35  		licenses = append(licenses, lic)
    36  	}
    37  
    38  	return licenses, nil
    39  }