-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.go
48 lines (43 loc) · 1.02 KB
/
common.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
package main
import (
"fmt"
"github.com/duego/mongotool/storage"
"labix.org/v2/mgo"
"net/url"
"os"
"strings"
)
// mongoSession gives a session or dies trying.
func mongoSession(addr string) *mgo.Session {
fmt.Fprintln(os.Stderr, "Connecting to", addr)
s, err := mgo.Dial(addr + "?connect=direct")
if err != nil {
errorf("Error connecting to %s: %v", addr, err)
exit()
}
return s
}
// selectStorage will figure out what kind of storage we're looking for in specified target.
func selectStorage(target string, compression bool) (root string, store storage.SaveFetcher) {
if target == "-" {
errorf("%s", "TODO: Set stdin storage here")
exit()
}
if strings.HasPrefix(target, "http") {
if u, err := url.Parse(target); err != nil {
errorf("%v", err)
exit()
} else {
store = storage.NewS3(fmt.Sprintf("%s://%s", u.Scheme, u.Host))
root = u.Path
}
} else {
store = storage.Filesystem{target}
root = ""
}
// Apply compression
if compression {
store = storage.NewGzipSaveFetcher(store)
}
return
}