/
console.go
59 lines (48 loc) · 1.25 KB
/
console.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
package main
import (
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/rpc"
"github.com/blockservice/echoin/console"
)
var (
attachCmd = &cobra.Command{
RunE: remoteConsole,
Use: "attach",
Short: "Start an interactive JavaScript environment (connect to node)",
}
)
func remoteConsole(cmd *cobra.Command, args []string) error {
if len(args) != 1 || len(args[0]) == 0 {
return errors.New("missing url")
}
client, err := dialRPC(args[0])
if err != nil {
utils.Fatalf("Unable to attach to remote node: %v", err)
}
config := console.Config{
DataDir: "",
DocRoot: "",
Client: client,
Preload: []string{},
}
console, err := console.New(config)
if err != nil {
utils.Fatalf("Failed to start the JavaScript console: %v", err)
}
defer console.Stop(false)
// Otherwise print the welcome screen and enter interactive mode
console.Welcome()
console.Interactive()
return nil
}
func dialRPC(endpoint string) (*rpc.Client, error) {
if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
// Backwards compatibility with geth < 1.5 which required
// these prefixes.
endpoint = endpoint[4:]
}
return rpc.Dial(endpoint)
}