-
Notifications
You must be signed in to change notification settings - Fork 20
/
bootstrap.go
296 lines (245 loc) · 6.59 KB
/
bootstrap.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
/*
* 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 (
"bytes"
"io/ioutil"
"os"
"strings"
"time"
"gopkg.in/yaml.v2"
)
// bootstrap basic zeus setup
// useful when starting from scratch
func runBootstrapCommand() {
err := os.MkdirAll(scriptDir, 0700)
if err != nil {
Log.WithError(err).Fatal("failed to create zeus directory")
}
f, err := os.Create(commandsFilePath)
if err != nil {
Log.WithError(err).Fatal("failed to create CommandsFile")
}
defer f.Close()
f.WriteString(asciiArtYAML + `
# default language
language: bash
# globals for all commands
globals:
# command data
commands:
# build the binary
build:
description: build project
dependencies:
- clean
buildNumber: true
exec: |
echo "build the binary"
# clean up the mess
clean:
description: clean up to prepare for build
exec: rm -rf bin/*
# perform install
install:
dependencies:
- clean
description: install to $PATH
help: Install the application to the default system location
exec: |
echo "perform install"
`)
}
func printCreateCommandUsageErr() {
l.Println("usage:")
l.Println("zeus create [<language> <commandName>] [script <all> | <commandName>]")
}
// bootstrap a single new command
// either append to CommandsFile or create a new script
// then drop into editor
func handleCreateCommand(args []string) {
if len(args) < 3 {
printCreateCommandUsageErr()
return
}
switch args[1] {
// create script <all> | <commandName>
case "script":
switch args[2] {
case "all":
err := createAllScripts()
if err != nil {
Log.WithError(err).Error("failed to create scripts from commands.yml")
}
default:
// check if command exists
cmdMap.Lock()
if cmd, ok := cmdMap.items[args[2]]; ok {
cmdMap.Unlock()
// get commandData from commandsFile
var commandsFile = newCommandsFile()
contents, err := ioutil.ReadFile(commandsFilePath)
if err != nil {
l.Println("unable to read " + commandsFilePath + ": " + err.Error())
return
}
err = yaml.UnmarshalStrict(contents, commandsFile)
if err != nil {
l.Println("failed to unmarshal commandsFile: " + err.Error())
return
}
if d, ok := commandsFile.Commands[args[2]]; ok {
err = commandsFile.createScript(d, cmd.name)
if err != nil {
l.Println("failed to create script: " + err.Error())
}
} else {
l.Println("could not find " + args[2] + " in commandsFile: " + err.Error())
}
err = stripExecSectionFromCommandsFile(cmd.name)
if err != nil {
l.Println("failed to strip exec section from commandsFile: " + err.Error())
}
return
}
cmdMap.Unlock()
l.Println("command " + args[2] + " does not exist!")
return
}
// create <lang> <commandName>
default:
lang, err := ls.getLang(args[1])
if err != nil {
l.Println("getLang: ", err)
return
}
// check if command exists
cmdMap.Lock()
if _, ok := cmdMap.items[args[2]]; ok {
cmdMap.Unlock()
l.Println("command " + args[2] + " exists!")
return
}
cmdMap.Unlock()
// check if there's a CommandsFile
_, err = os.Stat(commandsFilePath)
if err == nil {
// append command to CommandsFile
f, err := os.OpenFile(commandsFilePath, os.O_APPEND|os.O_WRONLY, 0744)
if err != nil {
Log.WithError(err).Error("failed to open CommandsFile for writing")
return
}
f.WriteString("\n" + ` ` + args[2] + `:
language: ` + lang.Name + `
description:
help:
arguments:
dependencies:
outputs:
exec: implement ` + args[2] + ` command
`)
err = f.Close()
if err != nil {
l.Println(err)
return
}
} else {
filename := scriptDir + "/" + args[2] + lang.FileExtension
// check if the script already exists
_, err := os.Stat(filename)
if err == nil {
l.Println("file " + filename + " exists!")
return
}
f, err := os.Create(filename)
if err != nil {
l.Println("failed to create file: ", err)
return
}
f.WriteString(lang.Bang + "\n")
err = f.Close()
if err != nil {
l.Println(err)
return
}
l.Println("created zeus command at " + filename)
}
// the WRITE event will cause the commandsFile to be parsed again - this happens async
// lets wait a little bit...
time.Sleep(120 * time.Millisecond)
// start editor
handleEditCommand([]string{"edit", args[2]})
}
}
func stripExecSectionFromCommandsFile(commandName string) error {
c, err := ioutil.ReadFile(commandsFilePath)
if err != nil {
return err
}
var (
commandStarted bool
execStarted bool
b bytes.Buffer
offsetCommandNamesAndGlobals int
)
// iterate over contents line by line
for _, line := range strings.Split(string(c), "\n") {
if strings.Contains(line, commandName+":") {
commandStarted = true
}
if offsetCommandNamesAndGlobals == 0 {
if commandStarted {
offsetCommandNamesAndGlobals = countLeadingSpace(line)
}
}
if commandStarted {
// check if next command started, for every line after the command started
if !strings.Contains(line, commandName+":") {
leadingSpace := countLeadingSpace(line)
if leadingSpace == offsetCommandNamesAndGlobals {
commandStarted = false
b.WriteString(line + "\n")
continue
}
}
field := extractYAMLField(line)
if field == "exec" {
execStarted = true
}
// check if a new YAML field started
if execStarted && field != "" && field != "exec" {
execStarted = false
}
if execStarted {
// skip line
continue
}
}
b.WriteString(line + "\n")
}
f, err := os.OpenFile(commandsFilePath, os.O_WRONLY|os.O_TRUNC, 0700)
if err != nil {
return err
}
defer f.Close()
blockWriteEvent()
// update commandsFile
f.WriteString(b.String())
return nil
}