forked from eosspark/geos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console_plugin.go
167 lines (151 loc) · 4.35 KB
/
console_plugin.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
package console_plugin
import (
"github.com/urfave/cli"
"github.com/zhangsifeng92/geos/common"
. "github.com/zhangsifeng92/geos/exception/try"
"github.com/zhangsifeng92/geos/libraries/asio"
. "github.com/zhangsifeng92/geos/plugins/appbase/app"
"github.com/zhangsifeng92/geos/plugins/console_plugin/console"
"strings"
)
const ConsolePlug = PluginTypeName("ConsolePlugin")
var consolePlugin Plugin = App().RegisterPlugin(ConsolePlug, NewConsolePlugin(App().GetIoService()))
type ConsolePlugin struct {
AbstractPlugin
my *ConsolePluginImpl
}
func NewConsolePlugin(io *asio.IoContext) *ConsolePlugin {
p := new(ConsolePlugin)
p.my = NewConsolePluginImpl(io)
p.my.Self = p
return p
}
func (cp *ConsolePlugin) SetProgramOptions(options *[]cli.Flag) {
*options = append(*options,
cli.BoolFlag{
Name: "console",
Usage: "Start an interactive JavaScript environment",
},
cli.StringFlag{
Name: "attach",
Usage: "Start an interactive JavaScript environment (connect to node)",
Value: common.HttpEndPoint,
},
cli.StringFlag{ // ATM the url is left to the user and deployment to
Name: "jspath",
Usage: "JavaScript root path for `loadScript`",
Value: ".",
},
cli.StringFlag{
Name: "console-datadir",
Usage: "Data directory for the databases and keystore",
Value: "./console_history",
},
cli.StringFlag{
Name: "exec",
Usage: "Execute JavaScript statement",
},
cli.StringFlag{
Name: "preload",
Usage: "Comma separated list of JavaScript files to preload into the console",
},
//// RPC settings
//cli.BoolFlag{
// Name: "rpc",
// Usage: "Enable the HTTP-RPC server",
//},
//cli.StringFlag{
// Name: "rpcaddr",
// Usage: "HTTP-RPC server listening interface",
// //Value: node.DefaultHTTPHost,
//},
//cli.IntFlag{
// Name: "rpcport",
// Usage: "HTTP-RPC server listening port",
// //Value: node.DefaultHTTPPort,
//},
//cli.StringFlag{
// Name: "rpccorsdomain",
// Usage: "Comma separated list of domains from which to accept cross origin requests (browser enforced)",
// Value: "",
//},
//cli.StringFlag{
// Name: "rpcvhosts",
// Usage: "Comma separated list of virtual hostnames from which to accept requests (server enforced). Accepts '*' wildcard.",
// //Value: strings.Join(node.DefaultConfig.HTTPVirtualHosts, ","),
//},
//cli.StringFlag{
// Name: "rpcapi",
// Usage: "API's offered over the HTTP-RPC interface",
// Value: "",
//},
//cli.BoolFlag{
// Name: "ipcdisable",
// Usage: "Disable the IPC-RPC server",
//},
//DirectoryFlag{//IPCPathFlag =
// Name: "ipcpath",
// Usage: "Filename for IPC socket/pipe within the datadir (explicit paths escape it)",
//},
//cli.BoolFlag{
// Name: "ws",
// Usage: "Enable the WS-RPC server",
//},
//cli.StringFlag{
// Name: "wsaddr",
// Usage: "WS-RPC server listening interface",
// //Value: node.DefaultWSHost,
//},
//cli.IntFlag{
// Name: "wsport",
// Usage: "WS-RPC server listening port",
// //Value: node.DefaultWSPort,
//},
//cli.StringFlag{
// Name: "wsapi",
// Usage: "API's offered over the WS-RPC interface",
// Value: "",
//},
//cli.StringFlag{
// Name: "wsorigins",
// Usage: "Origins from which to accept websockets requests",
// Value: "",
//}
)
}
func (cp *ConsolePlugin) PluginInitialize(c *cli.Context) {
Try(func() {
cp.my.datadir = c.String("console-datadir")
cp.my.exec = c.String("exec")
cp.my.jsPath = c.String("jspath")
preloadJSString := c.String("preload")
// MakeConsolePreloads retrieves the absolute paths for the console JavaScript
// scripts to preload before starting.
if preloadJSString != "" {
for _, file := range strings.Split(preloadJSString, ",") {
cp.my.preload = append(cp.my.preload, common.AbsolutePath(cp.my.jsPath, strings.TrimSpace(file)))
}
} else {
cp.my.preload = nil
}
cp.my.baseUrl = c.String("attach")
console.BaseUrl = cp.my.baseUrl
cp.my.enable = c.Bool("console")
if cp.my.enable {
err := cp.my.localConsole()
if err != nil {
FcThrow("Failed to start the JavaScript console : %s", err)
}
}
}).FcLogAndRethrow().End()
}
func (cp *ConsolePlugin) PluginStartup() {
if cp.my.enable {
// Otherwise print the welcome screen and enter interactive mode
cp.my.console.Welcome()
cp.my.console.Interactive()
}
}
func (cp *ConsolePlugin) PluginShutdown() {
cp.my.console.Stop(false)
}