|
| 1 | +// Copyright 2016 The go-ethereum Authors |
| 2 | +// This file is part of go-ethereum. |
| 3 | +// |
| 4 | +// go-ethereum is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// go-ethereum is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "os/signal" |
| 22 | + |
| 23 | + "github.com/codegangsta/cli" |
| 24 | + "github.com/ethereum/go-ethereum/cmd/utils" |
| 25 | + "github.com/ethereum/go-ethereum/console" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + consoleCommand = cli.Command{ |
| 30 | + Action: localConsole, |
| 31 | + Name: "console", |
| 32 | + Usage: `Geth Console: interactive JavaScript environment`, |
| 33 | + Description: ` |
| 34 | +The Geth console is an interactive shell for the JavaScript runtime environment |
| 35 | +which exposes a node admin interface as well as the Ðapp JavaScript API. |
| 36 | +See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console |
| 37 | +`, |
| 38 | + } |
| 39 | + attachCommand = cli.Command{ |
| 40 | + Action: remoteConsole, |
| 41 | + Name: "attach", |
| 42 | + Usage: `Geth Console: interactive JavaScript environment (connect to node)`, |
| 43 | + Description: ` |
| 44 | +The Geth console is an interactive shell for the JavaScript runtime environment |
| 45 | +which exposes a node admin interface as well as the Ðapp JavaScript API. |
| 46 | +See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console. |
| 47 | +This command allows to open a console on a running geth node. |
| 48 | + `, |
| 49 | + } |
| 50 | + javascriptCommand = cli.Command{ |
| 51 | + Action: ephemeralConsole, |
| 52 | + Name: "js", |
| 53 | + Usage: `executes the given JavaScript files in the Geth JavaScript VM`, |
| 54 | + Description: ` |
| 55 | +The JavaScript VM exposes a node admin interface as well as the Ðapp |
| 56 | +JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console |
| 57 | +`, |
| 58 | + } |
| 59 | +) |
| 60 | + |
| 61 | +// localConsole starts a new geth node, attaching a JavaScript console to it at the |
| 62 | +// same time. |
| 63 | +func localConsole(ctx *cli.Context) { |
| 64 | + // Create and start the node based on the CLI flags |
| 65 | + node := utils.MakeSystemNode(clientIdentifier, verString, relConfig, makeDefaultExtra(), ctx) |
| 66 | + startNode(ctx, node) |
| 67 | + defer node.Stop() |
| 68 | + |
| 69 | + // Attach to the newly started node and start the JavaScript console |
| 70 | + client, err := node.Attach() |
| 71 | + if err != nil { |
| 72 | + utils.Fatalf("Failed to attach to the inproc geth: %v", err) |
| 73 | + } |
| 74 | + config := console.Config{ |
| 75 | + DataDir: node.DataDir(), |
| 76 | + DocRoot: ctx.GlobalString(utils.JSpathFlag.Name), |
| 77 | + Client: client, |
| 78 | + Preload: utils.MakeConsolePreloads(ctx), |
| 79 | + } |
| 80 | + console, err := console.New(config) |
| 81 | + if err != nil { |
| 82 | + utils.Fatalf("Failed to start the JavaScript console: %v", err) |
| 83 | + } |
| 84 | + defer console.Stop(false) |
| 85 | + |
| 86 | + // If only a short execution was requested, evaluate and return |
| 87 | + if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" { |
| 88 | + console.Evaluate(script) |
| 89 | + return |
| 90 | + } |
| 91 | + // Otherwise print the welcome screen and enter interactive mode |
| 92 | + console.Welcome() |
| 93 | + console.Interactive() |
| 94 | +} |
| 95 | + |
| 96 | +// remoteConsole will connect to a remote geth instance, attaching a JavaScript |
| 97 | +// console to it. |
| 98 | +func remoteConsole(ctx *cli.Context) { |
| 99 | + // Attach to a remotely running geth instance and start the JavaScript console |
| 100 | + client, err := utils.NewRemoteRPCClient(ctx) |
| 101 | + if err != nil { |
| 102 | + utils.Fatalf("Unable to attach to remote geth: %v", err) |
| 103 | + } |
| 104 | + config := console.Config{ |
| 105 | + DataDir: utils.MustMakeDataDir(ctx), |
| 106 | + DocRoot: ctx.GlobalString(utils.JSpathFlag.Name), |
| 107 | + Client: client, |
| 108 | + Preload: utils.MakeConsolePreloads(ctx), |
| 109 | + } |
| 110 | + console, err := console.New(config) |
| 111 | + if err != nil { |
| 112 | + utils.Fatalf("Failed to start the JavaScript console: %v", err) |
| 113 | + } |
| 114 | + defer console.Stop(false) |
| 115 | + |
| 116 | + // If only a short execution was requested, evaluate and return |
| 117 | + if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" { |
| 118 | + console.Evaluate(script) |
| 119 | + return |
| 120 | + } |
| 121 | + // Otherwise print the welcome screen and enter interactive mode |
| 122 | + console.Welcome() |
| 123 | + console.Interactive() |
| 124 | +} |
| 125 | + |
| 126 | +// ephemeralConsole starts a new geth node, attaches an ephemeral JavaScript |
| 127 | +// console to it, and each of the files specified as arguments and tears the |
| 128 | +// everything down. |
| 129 | +func ephemeralConsole(ctx *cli.Context) { |
| 130 | + // Create and start the node based on the CLI flags |
| 131 | + node := utils.MakeSystemNode(clientIdentifier, verString, relConfig, makeDefaultExtra(), ctx) |
| 132 | + startNode(ctx, node) |
| 133 | + defer node.Stop() |
| 134 | + |
| 135 | + // Attach to the newly started node and start the JavaScript console |
| 136 | + client, err := node.Attach() |
| 137 | + if err != nil { |
| 138 | + utils.Fatalf("Failed to attach to the inproc geth: %v", err) |
| 139 | + } |
| 140 | + config := console.Config{ |
| 141 | + DataDir: node.DataDir(), |
| 142 | + DocRoot: ctx.GlobalString(utils.JSpathFlag.Name), |
| 143 | + Client: client, |
| 144 | + Preload: utils.MakeConsolePreloads(ctx), |
| 145 | + } |
| 146 | + console, err := console.New(config) |
| 147 | + if err != nil { |
| 148 | + utils.Fatalf("Failed to start the JavaScript console: %v", err) |
| 149 | + } |
| 150 | + defer console.Stop(false) |
| 151 | + |
| 152 | + // Evaluate each of the specified JavaScript files |
| 153 | + for _, file := range ctx.Args() { |
| 154 | + if err = console.Execute(file); err != nil { |
| 155 | + utils.Fatalf("Failed to execute %s: %v", file, err) |
| 156 | + } |
| 157 | + } |
| 158 | + // Wait for pending callbacks, but stop for Ctrl-C. |
| 159 | + abort := make(chan os.Signal, 1) |
| 160 | + signal.Notify(abort, os.Interrupt) |
| 161 | + |
| 162 | + go func() { |
| 163 | + <-abort |
| 164 | + os.Exit(0) |
| 165 | + }() |
| 166 | + console.Stop(true) |
| 167 | +} |
0 commit comments