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