github.com/quay/claircore@v1.5.28/matchers/registry/registry.go (about) 1 // Package matchers holds a registry of default matchers. 2 package registry 3 4 import ( 5 "context" 6 "errors" 7 "net/http" 8 "strings" 9 "sync" 10 11 "github.com/quay/claircore/libvuln/driver" 12 ) 13 14 var pkg = struct { 15 sync.Mutex 16 fs map[string]driver.MatcherFactory 17 }{ 18 fs: make(map[string]driver.MatcherFactory), 19 } 20 21 // Register registers an MatcherFactory. 22 // 23 // Register will panic if the same name is used twice. 24 func Register(name string, f driver.MatcherFactory) { 25 pkg.Lock() 26 defer pkg.Unlock() 27 if _, ok := pkg.fs[name]; ok { 28 panic("") 29 } 30 pkg.fs[name] = f 31 } 32 33 // Registered returns a new map populated with the registered MatcherFactories. 34 func Registered() map[string]driver.MatcherFactory { 35 pkg.Lock() 36 defer pkg.Unlock() 37 r := make(map[string]driver.MatcherFactory, len(pkg.fs)) 38 for k, v := range pkg.fs { 39 r[k] = v 40 } 41 return r 42 } 43 44 // Configure calls the Configure method on all the passed-in 45 // MatcherFactories. 46 func Configure(ctx context.Context, fs map[string]driver.MatcherFactory, cfg map[string]driver.MatcherConfigUnmarshaler, c *http.Client) error { 47 if c == nil { 48 return errors.New("invalid *http.Client") 49 } 50 51 errd := false 52 var b strings.Builder 53 b.WriteString("matchers: errors configuring factories:") 54 55 for name, fac := range fs { 56 f, fOK := fac.(driver.MatcherConfigurable) 57 cf, cfOK := cfg[name] 58 if fOK && cfOK { 59 if err := f.Configure(ctx, cf, c); err != nil { 60 errd = true 61 b.WriteString("\n\t") 62 b.WriteString(err.Error()) 63 } 64 } 65 } 66 67 if errd { 68 return errors.New(b.String()) 69 } 70 return nil 71 }