-
Notifications
You must be signed in to change notification settings - Fork 20
/
commandsFile.go
510 lines (426 loc) · 12.7 KB
/
commandsFile.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* ZEUS - An Electrifying Build System
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/mgutz/ansi"
yaml "gopkg.in/yaml.v2"
)
var (
// default path for CommandsFile
commandsFilePath = zeusDir + "/commands.yml"
// ErrFailedToReadCommandsFile occurs when the CommandsFile could not be read
ErrFailedToReadCommandsFile = errors.New("failed to read commandsFile")
)
// CommandsFile contains globals and commands for the main zeus configuration file commands.yml
type CommandsFile struct {
// Override default language bash
Language string `yaml:"language"`
// global vars for all commands
Globals map[string]string `yaml:"globals"`
// command data
Commands map[string]*commandData `yaml:"commands"`
// script to call when starting zeus
StartupHook string `yaml:"startupHook"`
// script to call when exiting zeus
ExitHook string `yaml:"exitHook"`
// commandsFile that is extended by the current commandsFile.
// commands from this file will be executed within the CURRENT zeus directory.
Extends string `yaml:"extends"`
// commandsFile that is extended by the current commandsFile.
// commands from this file will be executed within the ORIGINAL zeus directory.
Includes string `yaml:"includes"`
}
func newCommandsFile() *CommandsFile {
return &CommandsFile{
Language: "bash",
Globals: make(map[string]string, 0),
Commands: make(map[string]*commandData, 0),
}
}
// parse and initialize all commands from the CommandsFile
func parseCommandsFile(path string, flush bool) (*CommandsFile, error) {
var (
start = time.Now()
commandsFile = newCommandsFile()
)
// read file contents
contents, err := ioutil.ReadFile(path)
if err != nil {
Log.Debug(err)
return nil, errors.New(ErrFailedToReadCommandsFile.Error() + ": " + err.Error())
}
// unmarshal YAML
err = yaml.UnmarshalStrict(contents, commandsFile)
if err != nil {
i, lineErr := extractLineNumFromError(err.Error(), "line")
if lineErr == ErrNoLineNumberFound {
i = -1
} else if lineErr != nil {
l.Println("failed to retrieve line number in which the error occurred:", lineErr)
i = -1
}
if !shellBusy {
printCodeSnippet(string(contents), commandsFilePath, i)
}
return nil, err
}
// catch attempts to use includes and extends at the same time
// TODO: add support for this in the future
if commandsFile.Extends != "" && commandsFile.Includes != "" {
return nil, errors.New("commandsFile options 'extends' and 'includes' can't be used in combination at the moment")
}
// check if language is supported
_, err = ls.getLang(commandsFile.Language)
if err != nil {
return nil, errors.New(commandsFilePath + ": " + err.Error() + ": " + ansi.Red + commandsFile.Language + cp.Text)
}
if flush {
// flush command map
cmdMap.flush()
g = &globals{
Vars: make(map[string]string, 0),
}
}
if len(commandsFile.Globals) > 0 {
if g != nil {
for k, v := range commandsFile.Globals {
g.Vars[k] = v
}
} else {
// init
g = &globals{
Vars: commandsFile.Globals,
}
}
// resolve environment vars used in the globals
// must be in format ${VAR}
for k, v := range g.Vars {
g.Vars[k] = resolveEnvironment(v)
}
}
// initialize commands
for name, d := range commandsFile.Commands {
if d != nil {
err = d.init(commandsFile, name)
if err != nil {
return nil, errors.New("failed to init command: " + err.Error())
}
}
}
// handle base configurations
// since this allows commands to cross reference each other, this must be done after all commands have been initialized.
cmdMap.Lock()
defer cmdMap.Unlock()
// get current working directory
wd, err := os.Getwd()
if err != nil {
return nil, err
}
// set working directory for all commands that are from the current commandsFile
for name := range commandsFile.Commands {
if cmd, ok := cmdMap.items[name]; ok {
if cmd.workingDir == "" {
cmd.workingDir = wd
}
}
}
for _, cmd := range cmdMap.items {
if cmd.extends != "" {
if baseCmd, ok := cmdMap.items[cmd.extends]; ok {
// handle arguments
// save old args
oldArgs := cmd.args
// overwrite args with base args
cmd.args = baseCmd.args
// add the args of the current command again. this will allow to overwrite args from the base if desired.
for n, a := range oldArgs {
cmd.args[n] = a
}
// prepend outputs from base command
cmd.outputs = append(baseCmd.outputs, cmd.outputs...)
// if no description is provided for the current command, use the one from the base command.
if cmd.description == "" {
cmd.description = baseCmd.description
}
// if no help text is provided for the current command, use the one from the base command.
if cmd.help == "" {
cmd.help = baseCmd.help
}
// prepend deps from base command
cmd.dependencies = append(baseCmd.dependencies, cmd.dependencies...)
cmd.canModifyPrompt = baseCmd.canModifyPrompt
cmd.buildNumber = baseCmd.buildNumber
cmd.language = baseCmd.language
cmd.async = baseCmd.async
// handle exec action
if cmd.exec == "" && baseCmd.exec != "" {
cmd.exec = baseCmd.exec
}
if cmd.path == "" && baseCmd.path != "" {
cmd.path = baseCmd.path
}
} else {
return nil, errors.New("base command not found: " + cmd.extends)
}
}
}
// only print info when using the interactive shell
if len(os.Args) == 1 {
if conf.fields.Debug {
l.Println(cp.Text+"initialized "+cp.Prompt, len(cmdMap.items), cp.Text+" commands from CommandsFile in: "+cp.Prompt, time.Now().Sub(start), cp.Reset+"\n")
}
}
// invoke startupHook if set
if commandsFile.StartupHook != "" {
out, err := exec.Command(commandsFile.StartupHook).CombinedOutput()
if err != nil {
// cleanup without calling the exitHook to avoid confusion
// in case the startupHook fails, and the exitHook would fail as well due to that.
cleanup(nil)
fmt.Println(string(out))
log.Fatal("startupHook failed: ", err)
}
modifyPrompt()
}
return commandsFile, nil
}
// count prefix whitespace characters of a string
func countLeadingSpace(line string) int {
i := 0
for _, runeValue := range line {
if runeValue == ' ' {
i++
} else {
break
}
}
return i
}
var lastCommandsFileError error
// watch zeus file for changes and parse again
func watchCommandsFile(path, eventID string) {
// don't add a new watcher when the event exists
projectData.Lock()
for _, e := range projectData.fields.Events {
if e.Name == "commandsFile watcher" {
projectData.Unlock()
return
}
}
projectData.Unlock()
Log.Debug("watching commandsFile at ", path)
err := addEvent(newEvent(path, fsnotify.Write, "commandsFile watcher", ".yml", eventID, "internal", func(e fsnotify.Event) {
// without sleeping every line written to stdout has the length of the previous line as offset
// sleeping at least 100 millisecs seems to work - strange
//time.Sleep(100 * time.Millisecond)
//l.Println()
Log.Debug("received commandsFile WRITE event: ", e.Name)
cmdFile, err := parseCommandsFile(path, true)
if !shellBusy {
if err != nil {
// flush command map
cmdMap.flush()
g = &globals{
Vars: make(map[string]string, 0),
}
Log.WithError(err).Error("failed to parse commandsFile")
}
} else {
if err != nil {
// flush command map
cmdMap.flush()
g = &globals{
Vars: make(map[string]string, 0),
}
// shell is currently busy. store the error to present it to the user once the shell is free again.
lastCommandsFileError = err
} else {
// commandsFile was parsed successfully in the background. Make sure previous error is cleared.
lastCommandsFileError = nil
}
}
if cmdFile != nil {
// handle commandsFile extension
cmdFile.handleExtension()
// handle commandsFile inclusion
cmdFile.handleInclusion()
}
}))
if err != nil {
Log.WithError(err).Error("failed to watch commandsFile")
}
}
func createAllScripts() error {
// parse file
var (
commandsFile = newCommandsFile()
)
contents, err := ioutil.ReadFile(commandsFilePath)
if err != nil {
l.Println("unable to read " + commandsFilePath)
return err
}
err = yaml.Unmarshal(contents, commandsFile)
if err != nil {
return err
}
// create scriptDir if necessary
if _, err = os.Stat(scriptDir); err != nil {
err = os.MkdirAll(scriptDir, 0700)
if err != nil {
Log.WithError(err).Fatal("failed to create " + scriptDir + " directory")
}
}
// check if language is valid
_, err = ls.getLang(commandsFile.Language)
if err != nil {
return errors.New("CommandsFile: " + err.Error() + ": " + commandsFile.Language)
}
// write commands to disk
for name, d := range commandsFile.Commands {
if d.Path == "" {
err = commandsFile.createScript(d, name)
if err != nil {
l.Println(err)
continue
}
err = stripExecSectionFromCommandsFile(name)
if err != nil {
return errors.New("failed to strip exec section from commandsFile: " + err.Error())
}
} else {
l.Println("skipping command " + name + " because it has a custom path set")
}
}
_, err = parseCommandsFile(commandsFilePath, true)
return err
}
func (c *CommandsFile) createScript(d *commandData, name string) error {
// set default language
if d.Language == "" {
d.Language = "bash"
}
lang, err := ls.getLang(d.Language)
if err != nil {
return errors.New(err.Error() + ": " + d.Language)
}
// check commands args
_, err = c.validateArgs(d.Arguments)
if err != nil {
return err
}
scriptName := scriptDir + "/" + name + lang.FileExtension
// make sure the file does not already exist
_, err = os.Stat(scriptName)
if err == nil {
return errors.New(scriptName + " already exists!")
}
// create command script
f, err := os.Create(scriptName)
if err != nil {
return err
}
f.WriteString(lang.Bang + "\n\n")
f.WriteString(d.Exec + "\n")
f.Close()
// flush exec field
d.Exec = ""
return nil
}
func (c *CommandsFile) handleExtension() {
// handle commandsFile extension
if c.Extends != "" {
// get current working directory
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
// change into extension directory
p := strings.TrimSuffix(filepath.Dir(c.Extends), "zeus")
err = os.Chdir(p)
if err != nil {
log.Fatal(err)
}
// check if a CommandsFile for the project exists
// parse and flush it, in order to use it as base for the actual commandsfile of this project
_, err = parseCommandsFile(commandsFilePath, true)
if err != nil {
Log.Error("failed to parse commandsFile: ", err, "\n")
os.Exit(1)
}
// move back into actual root directory
err = os.Chdir(wd)
if err != nil {
log.Fatal(err)
}
// update workingDirs on loaded commands to point to the current directory
for _, cmd := range cmdMap.items {
cmd.workingDir = wd
}
// check if a CommandsFile for the project exists and collect the commands
c, err = parseCommandsFile(commandsFilePath, false)
if err != nil {
Log.Error("failed to parse commandsFile: ", err, "\n")
os.Exit(1)
}
}
}
func (c *CommandsFile) handleInclusion() {
// handle commandsFile inclusion
if c.Includes != "" {
// get current working directory
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
// change into extension directory
p := strings.TrimSuffix(filepath.Dir(c.Includes), "zeus")
err = os.Chdir(p)
if err != nil {
log.Fatal(err)
}
// check if a CommandsFile for the project exists
// parse and flush it, in order to use it as base for the actual commandsFile of this project
_, err = parseCommandsFile(commandsFilePath, true)
if err != nil {
Log.Error("failed to parse commandsFile: ", err, "\n")
os.Exit(1)
}
// move back into actual root directory
err = os.Chdir(wd)
if err != nil {
log.Fatal(err)
}
// check if a CommandsFile for the project exists and collect the commands
c, err = parseCommandsFile(commandsFilePath, false)
if err != nil {
Log.Error("failed to parse commandsFile: ", err, "\n")
os.Exit(1)
}
}
}