forked from Luzifer/cloudkeys-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.go
More file actions
43 lines (35 loc) · 1.02 KB
/
storage.go
File metadata and controls
43 lines (35 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"context"
"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
}
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
}