/
export.go
67 lines (56 loc) · 1.34 KB
/
export.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
package mongo
import (
"fmt"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type MongoExportCommand struct {
Conn Connection
DB Database
DataDir string
Log *logrus.Entry `json:"-"`
}
func (e MongoExportCommand) Execute() error {
if e.Log == nil {
e.Log = logrus.WithField("cmd", "MongoExportCommand")
}
if e.DB.Name == "" {
e.DB.Name = e.Conn.DBName
}
if e.Conn.DBName == "" {
e.Conn.DBName = e.DB.Name
}
pc, err := e.Conn.Prepare(e.Log)
if err != nil {
return errors.Wrap(err, "prepare connection")
}
wrapper, err := e.getMongoWrapper(e.DataDir, pc)
if err != nil {
return errors.Wrap(err, "connect to mongo")
}
if pc.CleanUp != nil {
defer pc.CleanUp()
}
for name, info := range e.DB.Collections {
err = wrapper.exportData(name, info.DataFile)
if err != nil {
logrus.Warnf("could not export collection %q from %q to %q: %v", name, e.DB.Name, info.DataFile, err)
}
}
return nil
}
func (e MongoExportCommand) getMongoWrapper(dataDir string, c PreparedConnection) (*mongoWrapper, error) {
wrapper, err := newMongoWrapper(
c.Host,
c.Port,
c.DBName,
c.Credentials.Username,
c.Credentials.Password,
c.Credentials.AuthSource,
dataDir,
e.Log.WithField("typ", "mongoWrapper"))
if err != nil {
return nil, fmt.Errorf("could not get mongo wrapper: %v", err)
}
return wrapper, nil
}