2023-09-13 06:03:25 +02:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var blobStoreFactories = make(map[string]BlobStoreFactory, 0)
|
|
|
|
|
|
|
|
type BlobStoreFactory func(url *url.URL) (storage.BlobStore, error)
|
|
|
|
|
|
|
|
func RegisterBlobStoreFactory(scheme string, factory BlobStoreFactory) {
|
|
|
|
blobStoreFactories[scheme] = factory
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlobStore(dsn string) (storage.BlobStore, error) {
|
|
|
|
url, err := url.Parse(dsn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
factory, exists := blobStoreFactories[url.Scheme]
|
|
|
|
if !exists {
|
2023-10-24 22:52:33 +02:00
|
|
|
return nil, errors.Wrapf(ErrSchemeNotRegistered, "no driver associated with scheme '%s'", url.Scheme)
|
2023-09-13 06:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
store, err := factory(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return store, nil
|
|
|
|
}
|