-
Notifications
You must be signed in to change notification settings - Fork 63
/
exec.go
63 lines (55 loc) · 1.66 KB
/
exec.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
// Copyright 2020-present Kuei-chun Chen. All rights reserved.
package keyhole
import (
"fmt"
"os"
"github.com/simagix/gox"
"go.mongodb.org/mongo-driver/bson"
)
const (
compareClusters = "compare_clusters"
printConnections = "print_connections"
)
// Config stores keyhole configuration
type Config struct {
Action string `bson:"action,omitempty"`
Filename string `bson:"filename,omitempty"`
NoColor bool `bson:"no_color,omitempty"`
Signature string `bson:"signature,omitempty"`
URI string `bson:"uri,omitempty"`
Verbose bool `bson:"verbose,omitempty"`
IsDeepCompare bool `bson:"deep_compare,omitempty"`
Filters []Filter `bson:"filters,omitempty"`
SampleSize int `bson:"sample_size,omitempty"`
SourceURI string `bson:"source_uri,omitempty"`
TargetURI string `bson:"target_uri,omitempty"`
}
// Filter holds query filter of a namespace
type Filter struct {
NS string `bson:"ns"`
Query bson.D `bson:"query"`
TargetNS string `bson:"target_ns,omitempty"`
}
// Exec executes a plan based on a configuration file
func Exec(filename string, signature string) error {
var err error
var cfg *Config
var data []byte
if data, err = os.ReadFile(filename); err != nil {
return err
} else if err = bson.UnmarshalExtJSON(data, false, &cfg); err != nil {
return err
} else if cfg.Action == "" {
return fmt.Errorf(`action is required`)
}
if cfg.Signature == "" {
cfg.Signature = signature
}
gox.GetLogger(cfg.Signature).Infof("executing %v", cfg.Action)
if cfg.Action == compareClusters {
return CompareClusters(cfg)
} else if cfg.Action == printConnections {
return PrintConnections(cfg)
}
return err
}