github.com/quay/claircore@v1.5.28/updater/registry.go (about)

     1  package updater
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"net/http"
     7  	"strings"
     8  	"sync"
     9  
    10  	"github.com/quay/zlog"
    11  
    12  	"github.com/quay/claircore/libvuln/driver"
    13  )
    14  
    15  var pkg = struct {
    16  	sync.Mutex
    17  	fs map[string]driver.UpdaterSetFactory
    18  }{
    19  	fs: make(map[string]driver.UpdaterSetFactory),
    20  }
    21  
    22  // Register registers an UpdaterSetFactory.
    23  //
    24  // Register will panic if the same name is used twice.
    25  //
    26  // Deprecated: See [Updater].
    27  func Register(name string, f driver.UpdaterSetFactory) {
    28  	pkg.Lock()
    29  	defer pkg.Unlock()
    30  	if _, ok := pkg.fs[name]; ok {
    31  		panic("")
    32  	}
    33  	pkg.fs[name] = f
    34  }
    35  
    36  // Registered returns a new map populated with the registered UpdaterSetFactories.
    37  //
    38  // Deprecated: See [Updater].
    39  func Registered() map[string]driver.UpdaterSetFactory {
    40  	pkg.Lock()
    41  	defer pkg.Unlock()
    42  	r := make(map[string]driver.UpdaterSetFactory, len(pkg.fs))
    43  	for k, v := range pkg.fs {
    44  		r[k] = v
    45  	}
    46  	return r
    47  }
    48  
    49  // Configure calls the Configure method on all the passed-in
    50  // UpdaterSetFactories.
    51  //
    52  // Deprecated: See [Updater].
    53  func Configure(ctx context.Context, fs map[string]driver.UpdaterSetFactory, cfg map[string]driver.ConfigUnmarshaler, c *http.Client) error {
    54  	if c == nil {
    55  		return errors.New("passed invalid *http.Client")
    56  	}
    57  	errd := false
    58  	var b strings.Builder
    59  	b.WriteString("updater: errors configuring factories:")
    60  
    61  	for name, fac := range fs {
    62  		ev := zlog.Debug(ctx).
    63  			Str("factory", name)
    64  		f, ok := fac.(driver.Configurable)
    65  		if ok {
    66  			ev.Msg("configuring factory")
    67  			cf := cfg[name]
    68  			if cf == nil {
    69  				cf = noopConfig
    70  			}
    71  			if err := f.Configure(ctx, cf, c); err != nil {
    72  				errd = true
    73  				b.WriteString("\n\t")
    74  				b.WriteString(err.Error())
    75  			}
    76  		} else {
    77  			ev.Msg("factory unconfigurable")
    78  		}
    79  	}
    80  
    81  	if errd {
    82  		return errors.New(b.String())
    83  	}
    84  	return nil
    85  }
    86  
    87  // NoopConfig is used when an explicit config is not provided.
    88  func noopConfig(_ interface{}) error { return nil }