-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
192 lines (161 loc) · 4.42 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"context"
"fmt"
"os"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/mongodb/mongo-tools/common/log"
"github.com/mongodb/mongo-tools/common/progress"
"github.com/mongodb/mongo-tools/common/signals"
"github.com/mongodb/mongo-tools/common/util"
"github.com/rockset/rockset-mongo/pkg/config"
"github.com/rockset/rockset-mongo/pkg/mongo"
"github.com/rockset/rockset-mongo/pkg/writers"
"golang.org/x/sync/errgroup"
)
const (
progressBarLength = 50
progressBarWaitTime = 3 * time.Second
)
var (
VersionStr = "built-without-version-string"
GitCommit = "build-without-git-commit"
)
func main() {
if os.Args[1] == "export" {
export(os.Args[2:])
return
}
run(os.Args[1:])
}
func run(args []string) {
ctx, cancelFn := context.WithCancel(context.Background())
defer cancelFn()
finishedChan := signals.HandleWithInterrupt(cancelFn)
defer close(finishedChan)
opts, err := ParseRunOptions(args, VersionStr, GitCommit)
if err != nil {
log.Logvf(log.Always, "error parsing command line options: %s", err.Error())
log.Logvf(log.Always, util.ShortUsage("mongodump"))
os.Exit(util.ExitFailure)
}
// print help, if specified
if opts.PrintHelp(false) {
return
}
// print version, if specified
if opts.PrintVersion() {
return
}
log.SetVerbosity(opts.Verbosity)
conf, err := config.ReadConfig(opts.ConfigPath)
if err != nil {
log.Logvf(log.Always, "error parsing config file: %v", err)
os.Exit(util.ExitFailure)
}
if opts.LoadOnly {
conf.LoadOnly = opts.LoadOnly
}
state, err := config.ReadState("state.json")
if err != nil {
log.Logvf(log.Always, "error parsing state file: %v", err)
os.Exit(util.ExitFailure)
}
// init logger
d := &Driver{
config: conf,
state: state,
dumpOpts: *opts.ToolOptions,
tui: false,
logLevel: log.Always,
}
var p *tea.Program
if false {
d.tui = true
d.logLevel = log.DebugLow
p = tea.NewProgram(&model{d}, tea.WithContext(ctx))
}
var errGrp errgroup.Group
errGrp.Go(func() error {
err := d.run(ctx)
if p != nil {
p.Quit()
}
return err
})
if p != nil {
if _, err := p.Run(); err != nil {
log.Logvf(log.Always, "error: %v", err)
os.Exit(util.ExitFailure)
}
cancelFn()
}
if err := errGrp.Wait(); err != nil {
log.Logvf(log.Always, "error: %v", err)
os.Exit(util.ExitFailure)
}
fmt.Println("Collection created and import is done")
}
func export(args []string) {
ctx, cancelFn := context.WithCancel(context.Background())
defer cancelFn()
finishedChan := signals.HandleWithInterrupt(cancelFn)
defer close(finishedChan)
opts, err := ParseExportOptions(args, VersionStr, GitCommit)
if err != nil {
log.Logvf(log.Always, "error parsing command line options: %s", err.Error())
log.Logvf(log.Always, util.ShortUsage("rockset-mongo export"))
os.Exit(util.ExitFailure)
}
// print help, if specified
if opts.PrintHelp(false) {
return
}
// print version, if specified
if opts.PrintVersion() {
return
}
// init logger
log.SetVerbosity(opts.Verbosity)
// verify uri options and log them
opts.URI.LogUnsupportedOptions()
dump := mongo.MongoDump{
ToolOptions: opts.ToolOptions,
}
if err = dump.Init(); err != nil {
log.Logvf(log.Always, "Failed: %v", err)
os.Exit(util.ExitFailure)
}
out, err := writers.NewWriter(ctx, &writers.WriterOptions{
Out: opts.Out,
TargetChunkSize: uint64(opts.TargetSize) * 1024 * 1024,
FilePrefix: opts.ToolOptions.DB + "." + opts.ToolOptions.Collection,
})
if err != nil {
log.Logvf(log.Always, "Failed to create writer: %v", err)
os.Exit(util.ExitFailure)
}
defer out.Close()
info, err := dump.CollectionInfo(ctx)
if err != nil {
log.Logvf(log.Always, "failed to get collection info: %v", err)
os.Exit(util.ExitFailure)
}
progressManager := progress.NewBarWriter(log.Writer(0), progressBarWaitTime, progressBarLength, false)
progressManager.Start()
defer progressManager.Stop()
dumpProgressor := progress.NewCounter(int64(info.Documents))
dbNamespace := opts.DB + "." + opts.Collection
if progressManager != nil {
progressManager.Attach(dbNamespace, dumpProgressor)
defer progressManager.Detach(dbNamespace)
}
dump.CollectionInfo(ctx)
log.Logvf(log.Always, "exporting MongoDB collection %v.%v", opts.ToolOptions.DB, opts.ToolOptions.Collection)
if err = dump.Dump(ctx, out, dumpProgressor); err != nil {
log.Logvf(log.Always, "Failed: %v", err)
os.Exit(util.ExitFailure)
}
log.Logvf(log.Always, "export done")
}