-
Notifications
You must be signed in to change notification settings - Fork 1
/
actionspec.go
51 lines (46 loc) · 1.11 KB
/
actionspec.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
package nettrigger
import (
"fmt"
"strings"
)
// ActionSpec is a parsed action specification.
type ActionSpec struct {
Type string
Args []string
}
// Arg returns the argument with the nth index.
//
// An empty string is returned if the argument doesn't exist.
func (spec ActionSpec) Arg(n int) string {
if n >= len(spec.Args) {
return ""
}
return spec.Args[n]
}
// ParseAction parses v as a string representation of an action.
func ParseAction(v string) (ActionSpec, error) {
parts := strings.Fields(v)
switch len(parts) {
case 0:
return ActionSpec{}, fmt.Errorf("missing definition: \"%s\"", v)
case 1:
return ActionSpec{Type: parts[0]}, nil
default:
return ActionSpec{Type: parts[0], Args: parts[1:]}, nil
}
}
// ParseActions parses v as a string representation of an action list.
func ParseActions(v string) ([]ActionSpec, error) {
if v == "" {
return nil, nil
}
var actions []ActionSpec
for i, a := range strings.Split(v, ",") {
action, err := ParseAction(a)
if err != nil {
return nil, fmt.Errorf("invalid action %d: %v", i, err)
}
actions = append(actions, action)
}
return actions, nil
}