forked from mongodb/mongo-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoimport.go
69 lines (59 loc) · 1.72 KB
/
mongoimport.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 (C) MongoDB, Inc. 2014-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Main package for the mongoimport tool.
package main
import (
"os"
"github.com/huimingz/mongo-tools/common/log"
"github.com/huimingz/mongo-tools/common/signals"
"github.com/huimingz/mongo-tools/common/util"
"github.com/huimingz/mongo-tools/mongoimport"
)
var (
VersionStr = "built-without-version-string"
GitCommit = "build-without-git-commit"
)
func main() {
opts, err := mongoimport.ParseOptions(os.Args[1:], VersionStr, GitCommit)
if err != nil {
log.Logvf(log.Always, "error parsing command line options: %v", err)
log.Logvf(log.Always, util.ShortUsage("mongoimport"))
os.Exit(util.ExitFailure)
}
signals.Handle()
// print help, if specified
if opts.PrintHelp(false) {
return
}
// print version, if specified
if opts.PrintVersion() {
return
}
m, err := mongoimport.New(opts)
if err != nil {
log.Logvf(log.Always, err.Error())
os.Exit(util.ExitFailure)
}
defer m.Close()
numDocs, numFailure, err := m.ImportDocuments()
if !opts.Quiet {
if err != nil {
log.Logvf(log.Always, "Failed: %v", err)
}
if m.ToolOptions.WriteConcern.Acknowledged() {
if opts.Mode == "delete" {
log.Logvf(log.Always, "%v document(s) deleted successfully. %v document(s) failed to delete.", numDocs, numFailure)
} else {
log.Logvf(log.Always, "%v document(s) imported successfully. %v document(s) failed to import.", numDocs, numFailure)
}
} else {
log.Logvf(log.Always, "done")
}
}
if err != nil {
os.Exit(util.ExitFailure)
}
}