-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
79 lines (64 loc) · 1.58 KB
/
commands.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
package otium
import (
"fmt"
"os"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
// cmdVariables implements the "variables" command.
func cmdVariables(pcd *Procedure) {
if len(pcd.bag.bag) == 0 {
fmt.Println("no variables")
return
}
keys := maps.Keys(pcd.bag.bag)
slices.Sort(keys)
for _, k := range keys {
v := pcd.bag.bag[k]
if v.set {
fmt.Printf("%s (%s): %v\n", k, v.Desc, v.val)
} else {
fmt.Printf("%s (%s): <unset>\n", k, v.Desc)
}
}
}
type visitFn func(pcd *Procedure, step *Step) error
// cmdNext implements the "next" command.
func cmdNext(pcd *Procedure) error {
return visitStep(pcd, visitAsNext)
}
func visitStep(pcd *Procedure, visitor visitFn) error {
if pcd.stepIdx >= len(pcd.steps) {
return fmt.Errorf("next: internal error: step index > len(steps)")
}
step := pcd.steps[pcd.stepIdx]
fmt.Printf("\n## %d. %s %s\n\n", pcd.stepIdx+1, step.Icon(), step.Title)
if step.Desc != "" {
if err := renderTemplate(os.Stdout, step.Desc, pcd.bag.bag); err != nil {
return fmt.Errorf("%s %w", err, ErrUnrecoverable)
}
fmt.Printf("\n\n")
}
if visitor != nil {
if err := visitor(pcd, step); err != nil {
return err
}
}
pcd.stepIdx++
return nil
}
func visitAsNext(pcd *Procedure, step *Step) error {
// Prompt the user for the declared variables.
for _, variable := range step.Vars {
if _, err := pcd.bag.ask(variable.Name, pcd.term); err != nil {
return err
}
}
// Run the step.
if step.Run != nil {
if err := step.Run(pcd.bag, pcd.uctx); err != nil {
return fmt.Errorf("step %d: %w", pcd.stepIdx+1, err)
}
}
return nil
}