1
0
Fork 0
mirror of https://github.com/Luzifer/cloudkeys-go.git synced 2024-11-08 14:10:05 +00:00
cloudkeys-go/storage.go

44 lines
1 KiB
Go
Raw Normal View History

2015-07-29 07:01:23 +00:00
package main
import (
"context"
2015-07-29 07:01:23 +00:00
"fmt"
"io"
"net/url"
)
var (
storageAdapters = map[string]storageAdapterInitializer{}
)
type storageAdapter interface {
Write(ctx context.Context, identifier string, data io.Reader) error
Read(ctx context.Context, identifier string) (io.Reader, error)
IsPresent(ctx context.Context, identifier string) bool
Backup(ctx context.Context, identifier string) error
2015-07-29 07:01:23 +00:00
}
type storageAdapterInitializer func(*url.URL) (storageAdapter, error)
func getStorageAdapter(cfg *config) (storageAdapter, error) {
storageURI, _ := cfg.ParsedStorage()
if sa, ok := storageAdapters[storageURI.Scheme]; ok {
s, err := sa(storageURI)
if err != nil {
return nil, err
}
return s, nil
}
return nil, fmt.Errorf("Did not find storage adapter for '%s'", storageURI.Scheme)
}
func registerStorage(scheme string, f storageAdapterInitializer) error {
if _, ok := storageAdapters[scheme]; ok {
return fmt.Errorf("Cannot register '%s', is already registered", scheme)
}
storageAdapters[scheme] = f
return nil
}