github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/cmd/devp2p/main.go (about)

     1  // Copyright 2019 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  	"os"
    22  
    23  	"github.com/ethereum/go-ethereum/internal/debug"
    24  	"github.com/ethereum/go-ethereum/internal/flags"
    25  	"github.com/ethereum/go-ethereum/p2p/enode"
    26  	"github.com/urfave/cli/v2"
    27  )
    28  
    29  var app = flags.NewApp("go-ethereum devp2p tool")
    30  
    31  func init() {
    32  	app.HideVersion = true
    33  	app.Flags = append(app.Flags, debug.Flags...)
    34  	app.Before = func(ctx *cli.Context) error {
    35  		flags.MigrateGlobalFlags(ctx)
    36  		return debug.Setup(ctx)
    37  	}
    38  	app.After = func(ctx *cli.Context) error {
    39  		debug.Exit()
    40  		return nil
    41  	}
    42  	app.CommandNotFound = func(ctx *cli.Context, cmd string) {
    43  		fmt.Fprintf(os.Stderr, "No such command: %s\n", cmd)
    44  		os.Exit(1)
    45  	}
    46  
    47  	// Add subcommands.
    48  	app.Commands = []*cli.Command{
    49  		enrdumpCommand,
    50  		keyCommand,
    51  		discv4Command,
    52  		discv5Command,
    53  		dnsCommand,
    54  		nodesetCommand,
    55  		rlpxCommand,
    56  	}
    57  }
    58  
    59  func main() {
    60  	exit(app.Run(os.Args))
    61  }
    62  
    63  // commandHasFlag returns true if the current command supports the given flag.
    64  func commandHasFlag(ctx *cli.Context, flag cli.Flag) bool {
    65  	names := flag.Names()
    66  	set := make(map[string]struct{}, len(names))
    67  	for _, name := range names {
    68  		set[name] = struct{}{}
    69  	}
    70  	for _, fn := range ctx.FlagNames() {
    71  		if _, ok := set[fn]; ok {
    72  			return true
    73  		}
    74  	}
    75  	return false
    76  }
    77  
    78  // getNodeArg handles the common case of a single node descriptor argument.
    79  func getNodeArg(ctx *cli.Context) *enode.Node {
    80  	if ctx.NArg() < 1 {
    81  		exit("missing node as command-line argument")
    82  	}
    83  	n, err := parseNode(ctx.Args().First())
    84  	if err != nil {
    85  		exit(err)
    86  	}
    87  	return n
    88  }
    89  
    90  func exit(err interface{}) {
    91  	if err == nil {
    92  		os.Exit(0)
    93  	}
    94  	fmt.Fprintln(os.Stderr, err)
    95  	os.Exit(1)
    96  }