-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomate-cli.go
70 lines (58 loc) · 1.46 KB
/
gomate-cli.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
package main
import (
"fmt"
"os"
"github.com/krasio/gomate"
"github.com/codegangsta/cli"
)
func main() {
app := cli.NewApp()
app.Name = "gomate"
app.Usage = "autocompelate like a boss"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "redis, r",
Value: "redis://localhost:6379/0",
Usage: "Redis connection string",
EnvVar: "GOMATE_REDIS_URL",
},
}
app.Commands = []cli.Command{
{
Name: "load",
ShortName: "l",
Usage: "Replaces collection specified by TYPE with items read from stdin in the JSON lines format.",
Description: "load [TYPE] < path/to/data.json",
Action: func(c *cli.Context) {
kind := c.Args()[0]
conn, _ := gomate.Connect(c.GlobalString("redis"))
defer conn.Close()
gomate.BulkLoad(kind, os.Stdin, conn)
},
},
{
Name: "query",
ShortName: "q",
Usage: "Queries for items from collection specified by TYPE.",
Description: "query [TYPE] [TERM]",
Action: func(c *cli.Context) {
kind := c.Args()[0]
query := c.Args()[1]
conn, _ := gomate.Connect(c.GlobalString("redis"))
defer conn.Close()
fmt.Printf("Query %s for \"%s\":\n", kind, query)
matches := gomate.Query(kind, query, conn)
fmt.Println()
if len(matches) > 0 {
for _, match := range matches {
fmt.Printf(" %s\n", match.Term)
}
} else {
fmt.Println(" Sorry, nothing found. ¯\\_(ツ)_/¯")
}
fmt.Println()
},
},
}
app.Run(os.Args)
}