@@ -19,6 +19,7 @@ package deps
1919
2020import (
2121 "fmt"
22+ "math"
2223 "path/filepath"
2324 "strings"
2425
@@ -73,34 +74,35 @@ func Check(mainLicenseSpdxID string, config *ConfigDeps) error {
7374 return CheckWithMatrix (mainLicenseSpdxID , & matrix , & report )
7475}
7576
76- func CheckWithMatrix (mainLicenseSpdxID string , matrix * CompatibilityMatrix , report * Report ) error {
77- var incompatibleResults []* Result
78- for _ , result := range append (report .Resolved , report .Skipped ... ) {
79- compare := func (list []string , spdxID string ) bool {
80- for _ , com := range list {
81- if spdxID == com {
82- return true
83- }
84- }
85- return false
86- }
87- compareAll := func (spdxIDs []string , compare func (spdxID string ) bool ) bool {
88- for _ , spdxID := range spdxIDs {
89- if ! compare (spdxID ) {
90- return false
91- }
92- }
77+ func compare (list []string , spdxID string ) bool {
78+ for _ , com := range list {
79+ if spdxID == com {
9380 return true
9481 }
95- compareAny := func ( spdxIDs [] string , compare func ( spdxID string ) bool ) bool {
96- for _ , spdxID := range spdxIDs {
97- if compare ( spdxID ) {
98- return true
99- }
100- }
82+ }
83+ return false
84+ }
85+ func compareAll ( spdxIDs [] string , compare func ( spdxID string ) bool ) bool {
86+ for _ , spdxID := range spdxIDs {
87+ if ! compare ( spdxID ) {
10188 return false
10289 }
90+ }
91+ return true
92+ }
93+ func compareAny (spdxIDs []string , compare func (spdxID string ) bool ) bool {
94+ for _ , spdxID := range spdxIDs {
95+ if compare (spdxID ) {
96+ return true
97+ }
98+ }
99+ return false
100+ }
103101
102+ func CheckWithMatrix (mainLicenseSpdxID string , matrix * CompatibilityMatrix , report * Report ) error {
103+ var incompatibleResults []* Result
104+ var unknownResults []* Result
105+ for _ , result := range append (report .Resolved , report .Skipped ... ) {
104106 operator , spdxIDs := parseLicenseExpression (result .LicenseSpdxID )
105107
106108 switch operator {
@@ -134,16 +136,34 @@ func CheckWithMatrix(mainLicenseSpdxID string, matrix *CompatibilityMatrix, repo
134136 }
135137 if incompatible := compare (matrix .Incompatible , spdxIDs [0 ]); incompatible {
136138 incompatibleResults = append (incompatibleResults , result )
139+ continue
137140 }
141+ unknownResults = append (unknownResults , result )
138142 }
139143 }
140144
141- if len (incompatibleResults ) > 0 {
142- str := ""
145+ if len (incompatibleResults ) > 0 || len ( unknownResults ) > 0 {
146+ dWidth , lWidth := float64 ( len ( "Dependency" )), float64 ( len ( "License" ))
143147 for _ , r := range incompatibleResults {
144- str += fmt .Sprintf ("\n License: %v Dependency: %v" , r .LicenseSpdxID , r .Dependency )
148+ dWidth = math .Max (float64 (len (r .Dependency )), dWidth )
149+ lWidth = math .Max (float64 (len (r .LicenseSpdxID )), lWidth )
145150 }
146- return fmt .Errorf ("the following licenses are incompatible with the main license: %v %v" , mainLicenseSpdxID , str )
151+ for _ , r := range unknownResults {
152+ dWidth = math .Max (float64 (len (r .Dependency )), dWidth )
153+ lWidth = math .Max (float64 (len (r .LicenseSpdxID )), lWidth )
154+ }
155+
156+ rowTemplate := fmt .Sprintf ("%%-%dv | %%%dv\n " , int (dWidth ), int (lWidth ))
157+ s := fmt .Sprintf (rowTemplate , "Dependency" , "License" )
158+ s += fmt .Sprintf (rowTemplate , strings .Repeat ("-" , int (dWidth )), strings .Repeat ("-" , int (lWidth )))
159+ for _ , r := range incompatibleResults {
160+ s += fmt .Sprintf (rowTemplate , r .Dependency , r .LicenseSpdxID )
161+ }
162+ for _ , r := range unknownResults {
163+ s += fmt .Sprintf (rowTemplate , r .Dependency , r .LicenseSpdxID )
164+ }
165+
166+ return fmt .Errorf ("the following licenses are unknown or incompatible with the main license, please check manually: %v\n %v" , mainLicenseSpdxID , s )
147167 }
148168
149169 return nil
0 commit comments