github.com/anchore/syft@v1.38.2/internal/licenses/context.go (about)

     1  package licenses
     2  
     3  import (
     4  	"context"
     5  )
     6  
     7  type licenseScannerKey struct{}
     8  
     9  var ctxKey = licenseScannerKey{}
    10  
    11  func SetContextLicenseScanner(ctx context.Context, s Scanner) context.Context {
    12  	return context.WithValue(ctx, ctxKey, s)
    13  }
    14  
    15  func IsContextLicenseScannerSet(ctx context.Context) bool {
    16  	_, ok := ctx.Value(ctxKey).(Scanner)
    17  	return ok
    18  }
    19  
    20  func ContextLicenseScanner(ctx context.Context) (Scanner, error) {
    21  	if s, ok := ctx.Value(ctxKey).(Scanner); ok {
    22  		return s, nil
    23  	}
    24  	return NewDefaultScanner()
    25  }