2014-10-29 02:15:40 +01:00
|
|
|
package factory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-08-24 13:18:39 +02:00
|
|
|
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
2014-10-29 02:15:40 +01:00
|
|
|
)
|
|
|
|
|
2014-10-29 20:14:19 +01:00
|
|
|
// driverFactories stores an internal mapping between storage driver names and their respective
|
|
|
|
// factories
|
2014-10-29 02:15:40 +01:00
|
|
|
var driverFactories = make(map[string]StorageDriverFactory)
|
|
|
|
|
2014-10-29 20:14:19 +01:00
|
|
|
// StorageDriverFactory is a factory interface for creating storagedriver.StorageDriver interfaces
|
2016-03-19 00:28:42 +01:00
|
|
|
// Storage drivers should call Register() with a factory to make the driver available by name.
|
|
|
|
// Individual StorageDriver implementations generally register with the factory via the Register
|
|
|
|
// func (below) in their init() funcs, and as such they should be imported anonymously before use.
|
|
|
|
// See below for an example of how to register and get a StorageDriver for S3
|
|
|
|
//
|
2020-08-24 13:18:39 +02:00
|
|
|
// import _ "github.com/distribution/distribution/v3/registry/storage/driver/s3-aws"
|
2016-03-19 00:28:42 +01:00
|
|
|
// s3Driver, err = factory.Create("s3", storageParams)
|
|
|
|
// // assuming no error, s3Driver is the StorageDriver that communicates with S3 according to storageParams
|
2014-10-29 02:15:40 +01:00
|
|
|
type StorageDriverFactory interface {
|
2014-10-29 20:14:19 +01:00
|
|
|
// Create returns a new storagedriver.StorageDriver with the given parameters
|
2014-10-29 02:15:40 +01:00
|
|
|
// Parameters will vary by driver and may be ignored
|
|
|
|
// Each parameter key must only consist of lowercase letters and numbers
|
2014-12-18 04:06:55 +01:00
|
|
|
Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error)
|
2014-10-29 02:15:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register makes a storage driver available by the provided name.
|
|
|
|
// If Register is called twice with the same name or if driver factory is nil, it panics.
|
2016-03-19 00:28:42 +01:00
|
|
|
// Additionally, it is not concurrency safe. Most Storage Drivers call this function
|
|
|
|
// in their init() functions. See the documentation for StorageDriverFactory for more.
|
2014-10-29 02:15:40 +01:00
|
|
|
func Register(name string, factory StorageDriverFactory) {
|
|
|
|
if factory == nil {
|
|
|
|
panic("Must not provide nil StorageDriverFactory")
|
|
|
|
}
|
|
|
|
_, registered := driverFactories[name]
|
|
|
|
if registered {
|
|
|
|
panic(fmt.Sprintf("StorageDriverFactory named %s already registered", name))
|
|
|
|
}
|
|
|
|
|
|
|
|
driverFactories[name] = factory
|
|
|
|
}
|
|
|
|
|
2015-06-30 01:39:45 +02:00
|
|
|
// Create a new storagedriver.StorageDriver with the given name and
|
|
|
|
// parameters. To use a driver, the StorageDriverFactory must first be
|
|
|
|
// registered with the given name. If no drivers are found, an
|
|
|
|
// InvalidStorageDriverError is returned
|
2014-12-18 04:06:55 +01:00
|
|
|
func Create(name string, parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
|
2014-10-29 02:15:40 +01:00
|
|
|
driverFactory, ok := driverFactories[name]
|
|
|
|
if !ok {
|
2014-12-05 23:05:37 +01:00
|
|
|
return nil, InvalidStorageDriverError{name}
|
2014-10-29 02:15:40 +01:00
|
|
|
}
|
|
|
|
return driverFactory.Create(parameters)
|
|
|
|
}
|
|
|
|
|
2014-10-29 20:14:19 +01:00
|
|
|
// InvalidStorageDriverError records an attempt to construct an unregistered storage driver
|
2014-10-29 02:15:40 +01:00
|
|
|
type InvalidStorageDriverError struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err InvalidStorageDriverError) Error() string {
|
|
|
|
return fmt.Sprintf("StorageDriver not registered: %s", err.Name)
|
|
|
|
}
|