github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/registry/middleware/repository/middleware.go (about) 1 package middleware 2 3 import ( 4 "fmt" 5 6 "github.com/docker/distribution" 7 "github.com/docker/distribution/context" 8 ) 9 10 // InitFunc is the type of a RepositoryMiddleware factory function and is 11 // used to register the constructor for different RepositoryMiddleware backends. 12 type InitFunc func(ctx context.Context, repository distribution.Repository, options map[string]interface{}) (distribution.Repository, error) 13 14 var middlewares map[string]InitFunc 15 16 // Register is used to register an InitFunc for 17 // a RepositoryMiddleware backend with the given name. 18 func Register(name string, initFunc InitFunc) error { 19 if middlewares == nil { 20 middlewares = make(map[string]InitFunc) 21 } 22 if _, exists := middlewares[name]; exists { 23 return fmt.Errorf("name already registered: %s", name) 24 } 25 26 middlewares[name] = initFunc 27 28 return nil 29 } 30 31 // Get constructs a RepositoryMiddleware with the given options using the named backend. 32 func Get(ctx context.Context, name string, options map[string]interface{}, repository distribution.Repository) (distribution.Repository, error) { 33 if middlewares != nil { 34 if initFunc, exists := middlewares[name]; exists { 35 return initFunc(ctx, repository, options) 36 } 37 } 38 39 return nil, fmt.Errorf("no repository middleware registered with name: %s", name) 40 }