-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
136 lines (115 loc) · 2.92 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
package main
import (
"context"
"embed"
"os"
"reflect"
"time"
"github.com/charmbracelet/log"
_ "github.com/mattn/go-sqlite3"
"github.com/risor-io/risor"
"github.com/risor-io/risor/errz"
ros "github.com/risor-io/risor/os"
)
//go:embed lib/*.risor
var _rsrLib embed.FS
//go:embed lib/rsx.risor
var _rsxLib string
//go:embed lib/pool.risor
var _rsxPool string
//go:embed main.risor
var _mainRsr string
//go:embed main.go
var _mainGo string
//go:embed generator.go
var _generatorGo string
//go:embed importer.go
var _importerGo string
//go:embed repl.go
var _replGo string
//go:embed version.go
var _versionGo string
//go:embed go.mod
var _goMod string
//go:embed go.sum
var _goSum string
//go:embed .modules
var _rsModules string
func main() {
logger := log.NewWithOptions(os.Stderr, log.Options{
ReportCaller: false,
ReportTimestamp: false,
TimeFormat: time.Kitchen,
Prefix: "",
})
if os.Getenv("RSX_DEBUG") != "" {
logger.SetLevel(log.DebugLevel)
}
ctx := context.Background()
opts := []risor.Option{
risor.WithConcurrency(),
risor.WithListenersAllowed(),
risor.WithLocalImporter("lib"),
risor.WithGlobal("_generatorGo", _generatorGo),
risor.WithGlobal("_goMod", _goMod),
risor.WithGlobal("_goSum", _goSum),
risor.WithGlobal("_generatorGo", _generatorGo),
risor.WithGlobal("_importerGo", _importerGo),
risor.WithGlobal("_mainGo", _mainGo),
risor.WithGlobal("_replGo", _replGo),
risor.WithGlobal("_rsModules", _rsModules),
risor.WithGlobal("_rsxLib", _rsxLib),
risor.WithGlobal("_rsxVersion", Version),
risor.WithGlobal("_rsxPool", _rsxPool),
risor.WithGlobal("_versionGo", _versionGo),
risor.WithGlobal("pool", _rsxPool),
risor.WithGlobal("rsx", _rsxLib),
}
for k, v := range globalModules() {
if reflect.ValueOf(v).IsNil() {
logger.Warnf("Missing module %s, missing build tag?", k)
continue
}
opts = append(opts, risor.WithGlobal(k, v))
}
opts = append(opts, risor.WithImporter(newEmbedImporter()))
if len(os.Args) > 1 && os.Args[1] == "run" {
m, err := os.ReadFile("main.risor")
if err != nil {
logger.Fatal("error reading main.risor")
}
_mainRsr = string(m)
if len(os.Args) > 2 {
ros.SetScriptArgs(append([]string{"main.risor"}, os.Args[2:]...))
}
} else if len(os.Args) > 1 && os.Args[1] == "eval" {
if len(os.Args) < 3 {
logger.Fatal("missing script argument")
}
script := os.Args[2]
m, err := os.ReadFile(script)
if err != nil {
logger.Fatal("error reading", script)
}
_mainRsr = string(m)
if len(os.Args) > 2 {
ros.SetScriptArgs(append([]string{script}, os.Args[3:]...))
}
} else if len(os.Args) > 1 && os.Args[1] == "repl" {
Repl(ctx, opts)
} else {
ros.SetScriptArgs(os.Args)
}
_, err := risor.Eval(
ctx,
_mainRsr,
opts...,
)
if err != nil {
errMsg := err.Error()
if friendlyErr, ok := err.(errz.FriendlyError); ok {
errMsg = friendlyErr.FriendlyErrorMessage()
}
logger.Fatal(errMsg)
}
}