consolecmd.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 != "" && ctx.GlobalBool(utils.TestnetFlag.Name) {
  111. path = filepath.Join(path, "testnet")
  112. }
  113. endpoint = fmt.Sprintf("%s/geth.ipc", path)
  114. }
  115. client, err := dialRPC(endpoint)
  116. if err != nil {
  117. utils.Fatalf("Unable to attach to remote geth: %v", err)
  118. }
  119. config := console.Config{
  120. DataDir: utils.MakeDataDir(ctx),
  121. DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
  122. Client: client,
  123. Preload: utils.MakeConsolePreloads(ctx),
  124. }
  125. console, err := console.New(config)
  126. if err != nil {
  127. utils.Fatalf("Failed to start the JavaScript console: %v", err)
  128. }
  129. defer console.Stop(false)
  130. if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" {
  131. console.Evaluate(script)
  132. return nil
  133. }
  134. // Otherwise print the welcome screen and enter interactive mode
  135. console.Welcome()
  136. console.Interactive()
  137. return nil
  138. }
  139. // dialRPC returns a RPC client which connects to the given endpoint.
  140. // The check for empty endpoint implements the defaulting logic
  141. // for "geth attach" and "geth monitor" with no argument.
  142. func dialRPC(endpoint string) (*rpc.Client, error) {
  143. if endpoint == "" {
  144. endpoint = node.DefaultIPCEndpoint(clientIdentifier)
  145. } else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
  146. // Backwards compatibility with geth < 1.5 which required
  147. // these prefixes.
  148. endpoint = endpoint[4:]
  149. }
  150. return rpc.Dial(endpoint)
  151. }
  152. // ephemeralConsole starts a new geth node, attaches an ephemeral JavaScript
  153. // console to it, executes each of the files specified as arguments and tears
  154. // everything down.
  155. func ephemeralConsole(ctx *cli.Context) error {
  156. // Create and start the node based on the CLI flags
  157. node := makeFullNode(ctx)
  158. startNode(ctx, node)
  159. defer node.Stop()
  160. // Attach to the newly started node and start the JavaScript console
  161. client, err := node.Attach()
  162. if err != nil {
  163. utils.Fatalf("Failed to attach to the inproc geth: %v", err)
  164. }
  165. config := console.Config{
  166. DataDir: utils.MakeDataDir(ctx),
  167. DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
  168. Client: client,
  169. Preload: utils.MakeConsolePreloads(ctx),
  170. }
  171. console, err := console.New(config)
  172. if err != nil {
  173. utils.Fatalf("Failed to start the JavaScript console: %v", err)
  174. }
  175. defer console.Stop(false)
  176. // Evaluate each of the specified JavaScript files
  177. for _, file := range ctx.Args() {
  178. if err = console.Execute(file); err != nil {
  179. utils.Fatalf("Failed to execute %s: %v", file, err)
  180. }
  181. }
  182. // Wait for pending callbacks, but stop for Ctrl-C.
  183. abort := make(chan os.Signal, 1)
  184. signal.Notify(abort, os.Interrupt)
  185. go func() {
  186. <-abort
  187. os.Exit(0)
  188. }()
  189. console.Stop(true)
  190. return nil
  191. }