-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
console.go
178 lines (156 loc) · 3.29 KB
/
console.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
package main
import (
"bytes"
"fmt"
"go/format"
"os"
"sort"
"strings"
"text/template"
"github.com/go-yaml/yaml"
)
type Console struct {
builtinInput string
predefinedInput string
output string
}
func newConsole() *Console {
return &Console{
builtinInput: "./builtin.yml",
predefinedInput: "./predefined.yml",
output: "../console/suggestions.go",
}
}
type Suggestion struct {
Name string
Description string
Scope []string
}
// filterByScope filters suggestions by provided scope
func filterByScope(scope string, s []*Suggestion) []*Suggestion {
var filtered []*Suggestion
for i := range s {
var isEnable bool
for j := range s[i].Scope {
if !strings.EqualFold(s[i].Scope[j], scope) {
continue
}
isEnable = true
break
}
if isEnable {
filtered = append(filtered, s[i])
}
}
return filtered
}
func (c *Console) generateSuggestions() error {
var suggestions []*Suggestion
if v, err := c.getVariableSuggestions(); err != nil {
return err
} else {
suggestions = append(suggestions, v...)
}
if v, err := c.getFunctionSuggestions(); err != nil {
return err
} else {
suggestions = append(suggestions, v...)
}
sort.Slice(suggestions, func(i, j int) bool {
return suggestions[i].Name < suggestions[j].Name
})
out := new(bytes.Buffer)
tpl := template.Must(
template.New("console.suggestions").
Funcs(template.FuncMap{
"filterByScope": filterByScope,
}).
Parse(consoleSuggestions),
)
if err := tpl.Execute(out, map[string]interface{}{
"Suggestions": suggestions,
"Scopes": []string{
"RECV",
"HASH",
"HIT",
"MISS",
"PASS",
"FETCH",
"ERROR",
"DELIVER",
"LOG",
},
// Statement suggestions only provide calculative statements
"Statements": []string{
"set",
"declare",
"if",
"add",
"unset",
"remove",
},
}); err != nil {
return err
}
ret, err := format.Source(out.Bytes())
if err != nil {
fmt.Println(out.String())
return err
}
f, err := os.OpenFile(c.output, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
if _, err := f.Write(ret); err != nil {
return err
}
return nil
}
func (c *Console) getVariableSuggestions() ([]*Suggestion, error) {
fp, err := os.Open(c.predefinedInput)
if err != nil {
return nil, err
}
defer fp.Close()
defs := map[string]*Definition{}
if err := yaml.NewDecoder(fp).Decode(&defs); err != nil {
return nil, err
}
var suggestions []*Suggestion
for key := range defs {
name := key
if idx := strings.Index(key, "%"); idx >= 0 {
name = key[:idx-1]
}
suggestions = append(suggestions, &Suggestion{
Name: name,
Description: "Predefined Variable",
Scope: defs[key].On,
})
}
return suggestions, nil
}
func (c *Console) getFunctionSuggestions() ([]*Suggestion, error) {
fp, err := os.Open(c.builtinInput)
if err != nil {
return nil, err
}
defer fp.Close()
defs := map[string]*FunctionSpec{}
if err := yaml.NewDecoder(fp).Decode(&defs); err != nil {
return nil, err
}
var suggestions []*Suggestion
for key := range defs {
if strings.Contains(key, "%") {
continue
}
suggestions = append(suggestions, &Suggestion{
Name: key,
Description: "Built-in Function",
Scope: defs[key].On,
})
}
return suggestions, nil
}