-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.go
209 lines (180 loc) · 4.28 KB
/
action.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
package action
import (
"context"
"errors"
)
// Action is just that, an action for your application.
type Action struct {
// Name action's name
Name string
// The *Run functions are executed in the following order:
// * PersistentPreRun()
// * PreRun()
// * Run()
// * PostRun()
// * PersistentPostRun()
// All functions get the same args.
//
// PersistentPreRun: children of this action will inherit and execute.
PersistentPreRun func(act *Action) error
// PreRun: children of this action will not inherit.
PreRun func(act *Action) error
// Run: Typically the actual work function. Most actions will only implement this.
Run func(act *Action) error
// PostRun: run after the Run action.
PostRun func(act *Action) error
// PersistentPostRun: children of this action will inherit and execute after PostRun.
PersistentPostRun func(act *Action) error
// Executable: whether is an executable action.
Executable func(act *Action) bool
// actions is the list of actions supported by this action.
actions []*Action
// parent is a parent action for this action.
parent *Action
ctx context.Context
}
// Context returns underlying action context. If action wasn't
// executed with ExecuteContext Context returns Background context.
func (a *Action) Context() context.Context {
return a.ctx
}
// HasParent determines if the action is a child action.
func (a *Action) HasParent() bool {
return a.parent != nil
}
// Runnable determines if the action is itself runnable.
func (a *Action) Runnable() bool {
return a.Run != nil
}
// Parent returns a actions parent action.
func (a *Action) Parent() *Action {
return a.parent
}
// Root finds root Action.
func (a *Action) Root() *Action {
if a.HasParent() {
return a.Parent().Root()
}
return a
}
// HasSubActions determines if the Action has children actions.
func (a *Action) HasSubActions() bool {
return len(a.actions) > 0
}
// Actions returns a slice of child actions.
func (a *Action) Actions() []*Action {
return a.actions
}
// AddAction adds one or more actions to this parent action.
func (a *Action) AddAction(actions ...*Action) error {
for i, x := range actions {
if actions[i] == a {
return errors.New("action can't be a child of itself")
}
actions[i].parent = a
a.actions = append(a.actions, x)
}
return nil
}
// RemoveAction removes one or more actions from a parent action.
func (a *Action) RemoveAction(actions ...*Action) {
var acts []*Action
main:
for _, action := range a.actions {
for _, act := range actions {
if action == act {
action.parent = nil
continue main
}
}
acts = append(acts, action)
}
a.actions = acts
}
// ExecuteContext is the same as Execute(), but sets the ctx on the action.
func (a *Action) ExecuteContext(ctx context.Context) (err error) {
a.ctx = ctx
return a.Execute()
}
// Execute executes the action.
func (a *Action) Execute() (err error) {
if a.ctx == nil {
a.ctx = context.Background()
}
// Regardless of what action execute is called on, run on Root only
if a.HasParent() {
return a.Root().Execute()
}
var target *Action
act := a.Find()
if act != nil {
target = act
} else {
target = a
}
// We have to pass global context to children action
// if context is present on the parent action.
if target.ctx == nil {
target.ctx = a.ctx
}
err = target.execute()
if err != nil {
return err
}
return err
}
// Find the first executable action.
func (a *Action) Find() *Action {
if a.Executable != nil && a.Executable(a) {
return a
}
if !a.HasSubActions() {
return nil
}
for _, v := range a.Actions() {
if target := v.Find(); target != nil {
return target
}
}
return nil
}
func (a *Action) execute() error {
if a == nil {
return nil
}
if !a.Runnable() {
return nil
}
for p := a; p != nil; p = p.Parent() {
if p.PersistentPreRun != nil {
if err := p.PersistentPreRun(a); err != nil {
return err
}
break
}
}
if a.PreRun != nil {
if err := a.PreRun(a); err != nil {
return err
}
}
if a.Run != nil {
if err := a.Run(a); err != nil {
return err
}
}
if a.PostRun != nil {
if err := a.PostRun(a); err != nil {
return err
}
}
for p := a; p != nil; p = p.Parent() {
if p.PersistentPostRun != nil {
if err := p.PersistentPostRun(a); err != nil {
return err
}
break
}
}
return nil
}