-
Notifications
You must be signed in to change notification settings - Fork 31
/
task.go
161 lines (122 loc) · 2.25 KB
/
task.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
package tachyon
import (
"fmt"
"strings"
)
type Task struct {
Play *Play
File string
data TaskData
cmd string
args string
Vars Vars
IncludeVars Vars
Paths Paths
}
type TaskData map[string]interface{}
type Tasks []*Task
func AdhocTask(cmd, args string) *Task {
return &Task{
cmd: cmd,
args: args,
data: TaskData{"name": "adhoc"},
Vars: make(Vars),
}
}
var cOptions = []string{
"name", "action", "notify", "async", "poll",
"when", "future", "register", "with_items",
}
func (t *Task) Init(env *Environment) error {
t.Vars = make(Vars)
for k, v := range t.data {
found := false
for _, i := range cOptions {
if k == i {
found = true
break
}
}
if !found {
if t.cmd != "" {
return fmt.Errorf("Duplicate command '%s', already: %s", k, t.cmd)
}
t.cmd = k
if m, ok := v.(map[interface{}]interface{}); ok {
for ik, iv := range m {
t.Vars[fmt.Sprintf("%v", ik)] = Any(iv)
}
} else {
t.args = fmt.Sprintf("%v", v)
}
}
}
if t.cmd == "" {
act, ok := t.data["action"]
if !ok {
return fmt.Errorf("No action specified")
}
parts := strings.SplitN(fmt.Sprintf("%v", act), " ", 2)
t.cmd = parts[0]
if len(parts) == 2 {
t.args = parts[1]
}
}
t.Paths = env.Paths
return nil
}
func (t *Task) Command() string {
return t.cmd
}
func (t *Task) Args() string {
return t.args
}
func (t *Task) Name() string {
return t.data["name"].(string)
}
func (t *Task) Register() string {
if v, ok := t.data["register"]; ok {
return v.(string)
}
return ""
}
func (t *Task) Future() string {
if v, ok := t.data["future"]; ok {
return v.(string)
}
return ""
}
func (t *Task) When() string {
if v, ok := t.data["when"]; ok {
return v.(string)
}
return ""
}
func (t *Task) Notify() []string {
var v interface{}
var ok bool
if v, ok = t.data["notify"]; !ok {
return nil
}
var list []interface{}
if list, ok = v.([]interface{}); !ok {
return nil
}
out := make([]string, len(list))
for i, x := range list {
out[i] = x.(string)
}
return out
}
func (t *Task) Async() bool {
_, ok := t.data["async"]
return ok
}
func (t *Task) Items() []interface{} {
if v, ok := t.data["with_items"]; ok {
if a, ok := v.([]interface{}); ok {
return a
}
}
return nil
}