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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
package main
import (
"bytes"
"fmt"
"log"
"os"
"github.com/codegangsta/cli"
)
var branch, commit string
func main() {
log.SetFlags(0)
NewApp().Run(os.Args)
}
// NewApp creates an Application instance.
func NewApp() *cli.App {
app := cli.NewApp()
app.Name = "bolt"
app.Usage = "BoltDB toolkit"
app.Version = fmt.Sprintf("0.1.0 (%s %s)", branch, commit)
app.Commands = []cli.Command{
{
Name: "get",
Usage: "Retrieve a value for given key in a bucket",
Action: func(c *cli.Context) {
path, name, key := c.Args().Get(0), c.Args().Get(1), c.Args().Get(2)
Get(path, name, key)
},
},
{
Name: "set",
Usage: "Sets a value for given key in a bucket",
Action: func(c *cli.Context) {
path, name, key, value := c.Args().Get(0), c.Args().Get(1), c.Args().Get(2), c.Args().Get(3)
Set(path, name, key, value)
},
},
{
Name: "keys",
Usage: "Retrieve a list of all keys in a bucket",
Action: func(c *cli.Context) {
path, name := c.Args().Get(0), c.Args().Get(1)
Keys(path, name)
},
},
{
Name: "buckets",
Usage: "Retrieves a list of all buckets",
Action: func(c *cli.Context) {
path := c.Args().Get(0)
Buckets(path)
},
},
{
Name: "pages",
Usage: "Dumps page information for a database",
Action: func(c *cli.Context) {
path := c.Args().Get(0)
Pages(path)
},
},
{
Name: "check",
Usage: "Performs a consistency check on the database",
Action: func(c *cli.Context) {
path := c.Args().Get(0)
Check(path)
},
},
}
return app
}
var logger = log.New(os.Stderr, "", 0)
var logBuffer *bytes.Buffer
func print(v ...interface{}) {
if testMode {
logger.Print(v...)
} else {
fmt.Print(v...)
}
}
func printf(format string, v ...interface{}) {
if testMode {
logger.Printf(format, v...)
} else {
fmt.Printf(format, v...)
}
}
func println(v ...interface{}) {
if testMode {
logger.Println(v...)
} else {
fmt.Println(v...)
}
}
func fatal(v ...interface{}) {
logger.Print(v...)
if !testMode {
os.Exit(1)
}
}
func fatalf(format string, v ...interface{}) {
logger.Printf(format, v...)
if !testMode {
os.Exit(1)
}
}
func fatalln(v ...interface{}) {
logger.Println(v...)
if !testMode {
os.Exit(1)
}
}
// LogBuffer returns the contents of the log.
// This only works while the CLI is in test mode.
func LogBuffer() string {
if logBuffer != nil {
return logBuffer.String()
}
return ""
}
var testMode bool
// SetTestMode sets whether the CLI is running in test mode and resets the logger.
func SetTestMode(value bool) {
testMode = value
if testMode {
logBuffer = bytes.NewBuffer(nil)
logger = log.New(logBuffer, "", 0)
} else {
logger = log.New(os.Stderr, "", 0)
}
}
|