-
Notifications
You must be signed in to change notification settings - Fork 6
/
cmdsmux.go
107 lines (102 loc) · 2.31 KB
/
cmdsmux.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
package main
import (
"fmt"
"io"
"strings"
"github.com/chzyer/readline"
)
// interpret interactively interprets commands
func interpreti(rl *readline.Instance) {
for {
line, err := rl.Readline()
if err != nil {
switch err {
case readline.ErrInterrupt:
debug("got interrupted")
continue
case io.EOF:
warn("got an EOF")
default:
debug("unkown signal received: " + err.Error())
}
}
line = strings.TrimSpace(line)
done := interpretl(line)
if done {
return
}
}
}
// interprets interprets a script,
// line by line
func interprets(script string) {
lines := strings.Split(script, "\n")
for _, line := range lines {
done := interpretl(line)
if done {
return
}
}
}
// interpretl interprets a single line,
// used both in interactive and scripting mode.
func interpretl(line string) bool {
switch {
case strings.HasPrefix(line, "cx"):
hcontexts(line)
case strings.HasPrefix(line, "cd"):
hcd(line)
case strings.HasPrefix(line, "curl"):
hcurl(line)
case strings.HasPrefix(line, "echo"):
hecho(line)
case strings.HasPrefix(line, "env"):
henv(line)
case strings.HasPrefix(line, "help"):
husage(line)
case strings.HasPrefix(line, "img"):
himg()
case strings.HasPrefix(line, "kill"):
hkill(line)
case strings.HasPrefix(line, "literally") || strings.HasPrefix(line, "`"):
if strings.HasPrefix(line, "`") {
line = fmt.Sprintf("literally %s", strings.TrimPrefix(line, "`"))
}
hliterally(line)
case strings.HasPrefix(line, "cat"):
hlocalexec(line)
case strings.HasPrefix(line, "ls"):
hlocalexec(line)
case strings.HasPrefix(line, "ns"):
hns(line)
case strings.HasPrefix(line, "plugin"):
hplugins(line)
case strings.HasPrefix(line, "ps"):
hps(line)
case strings.HasPrefix(line, "pwd"):
hlocalexec(line)
case strings.HasPrefix(line, "sleep"):
hsleep(line)
case line == "debug":
switch debugmode {
case true:
debugmode = false
info("DEBUG mode is now off.")
case false:
debugmode = true
info("DEBUG mode is now on.")
}
case line == exitcmd:
return true
case line == "version":
output(version)
case strings.Contains(line, "="):
envar := strings.Split(line, "=")[0]
value := strings.Split(line, "=")[1]
currentenv().evt.set(envar, value)
case line == "" || strings.HasPrefix(line, "#"):
default:
hlaunch(line)
}
return false
}