-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
dbg.go
359 lines (338 loc) · 8.6 KB
/
dbg.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package runn
import (
"context"
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/elk-language/go-prompt"
pstrings "github.com/elk-language/go-prompt/strings"
"github.com/k0kubun/pp/v3"
"github.com/olekukonko/tablewriter"
"github.com/samber/lo"
)
const (
dbgCmdNext = "next"
dbgCmdNextShort = "n"
dbgCmdQuit = "quit"
dbgCmdQuitShort = "q"
dbgCmdPrint = "print"
dbgCmdPrintShort = "p"
dbgCmdBreak = "break"
dbgCmdBreakShort = "b"
dbgCmdContinue = "continue"
dbgCmdContinueShort = "c"
dbgCmdInfo = "info"
dbgCmdInfoShort = "i"
dbgCmdList = "list"
dbgCmdListShort = "l"
)
const bpSep = ":"
type breakpoint struct {
runbookID string
stepKey string
}
// dbg is runn debugger.
type dbg struct {
enable bool
showPrompt bool
quit bool
history []string
breakpoints []breakpoint
opn *operatorN
pp *pp.PrettyPrinter
}
func newDBG(enable bool) *dbg {
return &dbg{
enable: enable,
showPrompt: true,
pp: pp.New(),
}
}
type completer struct {
dbg *dbg
step *step
}
func newCompleter(dbg *dbg, step *step) *completer {
return &completer{
dbg: dbg,
step: step,
}
}
func (c *completer) do(d prompt.Document) ([]prompt.Suggest, pstrings.RuneNumber, pstrings.RuneNumber) {
endIndex := d.CurrentRuneIndex()
w := d.GetWordBeforeCursor()
startIndex := endIndex - pstrings.RuneCount([]byte(w))
cmd := d.Text
splitted := strings.Split(cmd, " ")
var s []prompt.Suggest
switch {
case !strings.Contains(cmd, " "):
s = []prompt.Suggest{
{Text: dbgCmdNext, Description: "(n) run current step and next"},
{Text: dbgCmdQuit, Description: "(q) quit debugger and skip all steps"},
{Text: dbgCmdContinue, Description: "(c) continue to run until next breakpoint"},
{Text: dbgCmdPrint, Description: "(p) print variable. ('print [variable]')"},
{Text: dbgCmdBreak, Description: "(b) set breakpoint. ('break [id]' 'break [id]:[step]' 'break :[step]')"},
{Text: dbgCmdInfo, Description: "(i) show information"},
{Text: dbgCmdList, Description: "(l) list codes of step. ('list' 'list [id]' 'list [id]:[step]' 'list :[step]')"},
}
case splitted[0] == dbgCmdPrint || splitted[0] == dbgCmdPrintShort:
// print
store := c.step.parent.store.toMap()
store[storeRootKeyIncluded] = c.step.parent.included
store[storeRootKeyPrevious] = c.step.parent.store.latest()
keys := storeKeys(store)
for _, k := range keys {
if strings.HasPrefix(k, w) {
s = append(s, prompt.Suggest{Text: k})
}
}
case splitted[0] == dbgCmdBreak || splitted[0] == dbgCmdBreakShort || splitted[0] == dbgCmdList || splitted[0] == dbgCmdListShort:
// break, list
for _, o := range c.dbg.opn.ops {
id := o.ID()[:7]
s = append(s, prompt.Suggest{Text: id})
}
if c.step.idx != len(c.step.parent.steps)-1 {
for i := c.step.idx + 1; i < len(c.step.parent.steps); i++ {
step := c.step.parent.steps[i]
s = append(s, prompt.Suggest{Text: fmt.Sprintf(":%s", step.key)})
}
}
for _, o := range c.dbg.opn.ops {
id := o.ID()[:7]
for _, step := range o.steps {
s = append(s, prompt.Suggest{Text: fmt.Sprintf("%s:%s", id, step.key)})
}
}
case splitted[0] == dbgCmdInfo || splitted[0] == dbgCmdInfoShort:
// info
s = append(s, prompt.Suggest{Text: "breakpoints", Description: "(b) show breakpoints"})
s = append(s, prompt.Suggest{Text: "variables", Description: "(v) show variables"})
}
return prompt.FilterHasPrefix(s, w, true), startIndex, endIndex
}
func (d *dbg) attach(ctx context.Context, s *step) error {
prpt := "> "
if d.quit {
s.parent.skipped = true
return errStepSkiped
}
if !d.enable {
return nil
}
if s != nil {
id := s.parent.ID()
stepKey := s.key
stepIdx := strconv.Itoa(s.idx)
// check breakpoints
for _, bp := range d.breakpoints {
if !strings.HasPrefix(id, bp.runbookID) {
continue
}
if bp.stepKey != stepKey && bp.stepKey != stepIdx {
continue
}
d.showPrompt = true
}
prpt = fmt.Sprintf("%s[%s]> ", id[:7], s.key)
}
if !d.showPrompt {
return nil
}
d.showPrompt = false
L:
for {
in := prompt.Input(
prompt.WithPrefix(prpt),
prompt.WithCompleter(newCompleter(d, s).do),
prompt.WithHistory(d.history),
)
d.history = append(d.history, in)
cmd := strings.SplitN(strings.TrimSpace(in), " ", 2)
prog := cmd[0]
switch prog {
case dbgCmdNext, dbgCmdNextShort:
// next
d.showPrompt = true
break L
case dbgCmdContinue, dbgCmdContinueShort:
// continue
break L
case dbgCmdQuit, dbgCmdQuitShort:
// quit
d.quit = true
s.parent.skipped = true
return errStepSkiped
case dbgCmdPrint, dbgCmdPrintShort:
// print
if len(cmd) != 2 {
_, _ = fmt.Fprintf(os.Stderr, "args required")
continue
}
store := s.parent.store.toMapForDbg()
store[storeRootKeyIncluded] = s.parent.included
store[storeRootKeyPrevious] = s.parent.store.latest()
e, err := Eval(cmd[1], store)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err.Error())
continue
}
d.pp.Println(e)
case dbgCmdBreak, dbgCmdBreakShort:
// break
if len(cmd) != 2 {
_, _ = fmt.Fprintf(os.Stderr, "args required")
continue
}
splitted := strings.Split(cmd[1], bpSep)
bp := breakpoint{}
if splitted[0] != "" {
bp.runbookID = splitted[0]
} else {
bp.runbookID = s.parent.ID()
}
if len(splitted) > 1 && splitted[1] != "" {
bp.stepKey = splitted[1]
} else {
bp.stepKey = "0"
}
d.breakpoints = append(d.breakpoints, bp)
case dbgCmdInfo, dbgCmdInfoShort:
// info
if len(cmd) != 2 {
_, _ = fmt.Fprintf(os.Stderr, "args required")
continue
}
switch cmd[1] {
case "breakpoints", "b":
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Num", "ID", "Step"})
table.SetAutoWrapText(false)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoFormatHeaders(false)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("-")
table.SetHeaderLine(false)
table.SetBorder(false)
for i, bp := range d.breakpoints {
table.Append([]string{strconv.Itoa(i + 1), bp.runbookID, bp.stepKey})
}
table.Render()
case "variables", "v":
store := s.parent.store.toMapForDbg()
store[storeRootKeyIncluded] = s.parent.included
store[storeRootKeyPrevious] = s.parent.store.latest()
keys := lo.Keys(store)
sort.Strings(keys)
for _, k := range keys {
fmt.Println(k)
}
default:
_, _ = fmt.Fprintf(os.Stderr, "unknown args %s\n", cmd[1])
continue
}
case dbgCmdList, dbgCmdListShort:
// list
var (
path string
idx int
)
if len(cmd) != 2 {
if s == nil {
_, _ = fmt.Fprintf(os.Stderr, "invalid args %s\n", cmd[1])
continue
}
path = s.parent.bookPath
idx = s.idx
} else {
splitted := strings.Split(cmd[1], bpSep)
var (
id string
o *operator
)
if splitted[0] != "" {
id = splitted[0]
} else {
id = s.parent.ID()
}
// search runbook
found := false
for _, op := range d.opn.ops {
if strings.HasPrefix(op.ID(), id) {
if found {
_, _ = fmt.Fprintf(os.Stderr, "unable to identify runbook: %s\n", id)
continue L
}
o = op
found = true
}
}
if !found {
_, _ = fmt.Fprintf(os.Stderr, "runbook not found: %s\n", id)
continue L
}
path = o.bookPath
if len(splitted) > 1 && splitted[1] != "" {
i, err := strconv.Atoi(splitted[1])
if err != nil {
found := false
for _, s := range o.steps {
if s.key == splitted[1] {
found = true
idx = s.idx
}
}
if !found {
_, _ = fmt.Fprintf(os.Stderr, "step not found: %s\n", splitted[1])
continue L
}
} else {
idx = i
}
} else {
idx = 0
}
}
b, err := readFile(path)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
continue
}
picked, err := pickStepYAML(string(b), idx)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
continue
}
fmt.Println(picked)
default:
_, _ = fmt.Fprintf(os.Stderr, "unknown command %s\n", in)
}
select {
case <-ctx.Done():
return ctx.Err()
default:
}
}
return nil
}
// storeKeys lists all keys in the store.
func storeKeys(store map[string]any) []string {
const storeKeySep = "."
var keys []string
for k := range store {
keys = append(keys, k)
switch v := store[k].(type) {
case map[string]any:
subKeys := storeKeys(v)
for _, sk := range subKeys {
keys = append(keys, k+storeKeySep+sk)
}
}
}
sort.Strings(keys)
return keys
}