-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
213 lines (183 loc) · 4.75 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
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
package main
import (
"bufio"
"fmt"
"os"
"time"
"github.com/go-git/go-git/v5"
"github.com/ggql/ggql/ast"
"github.com/ggql/ggql/cli"
"github.com/ggql/ggql/engine"
"github.com/ggql/ggql/parser"
)
func main() {
args := os.Args
command := cli.ParseArguments(args)
if len(command.ReplMode.Repos) != 0 {
launchGitqlRepl(command.ReplMode)
}
if command.QueryMode.Query != "" {
reporter := cli.DiagnosticReporter{}
gitReposResult := validateGitRepositories(command.QueryMode.Arguments.Repos)
if gitReposResult.err != nil {
reporter.ReportDiagnostic("", &parser.Diagnostic{Message: gitReposResult.err.Error()})
return
}
repos := gitReposResult.ok
env := ast.Environment{}
executeGitqlQuery(command.QueryMode.Query, command.QueryMode.Arguments, repos, &env, &reporter)
}
if command.Help {
cli.PrintHelpList()
}
if command.Version != "" {
fmt.Printf("GitQL version %s\n", command.Version)
}
if command.Error != "" {
fmt.Println(command.Error)
}
}
func launchGitqlRepl(args cli.Arguments) {
reporter := cli.DiagnosticReporter{}
gitReposResult := validateGitRepositories(args.Repos)
if gitReposResult.err != nil {
reporter.ReportDiagnostic("", &parser.Diagnostic{Message: gitReposResult.err.Error()})
return
}
globalEnv := ast.Environment{}
gitRepositories := gitReposResult.ok
scanner := bufio.NewScanner(os.Stdin)
for {
if isTerminal(os.Stdin) {
fmt.Print("gql > ")
}
if !scanner.Scan() {
break
}
input := scanner.Text()
if input == "exit" {
fmt.Println("Goodbye!")
break
}
executeGitqlQuery(input, args, gitRepositories, &globalEnv, &reporter)
globalEnv.ClearSession()
}
}
// nolint:funlen,gocyclo
func executeGitqlQuery(
query string,
args cli.Arguments,
repos []*git.Repository,
env *ast.Environment,
reporter *cli.DiagnosticReporter,
) {
frontStart := time.Now()
tokens, err := parser.Tokenize(query)
if err.Message != "" {
reporter.ReportDiagnostic(query, err)
return
}
if len(tokens) == 0 {
return
}
queryNode, err1 := parser.ParserGql(tokens, env)
if err1.Message != "" {
reporter.ReportDiagnostic(query, &err1)
return
}
frontDuration := time.Since(frontStart)
engineStart := time.Now()
evaluationResult, err2 := engine.Evaluate(env, repos, queryNode)
if err2 != nil {
reporter.ReportDiagnostic(query, &parser.Diagnostic{Message: err2.Error()})
return
}
if evaluationResult.SelectedGroups.Obj.Len() != 0 {
groups := evaluationResult.SelectedGroups.Obj
hiddenselection := evaluationResult.SelectedGroups.Str
switch args.OutputFormat {
case cli.Render:
err := cli.RenderObjects(&groups, hiddenselection, args.Pagination, args.PageSize)
if err != nil {
fmt.Println(err)
}
case cli.JSON:
var indexes []int
for index, title := range groups.Titles {
if contains(hiddenselection, title) {
indexes = append([]int{index}, indexes...)
}
}
if groups.Len() > 1 {
groups.Flat()
}
for _, index := range indexes {
groups.Titles = append(groups.Titles[:index], groups.Titles[index+1:]...)
for _, row := range groups.Groups[0].Rows {
row.Values = append(row.Values[:index], row.Values[index+1:]...)
}
}
if json, err := groups.AsJson(); err == nil {
fmt.Println(json)
}
case cli.CSV:
indexes := []int{}
for index, title := range groups.Titles {
if contains(hiddenselection, title) {
indexes = append([]int{index}, indexes...)
}
}
if groups.Len() > 1 {
groups.Flat()
}
for _, index := range indexes {
groups.Titles = append(groups.Titles[:index], groups.Titles[index+1:]...)
for _, row := range groups.Groups[0].Rows {
row.Values = append(row.Values[:index], row.Values[index+1:]...)
}
}
if csv, err := groups.AsCsv(); err == nil {
fmt.Println(csv)
}
}
}
engineDuration := time.Since(engineStart)
if args.Analysis {
fmt.Println("Analysis:")
fmt.Println("Frontend : ", frontDuration)
fmt.Println("Engine : ", engineDuration)
fmt.Println("Total : ", frontDuration+engineDuration)
}
}
func validateGitRepositories(repositories []string) result {
var gitRepositories []*git.Repository
for _, repository := range repositories {
gitRepository, err := git.PlainOpen(repository)
if err != nil {
return result{err: err}
}
gitRepositories = append(gitRepositories, gitRepository)
}
return result{ok: gitRepositories}
}
type result struct {
ok []*git.Repository
err error
}
func isTerminal(f *os.File) bool {
// Check file descriptor if used as terminal or not with os.Isatty()
if stat, err := f.Stat(); err == nil {
return (stat.Mode() & os.ModeCharDevice) != 0
}
return false
}
func contains(arr []string, items ...string) bool {
for _, item := range items {
for _, a := range arr {
if item == a {
return true
}
}
}
return false
}