github.com/theQRL/go-zond@v0.1.1/cmd/gzond/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  	"fmt"
    21  	"strings"
    22  
    23  	"github.com/theQRL/go-zond/cmd/utils"
    24  	"github.com/theQRL/go-zond/console"
    25  	"github.com/theQRL/go-zond/internal/flags"
    26  	"github.com/urfave/cli/v2"
    27  )
    28  
    29  var (
    30  	consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag}
    31  
    32  	consoleCommand = &cli.Command{
    33  		Action: localConsole,
    34  		Name:   "console",
    35  		Usage:  "Start an interactive JavaScript environment",
    36  		Flags:  flags.Merge(nodeFlags, rpcFlags, consoleFlags),
    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://geth.ethereum.org/docs/interacting-with-geth/javascript-console.`,
    41  	}
    42  
    43  	attachCommand = &cli.Command{
    44  		Action:    remoteConsole,
    45  		Name:      "attach",
    46  		Usage:     "Start an interactive JavaScript environment (connect to node)",
    47  		ArgsUsage: "[endpoint]",
    48  		Flags:     flags.Merge([]cli.Flag{utils.DataDirFlag, utils.HttpHeaderFlag}, consoleFlags),
    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://geth.ethereum.org/docs/interacting-with-geth/javascript-console.
    53  This command allows to open a console on a running geth node.`,
    54  	}
    55  
    56  	javascriptCommand = &cli.Command{
    57  		Action:    ephemeralConsole,
    58  		Name:      "js",
    59  		Usage:     "(DEPRECATED) Execute the specified JavaScript files",
    60  		ArgsUsage: "<jsfile> [jsfile...]",
    61  		Flags:     flags.Merge(nodeFlags, consoleFlags),
    62  		Description: `
    63  The JavaScript VM exposes a node admin interface as well as the Ðapp
    64  JavaScript API. See https://geth.ethereum.org/docs/interacting-with-geth/javascript-console`,
    65  	}
    66  )
    67  
    68  // localConsole starts a new geth node, attaching a JavaScript console to it at the
    69  // same time.
    70  func localConsole(ctx *cli.Context) error {
    71  	// Create and start the node based on the CLI flags
    72  	prepare(ctx)
    73  	stack, backend := makeFullNode(ctx)
    74  	startNode(ctx, stack, backend, true)
    75  	defer stack.Close()
    76  
    77  	// Attach to the newly started node and create the JavaScript console.
    78  	client := stack.Attach()
    79  	config := console.Config{
    80  		DataDir: utils.MakeDataDir(ctx),
    81  		DocRoot: ctx.String(utils.JSpathFlag.Name),
    82  		Client:  client,
    83  		Preload: utils.MakeConsolePreloads(ctx),
    84  	}
    85  	console, err := console.New(config)
    86  	if err != nil {
    87  		return fmt.Errorf("failed to start the JavaScript console: %v", err)
    88  	}
    89  	defer console.Stop(false)
    90  
    91  	// If only a short execution was requested, evaluate and return.
    92  	if script := ctx.String(utils.ExecFlag.Name); script != "" {
    93  		console.Evaluate(script)
    94  		return nil
    95  	}
    96  
    97  	// Track node shutdown and stop the console when it goes down.
    98  	// This happens when SIGTERM is sent to the process.
    99  	go func() {
   100  		stack.Wait()
   101  		console.StopInteractive()
   102  	}()
   103  
   104  	// Print the welcome screen and enter interactive mode.
   105  	console.Welcome()
   106  	console.Interactive()
   107  	return nil
   108  }
   109  
   110  // remoteConsole will connect to a remote geth instance, attaching a JavaScript
   111  // console to it.
   112  func remoteConsole(ctx *cli.Context) error {
   113  	if ctx.Args().Len() > 1 {
   114  		utils.Fatalf("invalid command-line: too many arguments")
   115  	}
   116  	endpoint := ctx.Args().First()
   117  	if endpoint == "" {
   118  		cfg := defaultNodeConfig()
   119  		utils.SetDataDir(ctx, &cfg)
   120  		endpoint = cfg.IPCEndpoint()
   121  	}
   122  	client, err := utils.DialRPCWithHeaders(endpoint, ctx.StringSlice(utils.HttpHeaderFlag.Name))
   123  	if err != nil {
   124  		utils.Fatalf("Unable to attach to remote geth: %v", err)
   125  	}
   126  	config := console.Config{
   127  		DataDir: utils.MakeDataDir(ctx),
   128  		DocRoot: ctx.String(utils.JSpathFlag.Name),
   129  		Client:  client,
   130  		Preload: utils.MakeConsolePreloads(ctx),
   131  	}
   132  	console, err := console.New(config)
   133  	if err != nil {
   134  		utils.Fatalf("Failed to start the JavaScript console: %v", err)
   135  	}
   136  	defer console.Stop(false)
   137  
   138  	if script := ctx.String(utils.ExecFlag.Name); script != "" {
   139  		console.Evaluate(script)
   140  		return nil
   141  	}
   142  
   143  	// Otherwise print the welcome screen and enter interactive mode
   144  	console.Welcome()
   145  	console.Interactive()
   146  	return nil
   147  }
   148  
   149  // ephemeralConsole starts a new geth node, attaches an ephemeral JavaScript
   150  // console to it, executes each of the files specified as arguments and tears
   151  // everything down.
   152  func ephemeralConsole(ctx *cli.Context) error {
   153  	var b strings.Builder
   154  	for _, file := range ctx.Args().Slice() {
   155  		b.Write([]byte(fmt.Sprintf("loadScript('%s');", file)))
   156  	}
   157  	utils.Fatalf(`The "js" command is deprecated. Please use the following instead:
   158  geth --exec "%s" console`, b.String())
   159  	return nil
   160  }