-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.go
36 lines (32 loc) · 963 Bytes
/
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
package nettrigger
import (
"context"
"fmt"
)
// Action performs some action based on the environment and providers.
type Action func(context.Context, Environment, Providers) error
// BuildAction uses builders to construct an action from the specification.
func BuildAction(spec ActionSpec, builders ...ActionBuilder) (Action, error) {
for _, builder := range builders {
action, err := builder(spec)
if err != nil {
return action, err
}
if action != nil {
return action, nil
}
}
return nil, fmt.Errorf("unknown action type \"%s\"", spec.Type)
}
// BuildActions converts the given specifications into an action list.
func BuildActions(specs []ActionSpec, builders ...ActionBuilder) ([]Action, error) {
var actions []Action
for i, a := range specs {
action, err := BuildAction(a, builders...)
if err != nil {
return nil, fmt.Errorf("invalid action %d: %v", i, err)
}
actions = append(actions, action)
}
return actions, nil
}