-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
88 lines (80 loc) · 1.56 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
// Command bolt-diff compares two bolt databases and reports if they are equal or not.
// A diff can be optionnaly printed. Keys are printed using fmt's %+q and values are sha1-ed
// for clarity.
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"github.com/boltdb/bolt"
)
var showDiff bool
func init() {
flag.BoolVar(&showDiff, "d", false, "show diff")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [OPTIONS] db1 db2\n\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
flag.Parse()
}
func main() {
log.SetFlags(0)
var exitCode int
defer func() {
os.Exit(exitCode)
}()
if flag.NArg() != 2 {
flag.Usage()
log.Printf("expected 2 args, got %d", flag.NArg())
exitCode = 1
return
}
var dbs [2]*bolt.DB
for i, dbpath := range []string{flag.Arg(0), flag.Arg(1)} {
if fi, err := os.Stat(dbpath); err != nil {
log.Println(err)
exitCode = 1
return
} else if fi.IsDir() {
log.Printf("%s: not a regular file", dbpath)
exitCode = 1
return
}
db, err := bolt.Open(dbpath, 0444, &bolt.Options{ReadOnly: true})
if err != nil {
log.Println(err)
exitCode = 1
return
}
defer func() {
_ = db.Close()
}()
dbs[i] = db
}
if showDiff {
diffs, err := dbDiff(dbs[0], dbs[1])
if err != nil {
log.Println(err)
exitCode = 1
return
}
if len(diffs) > 0 {
exitCode = 2
for _, diff := range diffs {
fmt.Println(diff.String())
}
}
} else {
ok, err := dbEqual(dbs[0], dbs[1])
if err != nil {
log.Println(err)
exitCode = 1
return
}
if !ok {
exitCode = 2
}
}
}