consolecmd.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. consoleCommand = cli.Command{
  29. Action: localConsole,
  30. Name: "console",
  31. Usage: "Start an interactive JavaScript environment",
  32. ArgsUsage: "", // TODO: Write this!
  33. Category: "CONSOLE COMMANDS",
  34. Description: `
  35. The Geth console is an interactive shell for the JavaScript runtime environment
  36. which exposes a node admin interface as well as the Ðapp JavaScript API.
  37. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console
  38. `,
  39. }
  40. attachCommand = cli.Command{
  41. Action: remoteConsole,
  42. Name: "attach",
  43. Usage: "Start an interactive JavaScript environment (connect to node)",
  44. ArgsUsage: "", // TODO: Write this!
  45. Category: "CONSOLE COMMANDS",
  46. Description: `
  47. The Geth console is an interactive shell for the JavaScript runtime environment
  48. which exposes a node admin interface as well as the Ðapp JavaScript API.
  49. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console.
  50. This command allows to open a console on a running geth node.
  51. `,
  52. }
  53. javascriptCommand = cli.Command{
  54. Action: ephemeralConsole,
  55. Name: "js",
  56. Usage: "Execute the specified JavaScript files",
  57. ArgsUsage: "", // TODO: Write this!
  58. Category: "CONSOLE COMMANDS",
  59. Description: `
  60. The JavaScript VM exposes a node admin interface as well as the Ðapp
  61. JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console
  62. `,
  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: node.DataDir(),
  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 only a short execution was requested, evaluate and return
  118. if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" {
  119. console.Evaluate(script)
  120. return nil
  121. }
  122. // Otherwise print the welcome screen and enter interactive mode
  123. console.Welcome()
  124. console.Interactive()
  125. return nil
  126. }
  127. // dialRPC returns a RPC client which connects to the given endpoint.
  128. // The check for empty endpoint implements the defaulting logic
  129. // for "geth attach" and "geth monitor" with no argument.
  130. func dialRPC(endpoint string) (*rpc.Client, error) {
  131. if endpoint == "" {
  132. endpoint = node.DefaultIPCEndpoint(clientIdentifier)
  133. } else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
  134. // Backwards compatibility with geth < 1.5 which required
  135. // these prefixes.
  136. endpoint = endpoint[4:]
  137. }
  138. return rpc.Dial(endpoint)
  139. }
  140. // ephemeralConsole starts a new geth node, attaches an ephemeral JavaScript
  141. // console to it, and each of the files specified as arguments and tears the
  142. // everything down.
  143. func ephemeralConsole(ctx *cli.Context) error {
  144. // Create and start the node based on the CLI flags
  145. node := makeFullNode(ctx)
  146. startNode(ctx, node)
  147. defer node.Stop()
  148. // Attach to the newly started node and start the JavaScript console
  149. client, err := node.Attach()
  150. if err != nil {
  151. utils.Fatalf("Failed to attach to the inproc geth: %v", err)
  152. }
  153. config := console.Config{
  154. DataDir: node.DataDir(),
  155. DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
  156. Client: client,
  157. Preload: utils.MakeConsolePreloads(ctx),
  158. }
  159. console, err := console.New(config)
  160. if err != nil {
  161. utils.Fatalf("Failed to start the JavaScript console: %v", err)
  162. }
  163. defer console.Stop(false)
  164. // Evaluate each of the specified JavaScript files
  165. for _, file := range ctx.Args() {
  166. if err = console.Execute(file); err != nil {
  167. utils.Fatalf("Failed to execute %s: %v", file, err)
  168. }
  169. }
  170. // Wait for pending callbacks, but stop for Ctrl-C.
  171. abort := make(chan os.Signal, 1)
  172. signal.Notify(abort, os.Interrupt)
  173. go func() {
  174. <-abort
  175. os.Exit(0)
  176. }()
  177. console.Stop(true)
  178. return nil
  179. }