github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/cmd/geth/consolecmd.go (about)

     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  
    17  package main
    18  
    19  import (
    20  	"log"
    21  	"os"
    22  	"os/signal"
    23  
    24  	"github.com/ethereumproject/go-ethereum/console"
    25  	"github.com/ethereumproject/go-ethereum/node"
    26  	"github.com/ethereumproject/go-ethereum/rpc"
    27  	"gopkg.in/urfave/cli.v1"
    28  )
    29  
    30  var (
    31  	consoleCommand = cli.Command{
    32  		Action: localConsole,
    33  		Name:   "console",
    34  		Usage:  `Geth Console: interactive JavaScript environment`,
    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  		Flags: []cli.Flag{
    41  			ExecFlag,
    42  		},
    43  	}
    44  	attachCommand = cli.Command{
    45  		Action: remoteConsole,
    46  		Name:   "attach",
    47  		Usage:  `Geth Console: interactive JavaScript environment (connect to node)`,
    48  		Description: `
    49  	The Geth console is an interactive shell for the JavaScript runtime environment
    50  	which exposes a node admin interface as well as the Ðapp JavaScript API.
    51  	See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console.
    52  	This command allows to open a console on a running geth node.
    53  
    54  	<DATADIR> and <CHAINDIR> flags will be parsed as usual.
    55  	For example:
    56  
    57  		geth --chain=morden attach
    58  
    59  	or,
    60  
    61  		geth --data-dir=/path/to/gethdata --chain privatenet attach
    62  		`,
    63  		Flags: []cli.Flag{
    64  			ExecFlag,
    65  		},
    66  	}
    67  	javascriptCommand = cli.Command{
    68  		Action: ephemeralConsole,
    69  		Name:   "js",
    70  		Usage:  `Executes the given JavaScript files in the Geth JavaScript VM`,
    71  		Description: `
    72  	The JavaScript VM exposes a node admin interface as well as the Ðapp
    73  	JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console
    74  		`,
    75  	}
    76  )
    77  
    78  // localConsole starts a new geth node, attaching a JavaScript console to it at the
    79  // same time.
    80  func localConsole(ctx *cli.Context) error {
    81  	// Create and start the node based on the CLI flags
    82  	node := MakeSystemNode(Version, ctx)
    83  	startNode(ctx, node)
    84  	defer node.Stop()
    85  
    86  	// Attach to the newly started node and start the JavaScript console
    87  	client, err := node.Attach()
    88  	if err != nil {
    89  		log.Fatal("Failed to attach to the inproc geth: ", err)
    90  	}
    91  	config := console.Config{
    92  		DataDir: node.DataDir(),
    93  		DocRoot: ctx.GlobalString(JSpathFlag.Name),
    94  		Client:  client,
    95  		Preload: MakeConsolePreloads(ctx),
    96  	}
    97  	console, err := console.New(config)
    98  	if err != nil {
    99  		log.Fatal("Failed to start the JavaScript console: ", err)
   100  	}
   101  	defer console.Stop(false)
   102  
   103  	// If only a short execution was requested, evaluate and return
   104  	//
   105  	// --exec as command sub-flag
   106  	if script := ctx.String(ExecFlag.Name); script != "" {
   107  		console.Evaluate(script)
   108  		return nil
   109  	}
   110  
   111  	// --exec as global flag
   112  	if script := ctx.GlobalString(ExecFlag.Name); script != "" {
   113  		console.Evaluate(script)
   114  		return nil
   115  	}
   116  
   117  	// Otherwise print the welcome screen and enter interactive mode
   118  	console.Welcome()
   119  	console.Interactive()
   120  
   121  	return nil
   122  }
   123  
   124  // remoteConsole will connect to a remote geth instance, attaching a JavaScript
   125  // console to it.
   126  func remoteConsole(ctx *cli.Context) error {
   127  	// Attach to a remotely running geth instance and start the JavaScript console
   128  	chainDir := MustMakeChainDataDir(ctx)
   129  	var uri = "ipc:" + node.DefaultIPCEndpoint(chainDir)
   130  	if ctx.Args().Present() {
   131  		uri = ctx.Args().First()
   132  	}
   133  	client, err := rpc.NewClient(uri)
   134  	if err != nil {
   135  		log.Fatal("attach to remote geth: ", err)
   136  	}
   137  
   138  	config := console.Config{
   139  		DataDir: chainDir,
   140  		DocRoot: ctx.GlobalString(JSpathFlag.Name),
   141  		Client:  client,
   142  		Preload: MakeConsolePreloads(ctx),
   143  	}
   144  
   145  	console, err := console.New(config)
   146  	if err != nil {
   147  		log.Fatal("Failed to start the JavaScript console: ", err)
   148  	}
   149  	defer console.Stop(false)
   150  
   151  	// If only a short execution was requested, evaluate and return
   152  	//
   153  	// --exec as command sub-flag
   154  	if script := ctx.String(ExecFlag.Name); script != "" {
   155  		console.Evaluate(script)
   156  		return nil
   157  	}
   158  
   159  	// --exec as global flag
   160  	if script := ctx.GlobalString(ExecFlag.Name); script != "" {
   161  		console.Evaluate(script)
   162  		return nil
   163  	}
   164  	// Otherwise print the welcome screen and enter interactive mode
   165  	console.Welcome()
   166  	console.Interactive()
   167  
   168  	return nil
   169  }
   170  
   171  // ephemeralConsole starts a new geth node, attaches an ephemeral JavaScript
   172  // console to it, and each of the files specified as arguments and tears the
   173  // everything down.
   174  func ephemeralConsole(ctx *cli.Context) error {
   175  	// Create and start the node based on the CLI flags
   176  	node := MakeSystemNode(Version, ctx)
   177  	startNode(ctx, node)
   178  	defer node.Stop()
   179  
   180  	// Attach to the newly started node and start the JavaScript console
   181  	client, err := node.Attach()
   182  	if err != nil {
   183  		log.Fatal("Failed to attach to the inproc geth: ", err)
   184  	}
   185  	config := console.Config{
   186  		DataDir: node.DataDir(),
   187  		DocRoot: ctx.GlobalString(JSpathFlag.Name),
   188  		Client:  client,
   189  		Preload: MakeConsolePreloads(ctx),
   190  	}
   191  	console, err := console.New(config)
   192  	if err != nil {
   193  		log.Fatal("Failed to start the JavaScript console: ", err)
   194  	}
   195  	defer console.Stop(false)
   196  
   197  	// Evaluate each of the specified JavaScript files
   198  	for _, file := range ctx.Args() {
   199  		if err = console.Execute(file); err != nil {
   200  			log.Fatalf("Failed to execute %s: %v", file, err)
   201  		}
   202  	}
   203  	// Wait for pending callbacks, but stop for Ctrl-C.
   204  	abort := make(chan os.Signal, 1)
   205  	signal.Notify(abort, os.Interrupt)
   206  
   207  	go func() {
   208  		<-abort
   209  		os.Exit(0)
   210  	}()
   211  	console.Stop(true)
   212  
   213  	return nil
   214  }