forked from jawher/mow.cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow.go
68 lines (60 loc) · 1.15 KB
/
flow.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
package cli
import (
"fmt"
"strings"
)
type step struct {
do func()
success *step
error *step
desc string
}
func (s *step) run(p interface{}) {
s.callDo(p)
switch {
case s.success != nil:
s.success.run(p)
case p == nil:
return
default:
if code, ok := p.(exit); ok {
exiter(int(code))
return
}
panic(p)
}
}
func (s *step) callDo(p interface{}) {
if s.do == nil {
return
}
defer func() {
if e := recover(); e != nil {
if s.error == nil {
panic(p)
}
s.error.run(e)
}
}()
s.do()
}
func (s *step) dot() string {
trs := flowDot(s, map[*step]bool{})
return fmt.Sprintf("digraph G {\n\trankdir=LR\n%s\n}\n", strings.Join(trs, "\n"))
}
func flowDot(s *step, visited map[*step]bool) []string {
res := []string{}
if visited[s] {
return res
}
visited[s] = true
if s.success != nil {
res = append(res, fmt.Sprintf("\t\"%s\" -> \"%s\" [label=\"ok\"]", s.desc, s.success.desc))
res = append(res, flowDot(s.success, visited)...)
}
if s.error != nil {
res = append(res, fmt.Sprintf("\t\"%s\" -> \"%s\" [label=\"ko\"]", s.desc, s.error.desc))
res = append(res, flowDot(s.error, visited)...)
}
return res
}