forked from influxdata/flux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepl.go
344 lines (300 loc) · 7.48 KB
/
repl.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Package repl implements the read-eval-print-loop for the command line flux query console.
package repl
import (
"context"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"sort"
"strings"
"sync"
"syscall"
"github.com/InfluxCommunity/flux"
"github.com/InfluxCommunity/flux/dependency"
"github.com/InfluxCommunity/flux/execute"
"github.com/InfluxCommunity/flux/internal/operation"
"github.com/InfluxCommunity/flux/internal/spec"
"github.com/InfluxCommunity/flux/interpreter"
"github.com/InfluxCommunity/flux/lang"
"github.com/InfluxCommunity/flux/libflux/go/libflux"
"github.com/InfluxCommunity/flux/memory"
"github.com/InfluxCommunity/flux/plan"
"github.com/InfluxCommunity/flux/runtime"
"github.com/InfluxCommunity/flux/semantic"
"github.com/InfluxCommunity/flux/values"
"github.com/c-bata/go-prompt"
"github.com/opentracing/opentracing-go"
)
type REPL struct {
ctx context.Context
scope values.Scope
itrp *interpreter.Interpreter
analyzer *libflux.Analyzer
importer interpreter.Importer
cancelMu sync.Mutex
cancelFunc context.CancelFunc
enableSuggestions bool
}
type Option interface {
applyOption(r *REPL)
}
func New(ctx context.Context, opts ...Option) *REPL {
scope := values.NewScope()
importer := runtime.StdLib()
for _, p := range runtime.PreludeList {
pkg, err := importer.ImportPackageObject(p)
if err != nil {
panic(err)
}
pkg.Range(scope.Set)
}
analyzer, err := libflux.NewAnalyzerWithOptions(libflux.NewOptions(ctx))
if err != nil {
panic(err)
}
repl := &REPL{
ctx: ctx,
scope: scope,
itrp: interpreter.NewInterpreter(nil, &lang.ExecOptsConfig{}),
analyzer: analyzer,
importer: importer,
}
for _, opt := range opts {
opt.applyOption(repl)
}
return repl
}
func (r *REPL) Run() {
p := prompt.New(
r.input,
r.completer,
prompt.OptionPrefix("> "),
prompt.OptionTitle("flux"),
)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT)
go func() {
for range sigs {
r.cancel()
}
}()
p.Run()
}
func (r *REPL) cancel() {
r.cancelMu.Lock()
defer r.cancelMu.Unlock()
if r.cancelFunc != nil {
r.cancelFunc()
r.cancelFunc = nil
}
}
func (r *REPL) setCancel(cf context.CancelFunc) {
r.cancelMu.Lock()
defer r.cancelMu.Unlock()
r.cancelFunc = cf
}
func (r *REPL) clearCancel() {
r.setCancel(nil)
}
func (r *REPL) completer(d prompt.Document) []prompt.Suggest {
if r.enableSuggestions {
names := make([]string, 0, r.scope.Size())
r.scope.Range(func(k string, v values.Value) {
names = append(names, k)
})
sort.Strings(names)
s := make([]prompt.Suggest, 0, len(names))
for _, n := range names {
if n == "_" || !strings.HasPrefix(n, "_") {
s = append(s, prompt.Suggest{Text: n})
}
}
if d.Text == "" || strings.HasPrefix(d.Text, "@") {
root := "./" + strings.TrimPrefix(d.Text, "@")
fluxFiles, err := getFluxFiles(root)
if err == nil {
for _, fName := range fluxFiles {
s = append(s, prompt.Suggest{Text: "@" + fName})
}
}
dirs, err := getDirs(root)
if err == nil {
for _, fName := range dirs {
s = append(s, prompt.Suggest{Text: "@" + fName + string(os.PathSeparator)})
}
}
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
return nil
}
func (r *REPL) Input(t string) (*libflux.FluxError, error) {
return r.executeLine(t)
}
// input processes a line of input and prints the result.
func (r *REPL) input(t string) {
// Create a root span
span := opentracing.StartSpan("REPL.input")
r.ctx = opentracing.ContextWithSpan(r.ctx, span)
defer span.Finish()
if fluxError, err := r.executeLine(t); err != nil {
if fluxError != nil {
fluxError.Print()
} else {
fmt.Println("Error:", err)
}
}
}
func (r *REPL) Eval(t string) ([]interpreter.SideEffect, error) {
s, _, err := r.evalWithFluxError(t)
return s, err
}
func (r *REPL) evalWithFluxError(t string) ([]interpreter.SideEffect, *libflux.FluxError, error) {
if t == "" {
return nil, nil, nil
}
if t[0] == '@' {
q, err := LoadQuery(t)
if err != nil {
return nil, nil, err
}
t = q
}
pkg, fluxError, err := r.analyzeLine(t)
if err != nil {
return nil, fluxError, err
}
ctx, span := dependency.Inject(r.ctx, execute.DefaultExecutionDependencies())
defer span.Finish()
x, err := r.itrp.Eval(ctx, pkg, r.scope, r.importer)
return x, nil, err
}
// executeLine processes a line of input.
// If the input evaluates to a valid value, that value is returned.
func (r *REPL) executeLine(t string) (*libflux.FluxError, error) {
ses, fluxError, err := r.evalWithFluxError(t)
if err != nil {
return fluxError, err
}
for _, se := range ses {
if _, ok := se.Node.(*semantic.ExpressionStatement); ok {
if t, ok := se.Value.(*flux.TableObject); ok {
now, ok := r.scope.Lookup("now")
if !ok {
return nil, fmt.Errorf("now option not set")
}
nowTime, err := now.Function().Call(r.ctx, nil)
if err != nil {
return nil, err
}
s, err := spec.FromTableObject(r.ctx, t, nowTime.Time().Time())
if err != nil {
return nil, err
}
if err := r.doQuery(r.ctx, s); err != nil {
return nil, err
}
} else {
values.Display(os.Stdout, se.Value)
fmt.Println()
}
}
}
return nil, nil
}
func (r *REPL) analyzeLine(t string) (*semantic.Package, *libflux.FluxError, error) {
pkg, fluxError := r.analyzer.AnalyzeString(t)
if fluxError != nil {
return nil, fluxError, fluxError.GoError()
}
bs, err := pkg.MarshalFB()
if err != nil {
return nil, nil, err
}
x, err := semantic.DeserializeFromFlatBuffer(bs)
return x, nil, err
}
func (r *REPL) doQuery(ctx context.Context, spec *operation.Spec) error {
// Setup cancel context
nextPlanNodeID := new(int)
ctx, cancelFunc := context.WithCancel(context.WithValue(
ctx, plan.NextPlanNodeIDKey, nextPlanNodeID,
))
r.setCancel(cancelFunc)
defer cancelFunc()
defer r.clearCancel()
c := Compiler{
Spec: spec,
}
program, err := c.Compile(ctx, runtime.Default)
if err != nil {
return err
}
alloc := &memory.ResourceAllocator{}
qry, err := program.Start(ctx, alloc)
if err != nil {
return err
}
defer qry.Done()
for result := range qry.Results() {
tables := result.Tables()
fmt.Println("Result:", result.Name())
if err := tables.Do(func(tbl flux.Table) error {
_, err := execute.NewFormatter(tbl, nil).WriteTo(os.Stdout)
return err
}); err != nil {
return err
}
}
qry.Done()
return qry.Err()
}
func getFluxFiles(path string) ([]string, error) {
return filepath.Glob(path + "*.flux")
}
func getDirs(path string) ([]string, error) {
dir := filepath.Dir(path)
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
dirs := make([]string, 0, len(files))
for _, f := range files {
if f.IsDir() {
dirs = append(dirs, filepath.Join(dir, f.Name()))
}
}
return dirs, nil
}
// LoadQuery returns the Flux query q, except for two special cases:
// if q is exactly "-", the query will be read from stdin;
// and if the first character of q is "@",
// the @ prefix is removed and the contents of the file specified by the rest of q are returned.
func LoadQuery(q string) (string, error) {
if q == "-" {
data, err := io.ReadAll(os.Stdin)
if err != nil {
return "", err
}
return string(data), nil
}
if len(q) > 0 && q[0] == '@' {
data, err := os.ReadFile(q[1:])
if err != nil {
return "", err
}
return string(data), nil
}
return q, nil
}
type option func(r *REPL)
func (o option) applyOption(r *REPL) {
o(r)
}
func EnableSuggestions() Option {
return option(func(r *REPL) {
r.enableSuggestions = true
})
}