From c04bbf9c82f216891ba44b8b5b621b8020963086 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Fri, 29 Sep 2017 07:57:09 +0200 Subject: Restructure package storage --- storage/registry/registry.go | 58 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) (limited to 'storage/registry') diff --git a/storage/registry/registry.go b/storage/registry/registry.go index dec7c32..ad66c6a 100644 --- a/storage/registry/registry.go +++ b/storage/registry/registry.go @@ -1,13 +1,67 @@ package registry import ( + "code.laria.me/petrific/config" "code.laria.me/petrific/storage" "code.laria.me/petrific/storage/cloud" + "code.laria.me/petrific/storage/local" + "code.laria.me/petrific/storage/memory" + "errors" + "fmt" ) // List af all available storage types var StorageTypes = map[string]storage.CreateStorageFromConfig{ - "local": storage.LocalStorageFromConfig, - "memory": storage.MemoryStorageFromConfig, + "local": local.LocalStorageFromConfig, + "memory": memory.MemoryStorageFromConfig, "openstack-swift": cloud.SwiftStorageCreator(), } + +var notFoundErr = errors.New("Storage not found") + +type unknownMethodErr string + +func (method unknownMethodErr) Error() string { + return fmt.Sprintf("Method %s unknown", string(method)) +} + +type storageConfErr struct { + name string + err error +} + +func (e storageConfErr) Error() string { + return fmt.Sprintf("Failed setting up storage %s: %s", e.name, e.err.Error()) +} + +func loadStorage(conf config.Config, storageName string) (storage.Storage, error) { + storageOptions, ok := conf.Storage[storageName] + if !ok { + return nil, notFoundErr + } + + var method string + if err := storageOptions.Get("method", &method); err != nil { + return nil, err + } + + st, ok := StorageTypes[method] + if !ok { + return nil, unknownMethodErr(method) + } + + s, err := st(conf, storageName) + if err != nil { + return nil, err + } + + return s, nil +} + +func LoadStorage(conf config.Config, storageName string) (storage.Storage, error) { + s, err := loadStorage(conf, storageName) + if err != nil { + return nil, storageConfErr{storageName, err} + } + return s, nil +} -- cgit v1.2.3-70-g09d2