-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathshell.go
345 lines (278 loc) · 7.31 KB
/
shell.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
/*
* 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"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/dreadl0ck/readline"
)
var (
// ErrUnknownCommand occurs when the command requested is not known to zeus
ErrUnknownCommand = errors.New("unknown command")
// global readline instance
rl *readline.Instance
readlineMutex = &sync.Mutex{}
)
// readline loop for interactive mode
// when there's an unknown command it will be passed to the shell
func readlineLoop(cmdFile *CommandsFile) error {
if conf.fields.PrintBuiltins {
printBuiltins()
}
// print overview
printCommands()
var (
historyFileName string
err error
)
if conf.fields.HistoryFile {
historyFileName = zeusDir + "/.history"
}
conf.Lock()
historyLimit := conf.fields.HistoryLimit
conf.Unlock()
readlineMutex.Lock()
// prepare readline
rl, err = readline.NewEx(&readline.Config{
Prompt: printPrompt(),
AutoComplete: completer,
HistoryLimit: historyLimit,
HistoryFile: historyFileName,
Listener: listener,
InterruptPrompt: "\nBye." + cp.Reset,
})
readlineMutex.Unlock()
if err != nil {
return err
}
defer rl.Close()
for {
// read a line
line, err := rl.Readline()
if err != nil {
if err == io.EOF {
return nil
}
if err == readline.ErrInterrupt {
if conf.fields.ExitOnInterrupt {
cleanup(cmdFile)
os.Exit(0)
} else {
Log.Info("ExitOnInterrupt is disabled, type 'exit' if you want to leave.")
continue
}
}
return fmt.Errorf("readline error: %v", err)
}
handleLine(line)
}
}
// handle input line read by the readline instance
func handleLine(line string) {
// trim
line = strings.TrimSpace(line)
// set the color
print(cp.CmdOutput)
switch line {
case exitCommand:
l.Println(cp.Text + "Bye." + cp.Reset)
clearProcessMap()
os.Exit(0)
case helpCommand:
clearScreen()
l.Println(cp.Text + asciiArt + "v" + version)
conf.Lock()
if conf.fields.Debug {
l.Println(cp.Text + "Project Name: " + cp.Prompt + filepath.Base(workingDir) + cp.Text + "\n")
}
if conf.fields.PrintBuiltins {
printBuiltins()
}
conf.Unlock()
printCommands()
case infoCommand:
printProjectInfo()
case formatCommand:
f.formatCommand()
case "zeus": // prevent spawning a new interactive shell
case globalsCommand:
listGlobals()
case configCommand:
conf.dump()
case wikiCommand:
go StartWebListener(false)
open("http://" + hostName + ":" + strconv.Itoa(conf.fields.PortWebPanel) + "/wiki")
case webCommand:
go StartWebListener(true)
case dataCommand:
printProjectData()
case updateCommand:
updateZeus()
case versionCommand:
l.Println(version)
case clearCommand:
clearScreen()
l.Println(cp.Text + asciiArt + "v" + version)
l.Println(cp.Text + "Project Name: " + cp.Prompt + filepath.Base(workingDir) + cp.Text + "\n")
case builtinsCommand:
printBuiltins()
default:
// split the input line
args := strings.Fields(line)
// skip if empty
if len(args) == 0 {
return
}
// get the command name
commandName := args[0]
switch commandName {
case makefileCommand:
handleMakefileCommand(args)
case configCommand:
handleConfigCommand(args)
case eventsCommand:
handleEventsCommand(args)
case aliasCommand:
handleAliasCommand(args)
case editCommand:
handleEditCommand(args)
// wait a little for the commandsFile watcher to kick in and handle the write event
time.Sleep(100 * time.Millisecond)
// check if the commandsFile became invalid due to an edit while the current command was running.
if lastCommandsFileError != nil {
Log.WithError(lastCommandsFileError).Error("invalid commandsFile")
lastCommandsFileError = nil
}
case deadlineCommand:
handleDeadlineCommand(args)
case gitFilterCommand:
handleGitFilterCommand(args)
case milestonesCommand:
handleMilestonesCommand(args)
case procsCommand:
handleProcsCommand(args)
case helpCommand:
handleHelpCommand(args)
case colorsCommand:
handleColorsCommand(args)
case authorCommand:
handleAuthorCommand(args)
case keysCommand:
handleKeysCommand(args)
case createCommand:
handleCreateCommand(args)
printProjectHeader()
printCommands()
case todoCommand:
handleTodoCommand(args)
case generateCommand:
handleGenerateCommand(args)
default:
// check if its a commandChain
if strings.Contains(line, commandChainSeparator) {
fields := strings.Split(line, commandChainSeparator)
if cmdChain, ok := validCommandChain(fields, false); ok {
shellBusy = true
cmdChain.exec(fields)
shellBusy = false
} else {
l.Println("invalid commandChain")
}
return
}
// remove the command name from the slice
args = args[1:]
cmdMap.Lock()
// try to find the command in the commands map
cmd, ok := cmdMap.items[commandName]
if !ok {
cmdMap.Unlock()
projectData.Lock()
// check if its an alias
if command, ok := projectData.fields.Aliases[commandName]; ok {
projectData.Unlock()
handleLine(command)
s.reset()
return
}
projectData.Unlock()
// not an alias - pass to shell
if conf.fields.PassCommandsToShell {
err := passCommandToShell(commandName, args)
if err != nil {
l.Println(err)
}
} else {
l.Println(ErrUnknownCommand, ": ", commandName)
}
return
}
cmdMap.Unlock()
defer s.reset()
count, err := getTotalDependencyCount(cmd)
if err != nil {
l.Println(err)
return
}
s.Lock()
s.numCommands = s.numCommands + count
s.Unlock()
// run the command
shellBusy = true
err = cmd.Run(args, cmd.async)
if err != nil {
if err.Error() == "signal: interrupt" {
fmt.Println(" " + err.Error())
// check if the commandsFile became invalid due to an edit while the current command was running.
if lastCommandsFileError != nil {
Log.WithError(lastCommandsFileError).Error("invalid commandsFile")
lastCommandsFileError = nil
}
moveBack()
return
}
fmt.Printf("command "+cmd.name+" failed. error: %v\n", err)
}
shellBusy = false
// check if the commandsFile became invalid due to an edit while the current command was running.
if lastCommandsFileError != nil {
Log.WithError(lastCommandsFileError).Error("invalid commandsFile")
lastCommandsFileError = nil
}
moveBack()
if cmd.async {
time.Sleep(100 * time.Millisecond)
}
}
}
}
func moveBack() {
Log.Debug("moving back to: ", workingDir)
err := os.Chdir(workingDir)
if err != nil {
log.Fatal(err)
}
}