/
cmd.go
218 lines (177 loc) Β· 3.8 KB
/
cmd.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/chzyer/readline"
"github.com/cloudson/gitql/parser"
"github.com/cloudson/gitql/runtime"
"github.com/cloudson/gitql/semantical"
"github.com/jessevdk/go-flags"
"github.com/pkg/errors"
)
const Version = "Gitql 1.6.0"
type Gitql struct {
Path string `short:"p" default:"."`
Version bool `short:"v"`
Isinteractive bool `short:"i"`
ShowTables bool `long:"show-tables"`
TypeFormat string `long:"type" default:"table"`
Query string
}
func (cmd *Gitql) Run() int {
if err := unwrap(cmd.execute()); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return 1
}
return 0
}
func (cmd Gitql) execute() error {
if err := cmd.parseCommandLine(); err != nil {
return err
}
folder, err := filepath.Abs(cmd.Path)
if err != nil {
return err
}
if cmd.Isinteractive {
return runPrompt(folder, cmd.TypeFormat)
}
return runQuery(cmd.Query, folder, cmd.TypeFormat)
}
func runPrompt(folder, typeFormat string) error {
term, err := readline.NewEx(&readline.Config{
Prompt: "gitql> ",
AutoComplete: readline.SegmentFunc(suggestQuery),
})
if err != nil {
return err
}
defer term.Close()
for {
query, err := term.Readline()
if err != nil {
if err == io.EOF {
break // Ctrl^D
}
return err
}
if query == "" {
continue
}
if query == "exit" || query == "quit" {
break
}
if err := runQuery(query, folder, typeFormat); err != nil {
fmt.Println("Error: " + err.Error())
continue
}
}
return nil
}
func runQuery(query, folder, typeFormat string) error {
parser.New(query)
ast, err := parser.AST()
if err != nil {
return err
}
ast.Path = &folder
if err := semantical.Analysis(ast); err != nil {
return err
}
return runtime.Run(ast, &typeFormat)
}
func (cmd *Gitql) parseCommandLine() error {
if err := cmd.parse(os.Args[1:]); err != nil {
return err
}
if cmd.Version {
return makeIgnoreErr(Version)
}
if cmd.ShowTables {
return makeIgnoreErr(printTables())
}
return nil
}
func (cmd *Gitql) parse(argv []string) error {
p := flags.NewParser(cmd, flags.PrintErrors)
args, err := p.ParseArgs(argv)
if err != nil {
return err
}
if !cmd.Isinteractive && !cmd.Version && !cmd.ShowTables && len(args) == 0 {
os.Stderr.Write(cmd.usage())
return errors.New("invalid command line options")
}
cmd.Query = strings.Join(args, " ")
return nil
}
func (cmd Gitql) usage() []byte {
var buf bytes.Buffer
fmt.Fprintf(&buf, `Gitql - Git query language
Usage: gitql [flags] [args]
Flags:
-i Enter to interactive mode
-p string
The (optional) path to run gitql (default ".")
--show-tables
Show all tables
--type string
The output type format {table|json} (default "table")
-v The version of gitql
Arguments:
sql: A query to run
`)
return buf.Bytes()
}
func printTables() string {
var buf bytes.Buffer
buf.WriteString("Tables: \n\n")
tables := runtime.PossibleTables()
for tableName, fields := range tables {
buf.WriteString(fmt.Sprintf("%s\n\t", tableName))
for i, field := range fields {
comma := "."
if i+1 < len(fields) {
comma = ", "
}
buf.WriteString(fmt.Sprintf("%s%s", field, comma))
}
buf.WriteString("\n")
}
return buf.String()
}
func makeIgnoreErr(str string) error {
return ignore{err: errors.New(str)}
}
// Ignore error
type ignore struct {
err error
}
type cause interface {
Cause() error
}
func (i ignore) Error() string {
return i.err.Error()
}
func (i ignore) Cause() error {
return i.err
}
// get important message from wrapped error message
func unwrap(err error) error {
for e := err; e != nil; {
switch e.(type) {
case ignore:
fmt.Println(e.Error())
return nil
case cause:
e = e.(cause).Cause()
default:
return e
}
}
return nil
}