/
util.go
64 lines (59 loc) · 1.93 KB
/
util.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
package cli
import (
"strings"
"github.com/micro/cli/v2"
"github.com/micro/go-micro/v2/config/cmd"
"github.com/micro/go-micro/v2/store"
"github.com/pkg/errors"
)
// makeStore is a helper function that creates a store for snapshot and restore
func makeStore(ctx *cli.Context) (store.Store, error) {
builtinStore, err := getStore(ctx.String("store"))
if err != nil {
return nil, errors.Wrap(err, "makeStore")
}
s := builtinStore(
store.Nodes(strings.Split(ctx.String("nodes"), ",")...),
store.Database(ctx.String("database")),
store.Table(ctx.String("table")),
)
if err := s.Init(); err != nil {
return nil, errors.Wrapf(err, "Couldn't init %s store", ctx.String("store"))
}
return s, nil
}
// makeStores is a helper function that sets up 2 stores for sync
func makeStores(ctx *cli.Context) (store.Store, store.Store, error) {
fromBuilder, err := getStore(ctx.String("from-backend"))
if err != nil {
return nil, nil, errors.Wrap(err, "from store")
}
toBuilder, err := getStore(ctx.String("to-backend"))
if err != nil {
return nil, nil, errors.Wrap(err, "to store")
}
from := fromBuilder(
store.Nodes(strings.Split(ctx.String("from-nodes"), ",")...),
store.Database(ctx.String("from-database")),
store.Table(ctx.String("from-table")),
)
if err := from.Init(); err != nil {
return nil, nil, errors.Wrapf(err, "from: couldn't init %s", ctx.String("from-backend"))
}
to := toBuilder(
store.Nodes(strings.Split(ctx.String("to-nodes"), ",")...),
store.Database(ctx.String("to-database")),
store.Table(ctx.String("to-table")),
)
if err := to.Init(); err != nil {
return nil, nil, errors.Wrapf(err, "to: couldn't init %s", ctx.String("to-backend"))
}
return from, to, nil
}
func getStore(s string) (func(...store.Option) store.Store, error) {
builtinStore, exists := cmd.DefaultStores[s]
if !exists {
return nil, errors.Errorf("store %s is not an implemented store - check your plugins", s)
}
return builtinStore, nil
}