diff --git a/tools/compliance/cmd/bom/bom.go b/tools/compliance/cmd/bom/bom.go index aaec786da4..5363a59192 100644 --- a/tools/compliance/cmd/bom/bom.go +++ b/tools/compliance/cmd/bom/bom.go @@ -29,7 +29,7 @@ import ( var ( outputFile = flag.String("o", "-", "Where to write the bill of materials. (default stdout)") - stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root") + stripPrefix = newMultiString("strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)") failNoneRequested = fmt.Errorf("\nNo license metadata files requested") failNoLicenses = fmt.Errorf("No licenses found") @@ -39,7 +39,20 @@ type context struct { stdout io.Writer stderr io.Writer rootFS fs.FS - stripPrefix string + stripPrefix []string +} + +func (ctx context) strip(installPath string) string { + for _, prefix := range ctx.stripPrefix { + if strings.HasPrefix(installPath, prefix) { + p := strings.TrimPrefix(installPath, prefix) + if 0 == len(p) { + continue + } + return p + } + } + return installPath } func init() { @@ -54,6 +67,19 @@ Options: } } +// newMultiString creates a flag that allows multiple values in an array. +func newMultiString(name, usage string) *multiString { + var f multiString + flag.Var(&f, name, usage) + return &f +} + +// multiString implements the flag `Value` interface for multiple strings. +type multiString []string + +func (ms *multiString) String() string { return strings.Join(*ms, ", ") } +func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil } + func main() { flag.Parse() @@ -135,11 +161,7 @@ func billOfMaterials(ctx *context, files ...string) error { } for path := range ni.InstallPaths() { - if 0 < len(ctx.stripPrefix) && strings.HasPrefix(path, ctx.stripPrefix) { - fmt.Fprintln(ctx.stdout, path[len(ctx.stripPrefix):]) - } else { - fmt.Fprintln(ctx.stdout, path) - } + fmt.Fprintln(ctx.stdout, ctx.strip(path)) } return nil } diff --git a/tools/compliance/cmd/bom/bom_test.go b/tools/compliance/cmd/bom/bom_test.go index 163ac2aaec..4a9889f3f7 100644 --- a/tools/compliance/cmd/bom/bom_test.go +++ b/tools/compliance/cmd/bom/bom_test.go @@ -282,7 +282,7 @@ func Test(t *testing.T) { rootFiles = append(rootFiles, "testdata/"+tt.condition+"/"+r) } - ctx := context{stdout, stderr, os.DirFS("."), tt.stripPrefix} + ctx := context{stdout, stderr, os.DirFS("."), []string{tt.stripPrefix}} err := billOfMaterials(&ctx, rootFiles...) if err != nil { diff --git a/tools/compliance/cmd/dumpgraph/dumpgraph.go b/tools/compliance/cmd/dumpgraph/dumpgraph.go index 02ab0258b6..fa16b1bdf0 100644 --- a/tools/compliance/cmd/dumpgraph/dumpgraph.go +++ b/tools/compliance/cmd/dumpgraph/dumpgraph.go @@ -29,7 +29,7 @@ import ( var ( graphViz = flag.Bool("dot", false, "Whether to output graphviz (i.e. dot) format.") labelConditions = flag.Bool("label_conditions", false, "Whether to label target nodes with conditions.") - stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root") + stripPrefix = newMultiString("strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)") failNoneRequested = fmt.Errorf("\nNo license metadata files requested") failNoLicenses = fmt.Errorf("No licenses found") @@ -38,7 +38,20 @@ var ( type context struct { graphViz bool labelConditions bool - stripPrefix string + stripPrefix []string +} + +func (ctx context) strip(installPath string) string { + for _, prefix := range ctx.stripPrefix { + if strings.HasPrefix(installPath, prefix) { + p := strings.TrimPrefix(installPath, prefix) + if 0 == len(p) { + continue + } + return p + } + } + return installPath } func init() { @@ -60,6 +73,19 @@ Options: } } +// newMultiString creates a flag that allows multiple values in an array. +func newMultiString(name, usage string) *multiString { + var f multiString + flag.Var(&f, name, usage) + return &f +} + +// multiString implements the flag `Value` interface for multiple strings. +type multiString []string + +func (ms *multiString) String() string { return strings.Join(*ms, ", ") } +func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil } + func main() { flag.Parse() @@ -107,7 +133,7 @@ func dumpGraph(ctx *context, stdout, stderr io.Writer, files ...string) error { // targetOut calculates the string to output for `target` separating conditions as needed using `sep`. targetOut := func(target *compliance.TargetNode, sep string) string { - tOut := strings.TrimPrefix(target.Name(), ctx.stripPrefix) + tOut := ctx.strip(target.Name()) if ctx.labelConditions { conditions := target.LicenseConditions().Names() sort.Strings(conditions) diff --git a/tools/compliance/cmd/dumpgraph/dumpgraph_test.go b/tools/compliance/cmd/dumpgraph/dumpgraph_test.go index 516c1db04f..67b2b40204 100644 --- a/tools/compliance/cmd/dumpgraph/dumpgraph_test.go +++ b/tools/compliance/cmd/dumpgraph/dumpgraph_test.go @@ -59,7 +59,7 @@ func Test_plaintext(t *testing.T) { condition: "firstparty", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/firstparty/"}, + ctx: context{stripPrefix: []string{"testdata/firstparty/"}}, expectedOut: []string{ "bin/bin1.meta_lic lib/liba.so.meta_lic static", "bin/bin1.meta_lic lib/libc.a.meta_lic static", @@ -75,7 +75,7 @@ func Test_plaintext(t *testing.T) { condition: "firstparty", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/firstparty/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/firstparty/"}, labelConditions: true}, expectedOut: []string{ "bin/bin1.meta_lic:notice lib/liba.so.meta_lic:notice static", "bin/bin1.meta_lic:notice lib/libc.a.meta_lic:notice static", @@ -146,7 +146,7 @@ func Test_plaintext(t *testing.T) { condition: "notice", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/notice/"}, + ctx: context{stripPrefix: []string{"testdata/notice/"}}, expectedOut: []string{ "bin/bin1.meta_lic lib/liba.so.meta_lic static", "bin/bin1.meta_lic lib/libc.a.meta_lic static", @@ -162,7 +162,7 @@ func Test_plaintext(t *testing.T) { condition: "notice", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/notice/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/notice/"}, labelConditions: true}, expectedOut: []string{ "bin/bin1.meta_lic:notice lib/liba.so.meta_lic:notice static", "bin/bin1.meta_lic:notice lib/libc.a.meta_lic:notice static", @@ -233,7 +233,7 @@ func Test_plaintext(t *testing.T) { condition: "reciprocal", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/reciprocal/"}, + ctx: context{stripPrefix: []string{"testdata/reciprocal/"}}, expectedOut: []string{ "bin/bin1.meta_lic lib/liba.so.meta_lic static", "bin/bin1.meta_lic lib/libc.a.meta_lic static", @@ -249,7 +249,7 @@ func Test_plaintext(t *testing.T) { condition: "reciprocal", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/reciprocal/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/reciprocal/"}, labelConditions: true}, expectedOut: []string{ "bin/bin1.meta_lic:notice lib/liba.so.meta_lic:reciprocal static", "bin/bin1.meta_lic:notice lib/libc.a.meta_lic:reciprocal static", @@ -320,7 +320,7 @@ func Test_plaintext(t *testing.T) { condition: "restricted", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/restricted/"}, + ctx: context{stripPrefix: []string{"testdata/restricted/"}}, expectedOut: []string{ "bin/bin1.meta_lic lib/liba.so.meta_lic static", "bin/bin1.meta_lic lib/libc.a.meta_lic static", @@ -336,7 +336,7 @@ func Test_plaintext(t *testing.T) { condition: "restricted", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/restricted/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/restricted/"}, labelConditions: true}, expectedOut: []string{ "bin/bin1.meta_lic:notice lib/liba.so.meta_lic:restricted_allows_dynamic_linking static", "bin/bin1.meta_lic:notice lib/libc.a.meta_lic:reciprocal static", @@ -407,7 +407,7 @@ func Test_plaintext(t *testing.T) { condition: "proprietary", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/proprietary/"}, + ctx: context{stripPrefix: []string{"testdata/proprietary/"}}, expectedOut: []string{ "bin/bin1.meta_lic lib/liba.so.meta_lic static", "bin/bin1.meta_lic lib/libc.a.meta_lic static", @@ -423,7 +423,7 @@ func Test_plaintext(t *testing.T) { condition: "proprietary", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/proprietary/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/proprietary/"}, labelConditions: true}, expectedOut: []string{ "bin/bin1.meta_lic:notice lib/liba.so.meta_lic:by_exception_only:proprietary static", "bin/bin1.meta_lic:notice lib/libc.a.meta_lic:by_exception_only:proprietary static", @@ -613,7 +613,7 @@ func Test_graphviz(t *testing.T) { condition: "firstparty", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/firstparty/"}, + ctx: context{stripPrefix: []string{"testdata/firstparty/"}}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), matchTarget("bin/bin2.meta_lic"), @@ -636,7 +636,7 @@ func Test_graphviz(t *testing.T) { condition: "firstparty", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/firstparty/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/firstparty/"}, labelConditions: true}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic", "notice"), matchTarget("bin/bin2.meta_lic", "notice"), @@ -735,7 +735,7 @@ func Test_graphviz(t *testing.T) { condition: "notice", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/notice/"}, + ctx: context{stripPrefix: []string{"testdata/notice/"}}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), matchTarget("bin/bin2.meta_lic"), @@ -758,7 +758,7 @@ func Test_graphviz(t *testing.T) { condition: "notice", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/notice/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/notice/"}, labelConditions: true}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic", "notice"), matchTarget("bin/bin2.meta_lic", "notice"), @@ -857,7 +857,7 @@ func Test_graphviz(t *testing.T) { condition: "reciprocal", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/reciprocal/"}, + ctx: context{stripPrefix: []string{"testdata/reciprocal/"}}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), matchTarget("bin/bin2.meta_lic"), @@ -880,7 +880,7 @@ func Test_graphviz(t *testing.T) { condition: "reciprocal", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/reciprocal/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/reciprocal/"}, labelConditions: true}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic", "notice"), matchTarget("bin/bin2.meta_lic", "notice"), @@ -979,7 +979,7 @@ func Test_graphviz(t *testing.T) { condition: "restricted", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/restricted/"}, + ctx: context{stripPrefix: []string{"testdata/restricted/"}}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), matchTarget("bin/bin2.meta_lic"), @@ -1002,7 +1002,7 @@ func Test_graphviz(t *testing.T) { condition: "restricted", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/restricted/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/restricted/"}, labelConditions: true}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic", "notice"), matchTarget("bin/bin2.meta_lic", "notice"), @@ -1101,7 +1101,7 @@ func Test_graphviz(t *testing.T) { condition: "proprietary", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/proprietary/"}, + ctx: context{stripPrefix: []string{"testdata/proprietary/"}}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), matchTarget("bin/bin2.meta_lic"), @@ -1124,7 +1124,7 @@ func Test_graphviz(t *testing.T) { condition: "proprietary", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/proprietary/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/proprietary/"}, labelConditions: true}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic", "notice"), matchTarget("bin/bin2.meta_lic", "by_exception_only", "proprietary"), diff --git a/tools/compliance/cmd/dumpresolutions/dumpresolutions.go b/tools/compliance/cmd/dumpresolutions/dumpresolutions.go index 463dfadb65..9c5e972e97 100644 --- a/tools/compliance/cmd/dumpresolutions/dumpresolutions.go +++ b/tools/compliance/cmd/dumpresolutions/dumpresolutions.go @@ -30,7 +30,7 @@ var ( conditions = newMultiString("c", "License condition to resolve. (may be given multiple times)") graphViz = flag.Bool("dot", false, "Whether to output graphviz (i.e. dot) format.") labelConditions = flag.Bool("label_conditions", false, "Whether to label target nodes with conditions.") - stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root") + stripPrefix = newMultiString("strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)") failNoneRequested = fmt.Errorf("\nNo license metadata files requested") failNoLicenses = fmt.Errorf("No licenses found") @@ -40,7 +40,20 @@ type context struct { conditions []compliance.LicenseCondition graphViz bool labelConditions bool - stripPrefix string + stripPrefix []string +} + +func (ctx context) strip(installPath string) string { + for _, prefix := range ctx.stripPrefix { + if strings.HasPrefix(installPath, prefix) { + p := strings.TrimPrefix(installPath, prefix) + if 0 == len(p) { + continue + } + return p + } + } + return installPath } func init() { @@ -140,7 +153,7 @@ func dumpResolutions(ctx *context, stdout, stderr io.Writer, files ...string) (* // targetOut calculates the string to output for `target` adding `sep`-separated conditions as needed. targetOut := func(target *compliance.TargetNode, sep string) string { - tOut := strings.TrimPrefix(target.Name(), ctx.stripPrefix) + tOut := ctx.strip(target.Name()) if ctx.labelConditions { conditions := target.LicenseConditions().Names() if len(conditions) > 0 { diff --git a/tools/compliance/cmd/dumpresolutions/dumpresolutions_test.go b/tools/compliance/cmd/dumpresolutions/dumpresolutions_test.go index b7ccdc5f10..6fe1e8ab58 100644 --- a/tools/compliance/cmd/dumpresolutions/dumpresolutions_test.go +++ b/tools/compliance/cmd/dumpresolutions/dumpresolutions_test.go @@ -65,7 +65,7 @@ func Test_plaintext(t *testing.T) { condition: "firstparty", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/firstparty/"}, + ctx: context{stripPrefix: []string{"testdata/firstparty/"}}, expectedOut: []string{ "bin/bin1.meta_lic bin/bin1.meta_lic notice", "bin/bin1.meta_lic lib/liba.so.meta_lic notice", @@ -87,7 +87,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: []compliance.LicenseCondition{compliance.NoticeCondition}, - stripPrefix: "testdata/firstparty/", + stripPrefix: []string{"testdata/firstparty/"}, }, expectedOut: []string{ "bin/bin1.meta_lic bin/bin1.meta_lic notice", @@ -110,7 +110,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.AsList(), - stripPrefix: "testdata/firstparty/", + stripPrefix: []string{"testdata/firstparty/"}, }, expectedOut: []string{}, }, @@ -120,7 +120,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesPrivate.AsList(), - stripPrefix: "testdata/firstparty/", + stripPrefix: []string{"testdata/firstparty/"}, }, expectedOut: []string{}, }, @@ -130,7 +130,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: append(compliance.ImpliesPrivate.AsList(), compliance.ImpliesShared.AsList()...), - stripPrefix: "testdata/firstparty/", + stripPrefix: []string{"testdata/firstparty/"}, }, expectedOut: []string{}, }, @@ -138,7 +138,7 @@ func Test_plaintext(t *testing.T) { condition: "firstparty", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/firstparty/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/firstparty/"}, labelConditions: true}, expectedOut: []string{ "bin/bin1.meta_lic:notice bin/bin1.meta_lic:notice notice", "bin/bin1.meta_lic:notice lib/liba.so.meta_lic:notice notice", @@ -223,7 +223,7 @@ func Test_plaintext(t *testing.T) { condition: "notice", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/notice/"}, + ctx: context{stripPrefix: []string{"testdata/notice/"}}, expectedOut: []string{ "bin/bin1.meta_lic bin/bin1.meta_lic notice", "bin/bin1.meta_lic lib/liba.so.meta_lic notice", @@ -245,7 +245,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: []compliance.LicenseCondition{compliance.NoticeCondition}, - stripPrefix: "testdata/notice/", + stripPrefix: []string{"testdata/notice/"}, }, expectedOut: []string{ "bin/bin1.meta_lic bin/bin1.meta_lic notice", @@ -268,7 +268,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.AsList(), - stripPrefix: "testdata/notice/", + stripPrefix: []string{"testdata/notice/"}, }, expectedOut: []string{}, }, @@ -278,7 +278,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesPrivate.AsList(), - stripPrefix: "testdata/notice/", + stripPrefix: []string{"testdata/notice/"}, }, expectedOut: []string{}, }, @@ -288,7 +288,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: append(compliance.ImpliesShared.AsList(), compliance.ImpliesPrivate.AsList()...), - stripPrefix: "testdata/notice/", + stripPrefix: []string{"testdata/notice/"}, }, expectedOut: []string{}, }, @@ -296,7 +296,7 @@ func Test_plaintext(t *testing.T) { condition: "notice", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/notice/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/notice/"}, labelConditions: true}, expectedOut: []string{ "bin/bin1.meta_lic:notice bin/bin1.meta_lic:notice notice", "bin/bin1.meta_lic:notice lib/liba.so.meta_lic:notice notice", @@ -381,7 +381,7 @@ func Test_plaintext(t *testing.T) { condition: "reciprocal", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/reciprocal/"}, + ctx: context{stripPrefix: []string{"testdata/reciprocal/"}}, expectedOut: []string{ "bin/bin1.meta_lic bin/bin1.meta_lic notice", "bin/bin1.meta_lic lib/liba.so.meta_lic reciprocal", @@ -403,7 +403,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: []compliance.LicenseCondition{compliance.NoticeCondition}, - stripPrefix: "testdata/reciprocal/", + stripPrefix: []string{"testdata/reciprocal/"}, }, expectedOut: []string{ "bin/bin1.meta_lic bin/bin1.meta_lic notice", @@ -421,7 +421,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.AsList(), - stripPrefix: "testdata/reciprocal/", + stripPrefix: []string{"testdata/reciprocal/"}, }, expectedOut: []string{ "bin/bin1.meta_lic lib/liba.so.meta_lic reciprocal", @@ -437,7 +437,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesPrivate.AsList(), - stripPrefix: "testdata/reciprocal/", + stripPrefix: []string{"testdata/reciprocal/"}, }, expectedOut: []string{}, }, @@ -447,7 +447,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: append(compliance.ImpliesShared.AsList(), compliance.ImpliesPrivate.AsList()...), - stripPrefix: "testdata/reciprocal/", + stripPrefix: []string{"testdata/reciprocal/"}, }, expectedOut: []string{ "bin/bin1.meta_lic lib/liba.so.meta_lic reciprocal", @@ -461,7 +461,7 @@ func Test_plaintext(t *testing.T) { condition: "reciprocal", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/reciprocal/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/reciprocal/"}, labelConditions: true}, expectedOut: []string{ "bin/bin1.meta_lic:notice bin/bin1.meta_lic:notice notice", "bin/bin1.meta_lic:notice lib/liba.so.meta_lic:reciprocal reciprocal", @@ -547,7 +547,7 @@ func Test_plaintext(t *testing.T) { condition: "restricted", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/restricted/"}, + ctx: context{stripPrefix: []string{"testdata/restricted/"}}, expectedOut: []string{ "bin/bin1.meta_lic bin/bin1.meta_lic notice:restricted_allows_dynamic_linking", "bin/bin1.meta_lic lib/liba.so.meta_lic restricted_allows_dynamic_linking", @@ -570,7 +570,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: []compliance.LicenseCondition{compliance.NoticeCondition}, - stripPrefix: "testdata/restricted/", + stripPrefix: []string{"testdata/restricted/"}, }, expectedOut: []string{ "bin/bin1.meta_lic bin/bin1.meta_lic notice", @@ -586,7 +586,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.AsList(), - stripPrefix: "testdata/restricted/", + stripPrefix: []string{"testdata/restricted/"}, }, expectedOut: []string{ "bin/bin1.meta_lic bin/bin1.meta_lic restricted_allows_dynamic_linking", @@ -610,7 +610,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesPrivate.AsList(), - stripPrefix: "testdata/restricted/", + stripPrefix: []string{"testdata/restricted/"}, }, expectedOut: []string{}, }, @@ -620,7 +620,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: append(compliance.ImpliesShared.AsList(), compliance.ImpliesPrivate.AsList()...), - stripPrefix: "testdata/restricted/", + stripPrefix: []string{"testdata/restricted/"}, }, expectedOut: []string{ "bin/bin1.meta_lic bin/bin1.meta_lic restricted_allows_dynamic_linking", @@ -642,7 +642,7 @@ func Test_plaintext(t *testing.T) { condition: "restricted", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/restricted/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/restricted/"}, labelConditions: true}, expectedOut: []string{ "bin/bin1.meta_lic:notice bin/bin1.meta_lic:notice notice:restricted_allows_dynamic_linking", "bin/bin1.meta_lic:notice lib/liba.so.meta_lic:restricted_allows_dynamic_linking restricted_allows_dynamic_linking", @@ -730,7 +730,7 @@ func Test_plaintext(t *testing.T) { condition: "proprietary", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/proprietary/"}, + ctx: context{stripPrefix: []string{"testdata/proprietary/"}}, expectedOut: []string{ "bin/bin1.meta_lic bin/bin1.meta_lic notice", "bin/bin1.meta_lic lib/liba.so.meta_lic proprietary:by_exception_only", @@ -753,7 +753,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: []compliance.LicenseCondition{compliance.NoticeCondition}, - stripPrefix: "testdata/proprietary/", + stripPrefix: []string{"testdata/proprietary/"}, }, expectedOut: []string{ "bin/bin1.meta_lic bin/bin1.meta_lic notice", @@ -767,7 +767,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.AsList(), - stripPrefix: "testdata/proprietary/", + stripPrefix: []string{"testdata/proprietary/"}, }, expectedOut: []string{ "bin/bin2.meta_lic bin/bin2.meta_lic restricted", @@ -784,7 +784,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesPrivate.AsList(), - stripPrefix: "testdata/proprietary/", + stripPrefix: []string{"testdata/proprietary/"}, }, expectedOut: []string{ "bin/bin1.meta_lic lib/liba.so.meta_lic proprietary", @@ -802,7 +802,7 @@ func Test_plaintext(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: append(compliance.ImpliesShared.AsList(), compliance.ImpliesPrivate.AsList()...), - stripPrefix: "testdata/proprietary/", + stripPrefix: []string{"testdata/proprietary/"}, }, expectedOut: []string{ "bin/bin1.meta_lic lib/liba.so.meta_lic proprietary", @@ -822,7 +822,7 @@ func Test_plaintext(t *testing.T) { condition: "proprietary", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/proprietary/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/proprietary/"}, labelConditions: true}, expectedOut: []string{ "bin/bin1.meta_lic:notice bin/bin1.meta_lic:notice notice", "bin/bin1.meta_lic:notice lib/liba.so.meta_lic:proprietary:by_exception_only proprietary:by_exception_only", @@ -1080,7 +1080,7 @@ func Test_graphviz(t *testing.T) { condition: "firstparty", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/firstparty/"}, + ctx: context{stripPrefix: []string{"testdata/firstparty/"}}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), matchTarget("lib/liba.so.meta_lic"), @@ -1144,7 +1144,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: []compliance.LicenseCondition{compliance.NoticeCondition}, - stripPrefix: "testdata/firstparty/", + stripPrefix: []string{"testdata/firstparty/"}, }, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), @@ -1209,7 +1209,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.AsList(), - stripPrefix: "testdata/firstparty/", + stripPrefix: []string{"testdata/firstparty/"}, }, expectedOut: []getMatcher{}, }, @@ -1219,7 +1219,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesPrivate.AsList(), - stripPrefix: "testdata/firstparty/", + stripPrefix: []string{"testdata/firstparty/"}, }, expectedOut: []getMatcher{}, }, @@ -1229,7 +1229,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.Union(compliance.ImpliesPrivate).AsList(), - stripPrefix: "testdata/firstparty/", + stripPrefix: []string{"testdata/firstparty/"}, }, expectedOut: []getMatcher{}, }, @@ -1237,7 +1237,7 @@ func Test_graphviz(t *testing.T) { condition: "firstparty", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/firstparty/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/firstparty/"}, labelConditions: true}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic", "notice"), matchTarget("lib/liba.so.meta_lic", "notice"), @@ -1472,7 +1472,7 @@ func Test_graphviz(t *testing.T) { condition: "notice", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/notice/"}, + ctx: context{stripPrefix: []string{"testdata/notice/"}}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), matchTarget("lib/liba.so.meta_lic"), @@ -1536,7 +1536,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: []compliance.LicenseCondition{compliance.NoticeCondition}, - stripPrefix: "testdata/notice/", + stripPrefix: []string{"testdata/notice/"}, }, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), @@ -1601,7 +1601,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.AsList(), - stripPrefix: "testdata/notice/", + stripPrefix: []string{"testdata/notice/"}, }, expectedOut: []getMatcher{}, }, @@ -1611,7 +1611,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesPrivate.AsList(), - stripPrefix: "testdata/notice/", + stripPrefix: []string{"testdata/notice/"}, }, expectedOut: []getMatcher{}, }, @@ -1621,7 +1621,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.Union(compliance.ImpliesPrivate).AsList(), - stripPrefix: "testdata/notice/", + stripPrefix: []string{"testdata/notice/"}, }, expectedOut: []getMatcher{}, }, @@ -1629,7 +1629,7 @@ func Test_graphviz(t *testing.T) { condition: "notice", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/notice/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/notice/"}, labelConditions: true}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic", "notice"), matchTarget("lib/liba.so.meta_lic", "notice"), @@ -1864,7 +1864,7 @@ func Test_graphviz(t *testing.T) { condition: "reciprocal", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/reciprocal/"}, + ctx: context{stripPrefix: []string{"testdata/reciprocal/"}}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), matchTarget("lib/liba.so.meta_lic"), @@ -1928,7 +1928,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: []compliance.LicenseCondition{compliance.NoticeCondition}, - stripPrefix: "testdata/reciprocal/", + stripPrefix: []string{"testdata/reciprocal/"}, }, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), @@ -1971,7 +1971,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.AsList(), - stripPrefix: "testdata/reciprocal/", + stripPrefix: []string{"testdata/reciprocal/"}, }, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), @@ -2006,7 +2006,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesPrivate.AsList(), - stripPrefix: "testdata/reciprocal/", + stripPrefix: []string{"testdata/reciprocal/"}, }, expectedOut: []getMatcher{}, }, @@ -2016,7 +2016,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.Union(compliance.ImpliesPrivate).AsList(), - stripPrefix: "testdata/reciprocal/", + stripPrefix: []string{"testdata/reciprocal/"}, }, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), @@ -2049,7 +2049,7 @@ func Test_graphviz(t *testing.T) { condition: "reciprocal", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/reciprocal/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/reciprocal/"}, labelConditions: true}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic", "notice"), matchTarget("lib/liba.so.meta_lic", "reciprocal"), @@ -2296,7 +2296,7 @@ func Test_graphviz(t *testing.T) { condition: "restricted", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/restricted/"}, + ctx: context{stripPrefix: []string{"testdata/restricted/"}}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), matchTarget("lib/liba.so.meta_lic"), @@ -2372,7 +2372,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: []compliance.LicenseCondition{compliance.NoticeCondition}, - stripPrefix: "testdata/restricted/", + stripPrefix: []string{"testdata/restricted/"}, }, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), @@ -2406,7 +2406,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.AsList(), - stripPrefix: "testdata/restricted/", + stripPrefix: []string{"testdata/restricted/"}, }, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), @@ -2478,7 +2478,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesPrivate.AsList(), - stripPrefix: "testdata/restricted/", + stripPrefix: []string{"testdata/restricted/"}, }, expectedOut: []getMatcher{}, }, @@ -2488,7 +2488,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.Union(compliance.ImpliesPrivate).AsList(), - stripPrefix: "testdata/restricted/", + stripPrefix: []string{"testdata/restricted/"}, }, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), @@ -2558,7 +2558,7 @@ func Test_graphviz(t *testing.T) { condition: "restricted", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/restricted/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/restricted/"}, labelConditions: true}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic", "notice"), matchTarget("lib/liba.so.meta_lic", "restricted_allows_dynamic_linking"), @@ -2836,7 +2836,7 @@ func Test_graphviz(t *testing.T) { condition: "proprietary", name: "apex_trimmed", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/proprietary/"}, + ctx: context{stripPrefix: []string{"testdata/proprietary/"}}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), matchTarget("lib/liba.so.meta_lic"), @@ -2914,7 +2914,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: []compliance.LicenseCondition{compliance.NoticeCondition}, - stripPrefix: "testdata/proprietary/", + stripPrefix: []string{"testdata/proprietary/"}, }, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), @@ -2939,7 +2939,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.AsList(), - stripPrefix: "testdata/proprietary/", + stripPrefix: []string{"testdata/proprietary/"}, }, expectedOut: []getMatcher{ matchTarget("bin/bin2.meta_lic"), @@ -2977,7 +2977,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesPrivate.AsList(), - stripPrefix: "testdata/proprietary/", + stripPrefix: []string{"testdata/proprietary/"}, }, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), @@ -3021,7 +3021,7 @@ func Test_graphviz(t *testing.T) { roots: []string{"highest.apex.meta_lic"}, ctx: context{ conditions: compliance.ImpliesShared.Union(compliance.ImpliesPrivate).AsList(), - stripPrefix: "testdata/proprietary/", + stripPrefix: []string{"testdata/proprietary/"}, }, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic"), @@ -3082,7 +3082,7 @@ func Test_graphviz(t *testing.T) { condition: "proprietary", name: "apex_trimmed_labelled", roots: []string{"highest.apex.meta_lic"}, - ctx: context{stripPrefix: "testdata/proprietary/", labelConditions: true}, + ctx: context{stripPrefix: []string{"testdata/proprietary/"}, labelConditions: true}, expectedOut: []getMatcher{ matchTarget("bin/bin1.meta_lic", "notice"), matchTarget("lib/liba.so.meta_lic", "by_exception_only", "proprietary"), diff --git a/tools/compliance/cmd/htmlnotice/htmlnotice.go b/tools/compliance/cmd/htmlnotice/htmlnotice.go index 6363acfc02..938bb7d472 100644 --- a/tools/compliance/cmd/htmlnotice/htmlnotice.go +++ b/tools/compliance/cmd/htmlnotice/htmlnotice.go @@ -35,7 +35,7 @@ var ( outputFile = flag.String("o", "-", "Where to write the NOTICE text file. (default stdout)") depsFile = flag.String("d", "", "Where to write the deps file") includeTOC = flag.Bool("toc", true, "Whether to include a table of contents.") - stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root") + stripPrefix = newMultiString("strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)") title = flag.String("title", "", "The title of the notice file.") failNoneRequested = fmt.Errorf("\nNo license metadata files requested") @@ -47,11 +47,27 @@ type context struct { stderr io.Writer rootFS fs.FS includeTOC bool - stripPrefix string + stripPrefix []string title string deps *[]string } +func (ctx context) strip(installPath string) string { + for _, prefix := range ctx.stripPrefix { + if strings.HasPrefix(installPath, prefix) { + p := strings.TrimPrefix(installPath, prefix) + if 0 == len(p) { + p = ctx.title + } + if 0 == len(p) { + continue + } + return p + } + } + return installPath +} + func init() { flag.Usage = func() { fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...} @@ -65,6 +81,19 @@ Options: } } +// newMultiString creates a flag that allows multiple values in an array. +func newMultiString(name, usage string) *multiString { + var f multiString + flag.Var(&f, name, usage) + return &f +} + +// multiString implements the flag `Value` interface for multiple strings. +type multiString []string + +func (ms *multiString) String() string { return strings.Join(*ms, ", ") } +func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil } + func main() { flag.Parse() @@ -190,20 +219,7 @@ func htmlNotice(ctx *context, files ...string) error { id := fmt.Sprintf("id%d", i) i++ ids[installPath] = id - var p string - if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) { - p = installPath[len(ctx.stripPrefix):] - if 0 == len(p) { - if 0 < len(ctx.title) { - p = ctx.title - } else { - p = "root" - } - } - } else { - p = installPath - } - fmt.Fprintf(ctx.stdout, "
  • %s\n