consolecmd.go 6.3 KB

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