-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (112 loc) · 2.42 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
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
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/codegangsta/cli"
)
func main() {
app := cli.NewApp()
app.Name = "SqISH"
app.Usage = "Sql Interactive Shell History"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "database, d",
Value: os.ExpandEnv("${HOME}/.sqish_db"),
Usage: "Path to database",
},
cli.StringFlag{
Name: "shell_session_id",
Value: os.ExpandEnv("$SQISH_SESSION_ID"),
Usage: "Shell session ID. This is used to uniquely identify a shell session.",
},
}
app.Commands = []cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add a command to the history.",
Action: func(ctx *cli.Context) {
runWithErr(func() error {
db, err := newDatabase(ctx.GlobalString("database"))
if err != nil {
return err
}
defer db.Close()
// Fill a record.
wd, err := os.Getwd()
if err != nil {
return err
}
h, err := os.Hostname()
if err != nil {
return err
}
r := record{
Cmd: strings.TrimSpace(strings.Join(ctx.Args(), " ")),
Dir: wd,
Hostname: h,
ShellSessionID: ctx.GlobalString("shell_session_id"),
Time: time.Now(),
}
if len(r.Cmd) == 0 {
return nil
}
return db.Add(&r)
})
},
},
{
Name: "search",
Aliases: []string{"s"},
Usage: "Search (full screen)",
Flags: []cli.Flag{
cli.StringFlag{
Name: "query",
Usage: "Query to pre-fill search bar with.",
},
},
Action: func(ctx *cli.Context) {
runWithErr(
func() error {
db, err := newDatabase(ctx.GlobalString("database"))
if err != nil {
return err
}
defer db.Close()
return runGui(db, ctx.GlobalString("shell_session_id"), ctx.String("query"))
})
},
},
{
Name: "inline",
Aliases: []string{"i"},
Usage: "Inline search",
Flags: []cli.Flag{
cli.StringFlag{
Name: "query",
Usage: "Query to pre-fill search bar with.",
},
},
Action: func(ctx *cli.Context) {
runWithErr(
func() error {
db, err := newDatabase(ctx.GlobalString("database"))
if err != nil {
return err
}
defer db.Close()
// Find-as-you-type on input.
return cliFindAsYouType(db)
})
},
},
}
app.Run(os.Args)
}
func runWithErr(fn func() error) {
if err := fn(); err != nil {
fmt.Println("Error: ", err)
}
}