-
Notifications
You must be signed in to change notification settings - Fork 1
/
di.go
72 lines (59 loc) · 1.93 KB
/
di.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Package di provides a mechanism to select storage providers. In a separate package so as to avoid an import cycle.
package di
import (
"context"
"errors"
"fmt"
"os"
"cloud.google.com/go/firestore"
"github.com/andrewhowdencom/x40.link/cfg"
"github.com/andrewhowdencom/x40.link/storage"
"github.com/andrewhowdencom/x40.link/storage/boltdb"
fsdb "github.com/andrewhowdencom/x40.link/storage/firestore"
"github.com/andrewhowdencom/x40.link/storage/memory"
"github.com/andrewhowdencom/x40.link/storage/yaml"
"github.com/spf13/viper"
)
// Err* are sentinel errors
var (
ErrCannotResolveStorage = errors.New("failed to find a possible storage configuration")
)
// WireStorage generates a storage engine from the Viper based configuration. Fails
// if there are no configuration values supplied.
//
// Doesn't actually use wire (yet)
//
// TODO: Rewrite this with the new configuration format.
func WireStorage() (storage.Storer, error) {
if viper.GetBool(cfg.StorageHashMap.Path) {
return memory.NewHashTable(), nil
}
if path := viper.GetString(cfg.StorageYamlFile.Path); path != "" {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrCannotResolveStorage, err)
}
y, err := yaml.New(memory.NewHashTable(), f)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrCannotResolveStorage, err)
}
return y, nil
}
if path := viper.GetString(cfg.StorageBoltDBFile.Path); path != "" {
db, err := boltdb.New(path)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrCannotResolveStorage, err)
}
return db, nil
}
if project := viper.GetString(cfg.StorageFirestoreProject.Path); project != "" {
client, err := firestore.NewClient(context.Background(), project)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrCannotResolveStorage, err)
}
return fsdb.Firestore{
Client: client,
}, nil
}
return nil, fmt.Errorf("%w: %s", ErrCannotResolveStorage, "no valid storage provider supplied")
}