forked from filhodanuvem/gitql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (159 loc) · 3.33 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
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
package main
import (
"fmt"
"io"
"os"
"github.com/chzyer/readline"
"github.com/cloudson/gitql/lexical"
"github.com/cloudson/gitql/parser"
"github.com/cloudson/gitql/runtime"
"github.com/cloudson/gitql/semantical"
"github.com/urfave/cli/v2"
)
const Version = "Gitql 2.1.0"
func main() {
app := &cli.App{
Name: "gitql",
Usage: "A git query language",
Version: Version,
HideVersion: true,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "interactive",
Aliases: []string{"i"},
Usage: "Enter to interactive mode",
},
&cli.StringFlag{
Name: "path",
Aliases: []string{"p"},
Value: ".",
Usage: `The (optional) path to run gitql`,
},
&cli.StringFlag{
Name: "format",
Aliases: []string{"f"},
Value: "table",
Usage: "The output type format {table|json}",
},
// for backward compatibility
&cli.BoolFlag{
Name: "version",
Aliases: []string{"v"},
Hidden: true,
},
&cli.StringFlag{
Name: "type",
Hidden: true,
},
&cli.BoolFlag{
Name: "show-tables",
Aliases: []string{"s"},
Hidden: true,
},
},
Commands: []*cli.Command{
{
Name: "show-tables",
Aliases: []string{"s"},
Usage: "Show all tables",
Action: showTablesCmd,
},
{
Name: "version",
Aliases: []string{"v"},
Usage: "The version of gitql",
Action: func(c *cli.Context) error {
fmt.Println(Version)
return nil
},
},
},
Action: func(c *cli.Context) error {
path, format, interactive := c.String("path"), c.String("format"), c.Bool("interactive")
// for backward compatibility
if c.Bool("version") {
fmt.Println(Version)
return nil
}
if c.Bool("show-tables") {
return showTablesCmd(c)
}
if typ := c.String("type"); typ != "" {
format = typ
}
// ============================
if c.NArg() == 0 && !interactive {
return cli.ShowAppHelp(c)
}
if interactive {
return runPrompt(path, format)
}
return runQuery(c.Args().First(), path, format)
},
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}
func showTablesCmd(c *cli.Context) error {
prog := &parser.NodeProgram{
Child: &parser.NodeShow{
Tables: true,
},
}
return runtime.RunShow(prog)
}
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
switch lexical.Command {
case lexical.T_SELECT:
if err := semantical.Analysis(ast); err != nil {
return err
}
err = runtime.RunSelect(ast, &typeFormat)
break
case lexical.T_SHOW:
err = runtime.RunShow(ast)
break
case lexical.T_USE:
err = runtime.RunUse(ast)
break
}
return err
}