github.com/core-coin/go-core/v2@v2.1.9/cmd/devp2p/main.go (about)

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