2023-03-23 14:38:15 +00:00
|
|
|
package licenses
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/google/licensecheck"
|
2023-05-15 20:23:39 +00:00
|
|
|
|
2023-05-24 21:06:38 +00:00
|
|
|
"github.com/anchore/syft/syft/file"
|
2023-05-15 20:23:39 +00:00
|
|
|
"github.com/anchore/syft/syft/license"
|
|
|
|
"github.com/anchore/syft/syft/pkg"
|
2023-03-23 14:38:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
coverageThreshold = 75
|
|
|
|
unknownLicenseType = "UNKNOWN"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Parse scans the contents of a license file to attempt to determine the type of license it is
|
2023-05-24 21:06:38 +00:00
|
|
|
func Parse(reader io.Reader, l file.Location) (licenses []pkg.License, err error) {
|
2023-05-15 20:23:39 +00:00
|
|
|
licenses = make([]pkg.License, 0)
|
2023-03-23 14:38:15 +00:00
|
|
|
contents, err := io.ReadAll(reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cov := licensecheck.Scan(contents)
|
2023-05-15 20:23:39 +00:00
|
|
|
if cov.Percent < coverageThreshold {
|
|
|
|
// unknown or no licenses here?
|
|
|
|
return licenses, nil
|
2023-03-23 14:38:15 +00:00
|
|
|
}
|
2023-05-15 20:23:39 +00:00
|
|
|
|
2023-03-23 14:38:15 +00:00
|
|
|
for _, m := range cov.Match {
|
2023-05-15 20:23:39 +00:00
|
|
|
lic := pkg.NewLicenseFromLocations(m.ID, l)
|
|
|
|
lic.Type = license.Concluded
|
|
|
|
|
|
|
|
licenses = append(licenses, lic)
|
2023-03-23 14:38:15 +00:00
|
|
|
}
|
2023-05-15 20:23:39 +00:00
|
|
|
|
|
|
|
return licenses, nil
|
2023-03-23 14:38:15 +00:00
|
|
|
}
|