-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
175 lines (138 loc) · 3.61 KB
/
main.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
//go:build !js
// +build !js
//go:generate go build -v golang.org/x/tools/cmd/stringer
package main
import (
"flag"
"os"
"strings"
"time"
_ "github.com/lmorg/murex/builtins"
"github.com/lmorg/murex/builtins/pipes/term"
"github.com/lmorg/murex/config/defaults"
"github.com/lmorg/murex/config/profile"
"github.com/lmorg/murex/debug"
"github.com/lmorg/murex/lang"
"github.com/lmorg/murex/lang/ref"
"github.com/lmorg/murex/shell"
"github.com/lmorg/murex/utils/cache"
"github.com/lmorg/murex/utils/escape"
"github.com/lmorg/murex/utils/readline"
)
func main() {
readFlags()
switch {
case fRunTests:
runTests()
case fCommand != "":
runCommandString(fCommand)
case fExecute:
runCommandString(argvToCmdLineStr(flag.Args()))
case len(fSource) > 0:
runSource(fSource[0])
default:
startMurexRepl()
}
debug.Log("[FIN]")
}
func runTests() error {
lang.InitEnv()
defaults.Config(lang.ShellProcess.Config, fInteractive)
registerSignalHandlers(fInteractive)
// compiled profile
defaultProfile()
// enable tests
if err := lang.ShellProcess.Config.Set("test", "enabled", true, nil); err != nil {
return err
}
if err := lang.ShellProcess.Config.Set("test", "auto-report", false, nil); err != nil {
return err
}
if err := lang.ShellProcess.Config.Set("test", "verbose", false, nil); err != nil {
return err
}
tty := readline.IsTerminal(int(os.Stdout.Fd()))
if err := lang.ShellProcess.Config.Set("shell", "color", tty, nil); err != nil {
return err
}
// run unit tests
passed := lang.GlobalUnitTests.Run(lang.ShellProcess, "*")
lang.ShellProcess.Tests.WriteResults(lang.ShellProcess.Config, lang.ShellProcess.Stdout)
if !passed {
lang.Exit(1)
}
return nil
}
func argvToCmdLineStr(argv []string) string {
cmdLine := make([]string, len(argv))
copy(cmdLine, argv)
escape.CommandLine(cmdLine)
return strings.Join(cmdLine, " ")
}
func runCommandString(commandString string) {
lang.InitEnv()
// default config
defaults.Config(lang.ShellProcess.Config, fInteractive)
registerSignalHandlers(fInteractive)
// compiled profile
defaultProfile()
// load modules and profile
if fLoadMods {
profile.Execute(profile.F_PRELOAD | profile.F_MODULES | profile.F_PROFILE)
}
// read block from command line parameters
term.OutSetDataTypeIPC()
sourceRef := ref.Source{
DateTime: time.Now(),
Filename: "",
Module: "murex/-c",
}
execSource([]rune(commandString), &sourceRef, true)
if fInteractive {
shell.Start()
}
}
func runSource(filename string) {
lang.InitEnv()
// default config
defaults.Config(lang.ShellProcess.Config, fInteractive)
registerSignalHandlers(fInteractive)
// compiled profile
defaultProfile()
// load modules a profile
if fLoadMods {
profile.Execute(profile.F_PRELOAD | profile.F_MODULES | profile.F_PROFILE)
}
// read block from disk
term.OutSetDataTypeIPC()
disk, err := diskSource(filename)
if err != nil {
_, err := os.Stderr.WriteString(err.Error() + "\n")
if err != nil {
// wouldn't really make any difference at this point because we
// cannot write to stderr anyway :(
panic(err)
}
lang.Exit(1)
}
sourceRef := ref.Source{
DateTime: time.Now(),
Filename: filename,
Module: "murex/#!",
}
execSource([]rune(string(disk)), &sourceRef, true)
}
func startMurexRepl() {
lang.InitEnv()
// default config
defaults.Config(lang.ShellProcess.Config, true)
cache.SetPath(profile.ModulePath() + "cache.db")
cache.InitCache()
// compiled profile
defaultProfile()
// load modules and profile
profile.Execute(profile.F_PRELOAD | profile.F_MODULES | profile.F_PROFILE)
// start interactive shell
registerSignalHandlers(true)
shell.Start()
}