/
provider.go
69 lines (54 loc) · 1.41 KB
/
provider.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
/*
Copyright Gen Digital Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package storage
import (
"fmt"
"strings"
"github.com/trustbloc/did-go/legacy/mem"
"github.com/trustbloc/kms-go/spi/storage"
"github.com/trustbloc/vcs/component/wallet-cli/internal/storage/leveldb"
"github.com/trustbloc/vcs/component/wallet-cli/internal/storage/mongodb"
)
func NewProvider(storageType string, opts ...Opt) (storage.Provider, error) {
options := &providerOpts{}
for _, opt := range opts {
opt(options)
}
switch strings.ToLower(storageType) {
case "mongodb":
if options.connectionString == "" {
return nil, fmt.Errorf("mongodb connection string is empty")
}
p, err := mongodb.NewProvider(options.connectionString)
if err != nil {
return nil, err
}
return p, nil
case "leveldb":
if options.dbPath == "" {
return nil, fmt.Errorf("leveldb db path is empty")
}
return leveldb.NewProvider(options.dbPath), nil
case "mem":
return mem.NewProvider(), nil
default:
return nil, fmt.Errorf("unsupported storage type: %s", storageType)
}
}
type providerOpts struct {
connectionString string
dbPath string
}
type Opt func(opts *providerOpts)
func WithConnectionString(connectionString string) Opt {
return func(opts *providerOpts) {
opts.connectionString = connectionString
}
}
func WithDBPath(dbPath string) Opt {
return func(opts *providerOpts) {
opts.dbPath = dbPath
}
}