consolecmd.go 5.9 KB

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