-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
223 lines (190 loc) · 4.33 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package recipe
import (
"bytes"
"encoding/json"
"os"
"os/exec"
"strings"
"sync"
)
/*
* Sources:
* - https://blog.kowalczyk.info/article/wOYk/advanced-command-execution-in-go-with-osexec.html
*/
type Task struct {
Deps []string `json:"deps" toml:"deps"`
Env map[string]string `json:"env" toml:"env"`
Interp []string `json:"interp" toml:"interp"`
Cmd string `json:"cmd" toml:"cmd"`
Stdout string `json:"stdout" toml:"stdout"`
Stderr string `json:"stderr" toml:"stderr"`
AllowFailure bool `json:"allow_failure" toml:"allow_failure"`
cmd *exec.Cmd
mu sync.RWMutex
}
/***
* Task
*/
/*
// NOTE: This snippet shows how to reset unmarshaled structs to have different default values
// WARNING: At this moment is not working with TOML due to a go-toml limitation: https://github.com/pelletier/go-toml/blob/master/marshal.go#L318
// Used to avoid recursion in UnmarshalJSON below.
type task Task
func (t *task) reset() {
t.Deps = make([]string, 0)
t.Env = make(map[string]string, 0)
t.Interp = make([]string, 0)
t.Cmd = ""
t.Stdout = ""
t.Stderr = ""
t.AllowFailure = false
}
func (t *Task) UnmarshalJSON(b []byte) error {
fmt.Printf("DEBUG TASK UnmarshalJSON\n")
newT := task{}
newT.reset()
err := json.Unmarshal(b, &newT)
if err != nil {
return err
}
t.Deps = newT.Deps
t.Env = newT.Env
t.Interp = newT.Interp
t.Cmd = newT.Cmd
t.Stdout = newT.Stdout
t.Stderr = newT.Stderr
t.AllowFailure = newT.AllowFailure
t.mu = newT.mu
fmt.Printf("DEBUG task = %s\n", t)
return nil
}
func (t *Task) UnmarshalTOML(b []byte) error {
fmt.Printf("DEBUG TASK UnmarshalTOML\n")
newT := task{}
newT.reset()
err := toml.Unmarshal(b, &newT)
if err != nil {
return err
}
t.Deps = newT.Deps
t.Env = newT.Env
t.Interp = newT.Interp
t.Cmd = newT.Cmd
t.Stdout = newT.Stdout
t.Stderr = newT.Stderr
t.AllowFailure = newT.AllowFailure
t.mu = newT.mu
fmt.Printf("DEBUG task = %s\n", t)
return nil
}
*/
func (t *Task) composeEnv(r *Recipe) []string {
newEnv := os.Environ()
for key, value := range r.Environ() {
newEnv = append(newEnv, key+"="+value)
}
for key, value := range t.Environ() {
newEnv = append(newEnv, key+"="+value)
}
return newEnv
}
func replaceCmd(parts []string, spell string) []string {
newParts := make([]string, len(parts))
for i, txt := range parts {
newParts[i] = strings.Replace(txt, "{cmd}", spell, -1)
}
return newParts
}
func (t *Task) composeInterpreterCmd(spell string, r *Recipe) []string {
// Check task config
if parts := t.Interpreter(); parts != nil {
if len(parts) == 0 {
return t.composeDefaultInterpreterCmd(spell)
}
return replaceCmd(parts, spell)
}
// Check recipe config
if parts := r.Interpreter(); parts != nil {
if len(parts) == 0 {
return t.composeDefaultInterpreterCmd(spell)
}
return replaceCmd(parts, spell)
}
return t.composeDefaultInterpreterCmd(spell)
}
func (t *Task) Execute(r *Recipe) error {
t.mu.RLock()
defer t.mu.RUnlock()
if t.Cmd == "" {
return nil
}
parts := t.composeInterpreterCmd(t.Cmd, r)
env := t.composeEnv(r)
// Search program
path, err := exec.LookPath(parts[0])
if err != nil {
return err
}
// Create cmd
t.cmd = exec.Command(path, parts[1:]...)
// Redirect stdout and stderr
if t.Stdout != "" {
f, err := os.Create(t.Stdout)
if err != nil {
return err
}
defer f.Close()
t.cmd.Stdout = f
} else {
t.cmd.Stdout = os.Stdout
}
if t.Stderr != "" {
f, err := os.Create(t.Stderr)
if err != nil {
return err
}
defer f.Close()
t.cmd.Stderr = f
} else {
t.cmd.Stderr = os.Stderr
}
t.cmd.Env = env
// Set SysProcAttr
t.setSysProcAttr()
// Run
err = t.cmd.Run()
if err != nil {
return err
}
return nil
}
func (t *Task) Environ() map[string]string {
t.mu.RLock()
defer t.mu.RUnlock()
return t.Env
}
func (t *Task) Interpreter() []string {
t.mu.RLock()
defer t.mu.RUnlock()
return t.Interp
}
func (t *Task) String() string {
return t.serialize(false).String()
}
func (t *Task) PrettyString() string {
return t.serialize(true).String()
}
func (t *Task) serialize(indent bool) *bytes.Buffer {
t.mu.RLock()
defer t.mu.RUnlock()
b := bytes.Buffer{}
e := json.NewEncoder(&b)
if indent {
e.SetIndent("", " ")
}
err := e.Encode(t)
if err != nil {
panic(err)
}
return &b
}