forked from lmorg/murex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (108 loc) · 2.8 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
//go:build !js
// +build !js
package main
import (
"os"
_ "github.com/lmorg/murex/builtins"
"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/shell"
"github.com/lmorg/murex/utils/readline"
)
const (
interactive bool = true
nonInteractive bool = false
)
func main() {
readFlags()
switch {
case fRunTests:
runTests()
case fCommand != "":
runCommandLine(fCommand)
case len(fSource) > 0:
runSource(fSource[0])
default:
startMurex()
}
debug.Log("[FIN]")
}
func runTests() error {
lang.InitEnv()
defaults.Defaults(lang.ShellProcess.Config, nonInteractive)
shell.SignalHandler(nonInteractive)
// 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 {
os.Exit(1)
}
return nil
}
func runCommandLine(commandLine string) {
lang.InitEnv()
// default config
defaults.Defaults(lang.ShellProcess.Config, nonInteractive)
shell.SignalHandler(nonInteractive)
// 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
execSource([]rune(commandLine), nil)
}
func runSource(filename string) {
lang.InitEnv()
// default config
defaults.Defaults(lang.ShellProcess.Config, nonInteractive)
shell.SignalHandler(nonInteractive)
// compiled profile
defaultProfile()
// load modules a profile
if fLoadMods {
profile.Execute(profile.F_PRELOAD | profile.F_MODULES | profile.F_PROFILE)
}
// read block from disk
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)
}
os.Exit(1)
}
execSource([]rune(string(disk)), nil)
}
func startMurex() {
lang.InitEnv()
// default config
defaults.Defaults(lang.ShellProcess.Config, interactive)
// compiled profile
defaultProfile()
// load modules and profile
profile.Execute(profile.F_PRELOAD | profile.F_MODULES | profile.F_PROFILE)
// start interactive shell
shell.Start()
}