github.com/humaniq/go-ethereum@v1.6.8-0.20171225131628-061223a13848/cmd/geth/consolecmd.go (about) 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 "fmt" 21 "os" 22 "os/signal" 23 "path/filepath" 24 "strings" 25 26 "github.com/ethereum/go-ethereum/cmd/utils" 27 "github.com/ethereum/go-ethereum/console" 28 "github.com/ethereum/go-ethereum/node" 29 "github.com/ethereum/go-ethereum/rpc" 30 "gopkg.in/urfave/cli.v1" 31 ) 32 33 var ( 34 consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag} 35 36 consoleCommand = cli.Command{ 37 Action: utils.MigrateFlags(localConsole), 38 Name: "console", 39 Usage: "Start an interactive JavaScript environment", 40 Flags: append(append(append(nodeFlags, rpcFlags...), consoleFlags...), whisperFlags...), 41 Category: "CONSOLE COMMANDS", 42 Description: ` 43 The Geth console is an interactive shell for the JavaScript runtime environment 44 which exposes a node admin interface as well as the Ðapp JavaScript API. 45 See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console.`, 46 } 47 48 attachCommand = cli.Command{ 49 Action: utils.MigrateFlags(remoteConsole), 50 Name: "attach", 51 Usage: "Start an interactive JavaScript environment (connect to node)", 52 ArgsUsage: "[endpoint]", 53 Flags: append(consoleFlags, utils.DataDirFlag), 54 Category: "CONSOLE COMMANDS", 55 Description: ` 56 The Geth console is an interactive shell for the JavaScript runtime environment 57 which exposes a node admin interface as well as the Ðapp JavaScript API. 58 See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console. 59 This command allows to open a console on a running geth node.`, 60 } 61 62 javascriptCommand = cli.Command{ 63 Action: utils.MigrateFlags(ephemeralConsole), 64 Name: "js", 65 Usage: "Execute the specified JavaScript files", 66 ArgsUsage: "<jsfile> [jsfile...]", 67 Flags: append(nodeFlags, consoleFlags...), 68 Category: "CONSOLE COMMANDS", 69 Description: ` 70 The JavaScript VM exposes a node admin interface as well as the Ðapp 71 JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console`, 72 } 73 ) 74 75 // localConsole starts a new geth node, attaching a JavaScript console to it at the 76 // same time. 77 func localConsole(ctx *cli.Context) error { 78 // Create and start the node based on the CLI flags 79 node := makeFullNode(ctx) 80 startNode(ctx, node) 81 defer node.Stop() 82 83 // Attach to the newly started node and start the JavaScript console 84 client, err := node.Attach() 85 if err != nil { 86 utils.Fatalf("Failed to attach to the inproc geth: %v", err) 87 } 88 config := console.Config{ 89 DataDir: utils.MakeDataDir(ctx), 90 DocRoot: ctx.GlobalString(utils.JSpathFlag.Name), 91 Client: client, 92 Preload: utils.MakeConsolePreloads(ctx), 93 } 94 95 console, err := console.New(config) 96 if err != nil { 97 utils.Fatalf("Failed to start the JavaScript console: %v", err) 98 } 99 defer console.Stop(false) 100 101 // If only a short execution was requested, evaluate and return 102 if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" { 103 console.Evaluate(script) 104 return nil 105 } 106 // Otherwise print the welcome screen and enter interactive mode 107 console.Welcome() 108 console.Interactive() 109 110 return nil 111 } 112 113 // remoteConsole will connect to a remote geth instance, attaching a JavaScript 114 // console to it. 115 func remoteConsole(ctx *cli.Context) error { 116 // Attach to a remotely running geth instance and start the JavaScript console 117 endpoint := ctx.Args().First() 118 if endpoint == "" { 119 path := node.DefaultDataDir() 120 if ctx.GlobalIsSet(utils.DataDirFlag.Name) { 121 path = ctx.GlobalString(utils.DataDirFlag.Name) 122 } 123 if path != "" { 124 if ctx.GlobalBool(utils.TestnetFlag.Name) { 125 path = filepath.Join(path, "testnet") 126 } else if ctx.GlobalBool(utils.RinkebyFlag.Name) { 127 path = filepath.Join(path, "rinkeby") 128 } 129 } 130 endpoint = fmt.Sprintf("%s/geth.ipc", path) 131 } 132 client, err := dialRPC(endpoint) 133 if err != nil { 134 utils.Fatalf("Unable to attach to remote geth: %v", err) 135 } 136 config := console.Config{ 137 DataDir: utils.MakeDataDir(ctx), 138 DocRoot: ctx.GlobalString(utils.JSpathFlag.Name), 139 Client: client, 140 Preload: utils.MakeConsolePreloads(ctx), 141 } 142 143 console, err := console.New(config) 144 if err != nil { 145 utils.Fatalf("Failed to start the JavaScript console: %v", err) 146 } 147 defer console.Stop(false) 148 149 if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" { 150 console.Evaluate(script) 151 return nil 152 } 153 154 // Otherwise print the welcome screen and enter interactive mode 155 console.Welcome() 156 console.Interactive() 157 158 return nil 159 } 160 161 // dialRPC returns a RPC client which connects to the given endpoint. 162 // The check for empty endpoint implements the defaulting logic 163 // for "geth attach" and "geth monitor" with no argument. 164 func dialRPC(endpoint string) (*rpc.Client, error) { 165 if endpoint == "" { 166 endpoint = node.DefaultIPCEndpoint(clientIdentifier) 167 } else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") { 168 // Backwards compatibility with geth < 1.5 which required 169 // these prefixes. 170 endpoint = endpoint[4:] 171 } 172 return rpc.Dial(endpoint) 173 } 174 175 // ephemeralConsole starts a new geth node, attaches an ephemeral JavaScript 176 // console to it, executes each of the files specified as arguments and tears 177 // everything down. 178 func ephemeralConsole(ctx *cli.Context) error { 179 // Create and start the node based on the CLI flags 180 node := makeFullNode(ctx) 181 startNode(ctx, node) 182 defer node.Stop() 183 184 // Attach to the newly started node and start the JavaScript console 185 client, err := node.Attach() 186 if err != nil { 187 utils.Fatalf("Failed to attach to the inproc geth: %v", err) 188 } 189 config := console.Config{ 190 DataDir: utils.MakeDataDir(ctx), 191 DocRoot: ctx.GlobalString(utils.JSpathFlag.Name), 192 Client: client, 193 Preload: utils.MakeConsolePreloads(ctx), 194 } 195 196 console, err := console.New(config) 197 if err != nil { 198 utils.Fatalf("Failed to start the JavaScript console: %v", err) 199 } 200 defer console.Stop(false) 201 202 // Evaluate each of the specified JavaScript files 203 for _, file := range ctx.Args() { 204 if err = console.Execute(file); err != nil { 205 utils.Fatalf("Failed to execute %s: %v", file, err) 206 } 207 } 208 // Wait for pending callbacks, but stop for Ctrl-C. 209 abort := make(chan os.Signal, 1) 210 signal.Notify(abort, os.Interrupt) 211 212 go func() { 213 <-abort 214 os.Exit(0) 215 }() 216 console.Stop(true) 217 218 return nil 219 }