/
main.go
251 lines (204 loc) · 4.94 KB
/
main.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"fmt"
"github.com/Redundancy/jobless"
"github.com/codegangsta/cli"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
var app *cli.App = cli.NewApp()
func main() {
app.Name = "jobless"
app.Usage = "Organize your project commands effortlessly, then automate it."
app.Flags = []cli.Flag{}
runtime.GOMAXPROCS(runtime.NumCPU())
app.Commands = append(
app.Commands,
cli.Command{
Name: "find",
ShortName: "f",
Usage: "jobless find <pattern>",
Description: `finds tasks with a given pattern. Defaults to ** (all)`,
Action: find,
Flags: []cli.Flag{},
},
)
app.Commands = append(
app.Commands,
cli.Command{
Name: "run",
ShortName: "r",
Usage: "jobless run <pattern>",
Description: `runs tasks with a given pattern. Defaults to ** (all)`,
Action: run,
Flags: []cli.Flag{},
},
)
app.Run(os.Args)
}
func findParentStore(
path string,
stores map[string]*jobless.ChainedVariableStore,
) *jobless.ChainedVariableStore {
d := filepath.Dir(path)
for d[len(d)-1] != filepath.Separator {
if store, found := stores[d]; found {
return store
}
d = filepath.Dir(d)
}
return nil
}
func find(c *cli.Context) {
pattern := "**"
if len(c.Args()) == 1 {
pattern = c.Args()[0]
}
cwd, err := os.Getwd()
if err != nil {
fmt.Printf("Failed to get cwd: %v", err)
os.Exit(1)
}
WalkJoblessFiles(
cwd,
func(jobFile *JoblessFile) error {
for _, task := range jobFile.Tasks {
if task.Name.Matches(pattern) {
fmt.Printf("%v in %v\n", task.Name, jobFile.Filepath)
}
}
return nil
},
)
return
}
func run(c *cli.Context) {
pattern := "**"
if len(c.Args()) == 1 {
pattern = c.Args()[0]
}
cwd, err := os.Getwd()
if err != nil {
fmt.Printf("Failed to get cwd: %v", err)
os.Exit(1)
}
pathStores := make(map[string]*jobless.ChainedVariableStore)
if originalWd, err := os.Getwd(); err != nil {
log.Println(err)
os.Exit(1)
} else {
defer os.Chdir(originalWd)
}
err = WalkJoblessFiles(
cwd,
func(jobFile *JoblessFile) error {
parent := findParentStore(jobFile.Filepath, pathStores)
taskDir := filepath.Dir(jobFile.Filepath)
/*if parent != nil {
log.Println("Chaining", jobFile.Filepath, "to", parent.Path)
}*/
thisStore := &jobless.ChainedVariableStore{
Path: jobFile.Filepath,
Variables: jobFile.Variables,
Parent: parent,
}
pathStores[taskDir] = thisStore
for _, task := range jobFile.Tasks {
if !task.Name.Matches(pattern) {
continue
}
log.Println("--------", task.Name, "--------")
var err error
resolvedEnvironment := make(map[string]string, len(task.Environment))
for k, v := range task.Environment {
//log.Println("Resolving Env:", k)
resolvedEnvironment[k], err = thisStore.ResolveString(v)
if err != nil {
return fmt.Errorf(
"Could not resolve environment variable %v for task %v: %v",
k,
task.Name,
err,
)
}
}
//log.Println("Resolving Exe:", task.Command[0])
executable, err := thisStore.ResolveString(task.Command[0])
if err != nil {
return err
}
arguments := task.Command[1:]
if filepath.Ext(executable) == ".bat" {
arguments = append(
[]string{"/c", executable},
arguments...,
)
executable = "cmd.exe"
}
wd, err := thisStore.ResolveString(task.CWD)
if err != nil {
return fmt.Errorf(
"Could not resolve CWD for task %v - %v: %v",
task.Name,
task.CWD,
err,
)
}
if wd == "" {
wd = taskDir
}
if err = os.Chdir(wd); err != nil {
return err
}
// Set the CWD correctly before creating the command object
cmd := exec.Command(executable, arguments...)
cmd.Dir = wd
log.Println("Executing:", executable, arguments)
setEnvironment := make(map[string]bool, len(task.Environment))
cmd.Env = make([]string, 0, len(os.Environ()))
for _, e := range os.Environ() {
kv := strings.Split(e, "=")
k := kv[0]
if joblessEnvValue, present := resolvedEnvironment[k]; present {
log.Println("Env", k, "=", joblessEnvValue)
cmd.Env = append(cmd.Env, k+"="+joblessEnvValue)
setEnvironment[k] = true
} else {
//log.Println("Env inherit:", e)
cmd.Env = append(cmd.Env, e)
}
}
for k, v := range resolvedEnvironment {
if !setEnvironment[k] {
log.Println("Env", k, "=", v)
cmd.Env = append(cmd.Env, k+"="+v)
}
}
outPipe, _ := cmd.StdoutPipe()
defer outPipe.Close()
errPipe, _ := cmd.StderrPipe()
defer errPipe.Close()
go io.Copy(os.Stdout, outPipe)
go io.Copy(os.Stderr, errPipe)
log.Println("Working Directory =", cmd.Dir)
err = cmd.Run()
if err == nil {
log.Println("-------- Done --------")
} else {
return err
}
}
return nil
},
)
if err != nil {
log.Println(err)
os.Exit(1)
}
return
}