github.com/mprishchepo/go-ethereum@v1.9.7-0.20191031044858-21506be82b68/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  	"path/filepath"
    23  	"sort"
    24  
    25  	"github.com/Fantom-foundation/go-ethereum/internal/debug"
    26  	"github.com/Fantom-foundation/go-ethereum/p2p/enode"
    27  	"github.com/Fantom-foundation/go-ethereum/params"
    28  	"gopkg.in/urfave/cli.v1"
    29  )
    30  
    31  var (
    32  	// Git information set by linker when building with ci.go.
    33  	gitCommit string
    34  	gitDate   string
    35  	app       = &cli.App{
    36  		Name:        filepath.Base(os.Args[0]),
    37  		Usage:       "go-ethereum devp2p tool",
    38  		Version:     params.VersionWithCommit(gitCommit, gitDate),
    39  		Writer:      os.Stdout,
    40  		HideVersion: true,
    41  	}
    42  )
    43  
    44  func init() {
    45  	// Set up the CLI app.
    46  	app.Flags = append(app.Flags, debug.Flags...)
    47  	app.Before = func(ctx *cli.Context) error {
    48  		return debug.Setup(ctx, "")
    49  	}
    50  	app.After = func(ctx *cli.Context) error {
    51  		debug.Exit()
    52  		return nil
    53  	}
    54  	app.CommandNotFound = func(ctx *cli.Context, cmd string) {
    55  		fmt.Fprintf(os.Stderr, "No such command: %s\n", cmd)
    56  		os.Exit(1)
    57  	}
    58  	// Add subcommands.
    59  	app.Commands = []cli.Command{
    60  		enrdumpCommand,
    61  		discv4Command,
    62  		dnsCommand,
    63  		nodesetCommand,
    64  	}
    65  }
    66  
    67  func main() {
    68  	exit(app.Run(os.Args))
    69  }
    70  
    71  // commandHasFlag returns true if the current command supports the given flag.
    72  func commandHasFlag(ctx *cli.Context, flag cli.Flag) bool {
    73  	flags := ctx.FlagNames()
    74  	sort.Strings(flags)
    75  	i := sort.SearchStrings(flags, flag.GetName())
    76  	return i != len(flags) && flags[i] == flag.GetName()
    77  }
    78  
    79  // getNodeArg handles the common case of a single node descriptor argument.
    80  func getNodeArg(ctx *cli.Context) *enode.Node {
    81  	if ctx.NArg() != 1 {
    82  		exit("missing node as command-line argument")
    83  	}
    84  	n, err := parseNode(ctx.Args()[0])
    85  	if err != nil {
    86  		exit(err)
    87  	}
    88  	return n
    89  }
    90  
    91  func exit(err interface{}) {
    92  	if err == nil {
    93  		os.Exit(0)
    94  	}
    95  	fmt.Fprintln(os.Stderr, err)
    96  	os.Exit(1)
    97  }