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