@@ -611,6 +611,7 @@ func RemoveHashFromComponent(doc *cydx.BOM, entries []interface{}, params *types
611611func RemoveLicenseFromComponent (doc * cydx.BOM , entries []interface {}, params * types.RmParams ) error {
612612 log := logger .FromContext (* params .Ctx )
613613 removedCount := 0
614+
614615 for _ , e := range entries {
615616 entry , ok := e .(LicenseEntry )
616617 if ! ok || entry .Value == "" {
@@ -619,58 +620,81 @@ func RemoveLicenseFromComponent(doc *cydx.BOM, entries []interface{}, params *ty
619620 }
620621
621622 comp := entry .Component
622- found := false
623- if doc .Metadata .Component != nil && comp == doc .Metadata .Component {
624- found = true
625- } else if doc .Components != nil {
626- for i := range * doc .Components {
627- if & (* doc .Components )[i ] == comp {
628- found = true
629- break
630- }
631- }
632- }
633- if ! found {
634- log .Warnf ("Warning: Component %s@%s not found in document\n " , comp .Name , comp .Version )
623+
624+ if comp .Licenses == nil {
635625 continue
636626 }
637627
638- if params .Value == "NOASSERTION" {
639- log .Warnf ("Warning: NOASSERTION is unlikely for license field" )
640- }
628+ var newLicenses cydx.Licenses
641629
642- if comp .Licenses != nil {
643- var newLicenses cydx.Licenses
644- for _ , license := range * comp .Licenses {
645- licenseValue := license .License .ID
646- field := "ID"
647- if licenseValue == "" {
648- licenseValue = license .License .Name
649- field = "Name"
650- }
651- if licenseValue == "" {
652- licenseValue = license .Expression
653- field = "Expression"
654- }
655- if licenseValue == "" || ! strings .EqualFold (licenseValue , entry .Value ) {
656- newLicenses = append (newLicenses , license )
657- } else {
658- removedCount ++
659- log .Debugf ("Removed license from component: %s@%s, License: %s (Field: %s)\n " ,
660- comp .Name , comp .Version , licenseValue , field )
661- }
630+ for _ , license := range * comp .Licenses {
631+
632+ remove := false
633+
634+ // Match license ID
635+ if license .License != nil &&
636+ license .License .ID != "" &&
637+ strings .EqualFold (license .License .ID , entry .Value ) {
638+
639+ remove = true
640+
641+ log .Debugf (
642+ "Removed license from component: %s@%s, License: %s (Field: ID)" ,
643+ comp .Name ,
644+ comp .Version ,
645+ license .License .ID ,
646+ )
662647 }
663- comp .Licenses = & newLicenses
664- if len (newLicenses ) == 0 {
665- comp .Licenses = nil
648+
649+ // Match license Name
650+ if license .License != nil &&
651+ license .License .Name != "" &&
652+ strings .EqualFold (license .License .Name , entry .Value ) {
653+
654+ remove = true
655+
656+ log .Debugf (
657+ "Removed license from component: %s@%s, License: %s (Field: Name)" ,
658+ comp .Name ,
659+ comp .Version ,
660+ license .License .Name ,
661+ )
662+ }
663+
664+ // Match expression
665+ if license .Expression != "" &&
666+ strings .EqualFold (license .Expression , entry .Value ) {
667+
668+ remove = true
669+
670+ log .Debugf (
671+ "Removed license from component: %s@%s, License: %s (Field: Expression)" ,
672+ comp .Name ,
673+ comp .Version ,
674+ license .Expression ,
675+ )
676+ }
677+
678+ if remove {
679+ removedCount ++
680+ } else {
681+ newLicenses = append (newLicenses , license )
666682 }
667683 }
684+
685+ if len (newLicenses ) == 0 {
686+ comp .Licenses = nil
687+ } else {
688+ comp .Licenses = & newLicenses
689+ }
668690 }
669691
670- log .Debugf ("Removed %d license entries from components\n " , removedCount )
692+ log .Debugf ("Removed %d license entries from components" , removedCount )
693+
671694 if len (entries ) > 0 && removedCount == 0 {
672- log .Debugf ("No license entries removed\n " )
695+ log .Debugf ("No license entries removed" )
673696 }
697+
674698 return nil
675699}
676700
0 commit comments