github.com/lusis/distribution@v2.0.1+incompatible/registry/middleware/repository/middleware.go (about)

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