consolecmd.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. "os"
  19. "os/signal"
  20. "strings"
  21. "github.com/ethereum/go-ethereum/cmd/utils"
  22. "github.com/ethereum/go-ethereum/console"
  23. "github.com/ethereum/go-ethereum/node"
  24. "github.com/ethereum/go-ethereum/rpc"
  25. "gopkg.in/urfave/cli.v1"
  26. )
  27. var (
  28. consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag}
  29. )
  30. var (
  31. consoleCommand = cli.Command{
  32. Action: utils.MigrateFlags(localConsole),
  33. Name: "console",
  34. Usage: "Start an interactive JavaScript environment",
  35. Flags: append(append(nodeFlags, rpcFlags...), consoleFlags...),
  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. client, err := dialRPC(ctx.Args().First())
  105. if err != nil {
  106. utils.Fatalf("Unable to attach to remote geth: %v", err)
  107. }
  108. config := console.Config{
  109. DataDir: utils.MakeDataDir(ctx),
  110. DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
  111. Client: client,
  112. Preload: utils.MakeConsolePreloads(ctx),
  113. }
  114. console, err := console.New(config)
  115. if err != nil {
  116. utils.Fatalf("Failed to start the JavaScript console: %v", err)
  117. }
  118. defer console.Stop(false)
  119. if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" {
  120. console.Evaluate(script)
  121. return nil
  122. }
  123. // Otherwise print the welcome screen and enter interactive mode
  124. console.Welcome()
  125. console.Interactive()
  126. return nil
  127. }
  128. // dialRPC returns a RPC client which connects to the given endpoint.
  129. // The check for empty endpoint implements the defaulting logic
  130. // for "geth attach" and "geth monitor" with no argument.
  131. func dialRPC(endpoint string) (*rpc.Client, error) {
  132. if endpoint == "" {
  133. endpoint = node.DefaultIPCEndpoint(clientIdentifier)
  134. } else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
  135. // Backwards compatibility with geth < 1.5 which required
  136. // these prefixes.
  137. endpoint = endpoint[4:]
  138. }
  139. return rpc.Dial(endpoint)
  140. }
  141. // ephemeralConsole starts a new geth node, attaches an ephemeral JavaScript
  142. // console to it, executes each of the files specified as arguments and tears
  143. // everything down.
  144. func ephemeralConsole(ctx *cli.Context) error {
  145. // Create and start the node based on the CLI flags
  146. node := makeFullNode(ctx)
  147. startNode(ctx, node)
  148. defer node.Stop()
  149. // Attach to the newly started node and start the JavaScript console
  150. client, err := node.Attach()
  151. if err != nil {
  152. utils.Fatalf("Failed to attach to the inproc geth: %v", err)
  153. }
  154. config := console.Config{
  155. DataDir: utils.MakeDataDir(ctx),
  156. DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
  157. Client: client,
  158. Preload: utils.MakeConsolePreloads(ctx),
  159. }
  160. console, err := console.New(config)
  161. if err != nil {
  162. utils.Fatalf("Failed to start the JavaScript console: %v", err)
  163. }
  164. defer console.Stop(false)
  165. // Evaluate each of the specified JavaScript files
  166. for _, file := range ctx.Args() {
  167. if err = console.Execute(file); err != nil {
  168. utils.Fatalf("Failed to execute %s: %v", file, err)
  169. }
  170. }
  171. // Wait for pending callbacks, but stop for Ctrl-C.
  172. abort := make(chan os.Signal, 1)
  173. signal.Notify(abort, os.Interrupt)
  174. go func() {
  175. <-abort
  176. os.Exit(0)
  177. }()
  178. console.Stop(true)
  179. return nil
  180. }