forked from bbengfort/crate
/
viewkey.go
63 lines (50 loc) · 1.27 KB
/
viewkey.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
package main
import (
"os"
"github.com/bbengfort/crate/crate"
"github.com/bbengfort/crate/crate/version"
"github.com/codegangsta/cli"
)
var console *crate.Console
func main() {
app := cli.NewApp()
app.Name = "viewkey"
app.Usage = "view a key in the database"
app.Version = version.Version()
app.Author = "Benjamin Bengfort"
app.Email = "benjamin@bengfort.com"
app.Flags = []cli.Flag{
cli.BoolFlag{"debug", "set debug mode for vebose logging", ""},
cli.BoolFlag{"all", "show all the database keys", ""},
cli.IntFlag{"limit", 100, "limit the keys returned in all", ""},
}
app.Action = func(c *cli.Context) {
debug := c.Bool("debug")
all := c.Bool("all")
limit := c.Int("limit")
// Create debug console for now
console = new(crate.Console)
console.Init(debug)
crate.InitMagic()
defer crate.Magic.Close()
// Connect to the database
crate.InitializeDatabase()
defer crate.CloseDatabase()
if all {
// TODO show all the keys in the database
for _, key := range crate.FetchKeys(limit) {
console.Log(key)
}
} else {
for _, key := range c.Args() {
path, err := crate.Fetch(key)
if err != nil {
console.Err("database lookup err", err)
} else {
console.Log(path.Info() + "\n")
}
}
}
}
app.Run(os.Args)
}