-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcommandData.go
398 lines (336 loc) · 9.51 KB
/
commandData.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
/*
* 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"
"io/ioutil"
"log"
"os/user"
"strconv"
"strings"
"github.com/dreadl0ck/readline"
)
// command header
// used in scripts to supply information for ZEUS
type commandData struct {
// one line Description text
Description string `yaml:"description"`
// scripting language of the command
Language string `yaml:"language"`
// Help page text
Help string `yaml:"help"`
// Arguments
Arguments []string `yaml:"arguments"`
// Dependencies
Dependencies []string `yaml:"dependencies"`
// outputs
Outputs []string `yaml:"outputs"`
// increase buildnumber on each execution
BuildNumber bool `yaml:"buildNumber"`
// execute command in a detached screen session
Async bool `yaml:"async"`
// Exec is the script to run when executed
Exec string `yaml:"exec"`
// Path allows to set a custom path for the command
Path string `yaml:"path"`
// Hidden controls if the command is shown the menu
Hidden bool `yaml:"hidden"`
// CanModifyPrompt controls if the command can modify the zeus prompt
CanModifyPrompt bool `yaml:"canModifyPrompt"`
// Extends sets the base configuration to use for this command
Extends string `yaml:"extends"`
// WorkingDir overwrites the working directory for this command
WorkingDir string `yaml:"workingDir"`
}
// initialize a command from a commandData instance
// returns if command does already exist
func (d *commandData) init(commandsFile *CommandsFile, name string) error {
// check if a path is set and an exec section specified
// that's invalid - print debug info and return an error
if d.Path != "" && d.Exec != "" {
c, err := ioutil.ReadFile(commandsFilePath)
if err != nil {
return err
}
var (
highlightLine int
commandStarted bool
)
for index, line := range strings.Split(string(c), "\n") {
if strings.Contains(line, name+":") {
commandStarted = true
}
if commandStarted {
if strings.Contains(line, d.Path) {
highlightLine = index
break
}
}
}
printCodeSnippet(string(c), commandsFilePath, highlightLine)
return errors.New("command " + name + " has custom path set, but specifies an exec action")
}
// check if the current command is a dependency and abort if true
for index, dep := range d.Dependencies {
fields := strings.Fields(dep)
if len(fields) >= 1 {
if fields[0] == name {
c, err := ioutil.ReadFile(commandsFilePath)
if err != nil {
return err
}
var (
highlightLine int
commandStarted bool
)
for index, line := range strings.Split(string(c), "\n") {
if strings.Contains(line, name+":") {
commandStarted = true
}
if commandStarted {
if strings.Contains(line, "- "+name) {
highlightLine = index
break
}
}
}
printCodeSnippet(string(c), commandsFilePath, highlightLine)
return errors.New("command " + name + " has itself as dependency at index: " + strconv.Itoa(index) + " This will result in a loop")
}
}
}
// assemble commands args
args, err := commandsFile.validateArgs(d.Arguments)
if err != nil {
return errors.New("command " + name + ": " + err.Error())
}
var lang string
if d.Language == "" {
lang = commandsFile.Language
} else {
lang = d.Language
}
// create command
cmd := &command{
name: name,
args: args,
description: d.Description,
help: d.Help,
hidden: d.Hidden,
// PrefixCompleter: readline.PcItem(name,
// readline.PcItemDynamic(func(path string) (res []string) {
// // fmt.Println("\npath:", path)
// var allRequiredArgsSet = true
// for _, a := range args {
// if !strings.Contains(path, a.name+"=") {
// res = append(res, a.name+"=")
// if !a.optional {
// allRequiredArgsSet = false
// }
// }
// }
// if !allRequiredArgsSet {
// return
// }
// if allRequiredArgsSet && strings.HasSuffix(path, commandChainSeparator+" ") {
// // return all available commands
// cmdMap.Lock()
// defer cmdMap.Unlock()
// for name := range cmdMap.items {
// res = append(res, name)
// }
// // fmt.Println("allRequiredArgsSet:", allRequiredArgsSet, "commands result:", res)
// return
// }
// if allRequiredArgsSet {
// res = append(res, "->")
// }
// return
// }),
// ),
PrefixCompleter: readline.PcItem(name,
// completer for current commands arguments
readline.PcItemDynamic(func(path string) (res []string) {
var allRequiredArgsSet = true
for _, a := range args {
if !strings.Contains(path, a.name+"=") {
res = append(res, a.name+"=")
if !a.optional {
allRequiredArgsSet = false
}
}
}
if allRequiredArgsSet {
res = append(res, commandChainSeparator)
}
// l.Println("\npath:", path)
// l.Println("result:", res)
return
},
// completer for next command names
readline.PcItemDynamic(func(path string) (res []string) {
// return all available commands
cmdMap.Lock()
defer cmdMap.Unlock()
for name := range cmdMap.items {
res = append(res, name)
}
// l.Println("\npath:", path)
// l.Println("result:", res)
return
},
// completer for next commands args
readline.PcItemDynamic(func(path string) (res []string) {
slice := strings.Split(path, commandChainSeparator)
if len(slice) == 0 {
return
}
cmdArgSlice := strings.Fields(slice[len(slice)-1])
if len(cmdArgSlice) == 0 {
return
}
cmdMap.Lock()
c, ok := cmdMap.items[cmdArgSlice[0]]
if !ok {
cmdMap.Unlock()
return
}
cmdMap.Unlock()
// return the next commands completer?
// return c.PrefixCompleter.Callback(path)
var allRequiredArgsSet = true
for _, a := range c.args {
if !strings.Contains(path, a.name+"=") {
res = append(res, a.name+"=")
if !a.optional {
allRequiredArgsSet = false
}
}
}
if allRequiredArgsSet {
res = append(res, commandChainSeparator)
}
// l.Println("\npath:", path)
// l.Println("result:", res)
return
}),
),
),
),
buildNumber: d.BuildNumber,
dependencies: d.Dependencies,
outputs: d.Outputs,
exec: d.Exec,
async: d.Async,
language: lang,
canModifyPrompt: d.CanModifyPrompt,
extends: d.Extends,
}
if d.Path != "" {
// 1) expand home dir
var p string
for _, v := range []string{"~", "${HOME}", "$HOME"} {
if strings.Contains(d.Path, v) {
u, err := user.Current()
if err != nil {
log.Fatal(err)
}
// replace home dir variable with path
p = strings.Replace(d.Path, v, u.HomeDir, 1)
break
}
}
if p == "" {
p = d.Path
}
// 2) expand ZEUS globals
p = commandsFile.replaceGlobals(p)
// 3) update workingDir
cmd.path = p
}
if d.WorkingDir != "" {
// 1) expand home dir
var workDir string
for _, v := range []string{"~", "${HOME}", "$HOME"} {
if strings.Contains(d.WorkingDir, v) {
u, err := user.Current()
if err != nil {
log.Fatal(err)
}
// replace home dir variable with path
workDir = strings.Replace(d.WorkingDir, v, u.HomeDir, 1)
break
}
}
if workDir == "" {
workDir = d.WorkingDir
}
// 2) expand ZEUS globals
workDir = commandsFile.replaceGlobals(workDir)
// 3) update workingDir
cmd.workingDir = workDir
}
if lang == "go" && d.Exec != "" {
return errors.New("when using Go, use of the exec field is not allowed. Please a create a file in the scripts folder instead")
}
// replace globals in outputs
for i, o := range cmd.outputs {
cmd.outputs[i] = commandsFile.replaceGlobals(o)
}
// replace globals in dependencies
for i, dep := range cmd.dependencies {
cmd.dependencies[i] = commandsFile.replaceGlobals(dep)
}
// disable completion for hidden commands
if d.Hidden {
cmd.PrefixCompleter = readline.NewPrefixCompleter()
}
if d.Exec == "" {
if d.Path == "" {
l, err := cmd.getLanguage()
if err != nil {
return err
}
cmd.path = scriptDir + "/" + name + l.FileExtension
}
}
var exists bool
// update the completer if a completion exists
completer.Lock()
for i, c := range completer.Children {
if string(cmd.PrefixCompleter.GetName()) == string(c.GetName()) {
exists = true
// update completer
completer.Children[i] = cmd.PrefixCompleter
}
}
// add to completer if none exists
if !exists {
completer.Children = append(completer.Children, cmd.PrefixCompleter)
}
completer.Unlock()
// add to command map
cmdMap.Lock()
cmdMap.items[cmd.name] = cmd
cmdMap.Unlock()
Log.WithField("prefix", "parseCommandsFile").Debug("added " + cp.CmdName + cmd.name + cp.Reset + " to the command map")
// if debug {
// cmd.dump()
// }
return nil
}