consolecmd.go 6.7 KB

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