github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/internal/licenses/parser.go (about)

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