-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbounce2db.go
56 lines (47 loc) · 1010 Bytes
/
bounce2db.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
package main
import "log"
import "os"
import "flag"
import "./postfixutil"
import "encoding/json"
import "gopkg.in/mgo.v2"
const (
BounceCollection string = "bounces"
)
type Config struct {
DBHost string
DBName string
}
func ReadConfig(configFilePath string) Config {
file, _ := os.Open(configFilePath)
decoder := json.NewDecoder(file)
config := Config{}
err := decoder.Decode(&config)
if err != nil {
log.Fatal(err)
}
return config
}
func main() {
var configPath string
flag.StringVar(&configPath, "conf", "conf.json", "Configuration file")
flag.Parse()
filePaths := flag.Args()
config := ReadConfig(configPath)
session, err := mgo.Dial(config.DBHost)
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB(config.DBName).C(BounceCollection)
bounces := postfixutil.FindBounces(&filePaths)
for _, bounce := range bounces {
if bounce.IsHard() {
err = c.Insert(&bounce)
if err != nil {
log.Fatal(err)
}
}
}
}