mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 08:23:15 +01:00
Perf: skip license scanner injection (#3796)
* (perf): allow library users to skip default scanner injection Signed-off-by: Adam McClenaghan <adam@mcclenaghan.co.uk> * (perf): remove prints Signed-off-by: Adam McClenaghan <adam@mcclenaghan.co.uk> * perf: move to cataloging licenses.go Signed-off-by: adammcclenaghan <adam.mcclenaghan@upwind.io> * perf: Simplify to expose a SetContextLicenseScanner func Signed-off-by: adammcclenaghan <adam.mcclenaghan@upwind.io> --------- Signed-off-by: Adam McClenaghan <adam@mcclenaghan.co.uk> Signed-off-by: adammcclenaghan <adam.mcclenaghan@upwind.io>
This commit is contained in:
parent
273d414b6b
commit
f6d4a7d27a
@ -6,12 +6,19 @@ import (
|
||||
|
||||
type licenseScannerKey struct{}
|
||||
|
||||
var ctxKey = licenseScannerKey{}
|
||||
|
||||
func SetContextLicenseScanner(ctx context.Context, s Scanner) context.Context {
|
||||
return context.WithValue(ctx, licenseScannerKey{}, s)
|
||||
return context.WithValue(ctx, ctxKey, s)
|
||||
}
|
||||
|
||||
func IsContextLicenseScannerSet(ctx context.Context) bool {
|
||||
_, ok := ctx.Value(ctxKey).(Scanner)
|
||||
return ok
|
||||
}
|
||||
|
||||
func ContextLicenseScanner(ctx context.Context) (Scanner, error) {
|
||||
if s, ok := ctx.Value(licenseScannerKey{}).(Scanner); ok {
|
||||
if s, ok := ctx.Value(ctxKey).(Scanner); ok {
|
||||
return s, nil
|
||||
}
|
||||
return NewDefaultScanner()
|
||||
|
||||
47
internal/licenses/context_test.go
Normal file
47
internal/licenses/context_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
package licenses
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSetContextLicenseScanner(t *testing.T) {
|
||||
scanner := testScanner(true)
|
||||
ctx := context.Background()
|
||||
ctx = SetContextLicenseScanner(ctx, scanner)
|
||||
|
||||
val := ctx.Value(ctxKey)
|
||||
require.NotNil(t, val)
|
||||
s, ok := val.(Scanner)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, scanner, s)
|
||||
}
|
||||
|
||||
func TestIsContextLicenseScannerSet(t *testing.T) {
|
||||
scanner := testScanner(true)
|
||||
ctx := context.Background()
|
||||
require.False(t, IsContextLicenseScannerSet(ctx))
|
||||
|
||||
ctx = SetContextLicenseScanner(ctx, scanner)
|
||||
require.True(t, IsContextLicenseScannerSet(ctx))
|
||||
}
|
||||
|
||||
func TestContextLicenseScanner(t *testing.T) {
|
||||
t.Run("with scanner", func(t *testing.T) {
|
||||
scanner := testScanner(true)
|
||||
ctx := SetContextLicenseScanner(context.Background(), scanner)
|
||||
s, err := ContextLicenseScanner(ctx)
|
||||
if err != nil || s != scanner {
|
||||
t.Fatal("expected scanner from context")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("without scanner", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := ContextLicenseScanner(ctx)
|
||||
if err != nil || s == nil {
|
||||
t.Fatal("expected default scanner")
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -96,14 +96,21 @@ func setupContext(ctx context.Context, cfg *CreateSBOMConfig) (context.Context,
|
||||
ctx = setContextExecutors(ctx, cfg)
|
||||
|
||||
// configure license scanner
|
||||
return setContextLicenseScanner(ctx, cfg)
|
||||
// skip injecting a license scanner if one already set on context
|
||||
if licenses.IsContextLicenseScannerSet(ctx) {
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func setContextLicenseScanner(ctx context.Context, cfg *CreateSBOMConfig) (context.Context, error) {
|
||||
return SetContextLicenseScanner(ctx, cfg.Licenses)
|
||||
}
|
||||
|
||||
// SetContextLicenseScanner creates and sets a license scanner
|
||||
// on the provided context using the provided license config.
|
||||
func SetContextLicenseScanner(ctx context.Context, cfg cataloging.LicenseConfig) (context.Context, error) {
|
||||
// inject a single license scanner and content config for all package cataloging tasks into context
|
||||
licenseScanner, err := licenses.NewDefaultScanner(
|
||||
licenses.WithIncludeLicenseContent(cfg.Licenses.IncludeUnkownLicenseContent),
|
||||
licenses.WithCoverage(cfg.Licenses.Coverage),
|
||||
licenses.WithIncludeLicenseContent(cfg.IncludeUnkownLicenseContent),
|
||||
licenses.WithCoverage(cfg.Coverage),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not build licenseScanner for cataloging: %w", err)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user