github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/cmd/devp2p/main.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package main
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  	"sort"
    25  
    26  	"github.com/AigarNetwork/aigar/internal/debug"
    27  	"github.com/AigarNetwork/aigar/p2p/enode"
    28  	"github.com/AigarNetwork/aigar/params"
    29  	"gopkg.in/urfave/cli.v1"
    30  )
    31  
    32  var (
    33  	// Git information set by linker when building with ci.go.
    34  	gitCommit string
    35  	gitDate   string
    36  	app       = &cli.App{
    37  		Name:        filepath.Base(os.Args[0]),
    38  		Usage:       "go-ethereum devp2p tool",
    39  		Version:     params.VersionWithCommit(gitCommit, gitDate),
    40  		Writer:      os.Stdout,
    41  		HideVersion: true,
    42  	}
    43  )
    44  
    45  func init() {
    46  	// Set up the CLI app.
    47  	app.Flags = append(app.Flags, debug.Flags...)
    48  	app.Before = func(ctx *cli.Context) error {
    49  		return debug.Setup(ctx, "")
    50  	}
    51  	app.After = func(ctx *cli.Context) error {
    52  		debug.Exit()
    53  		return nil
    54  	}
    55  	app.CommandNotFound = func(ctx *cli.Context, cmd string) {
    56  		fmt.Fprintf(os.Stderr, "No such command: %s\n", cmd)
    57  		os.Exit(1)
    58  	}
    59  	// Add subcommands.
    60  	app.Commands = []cli.Command{
    61  		enrdumpCommand,
    62  		discv4Command,
    63  		dnsCommand,
    64  		nodesetCommand,
    65  	}
    66  }
    67  
    68  func main() {
    69  	exit(app.Run(os.Args))
    70  }
    71  
    72  // commandHasFlag returns true if the current command supports the given flag.
    73  func commandHasFlag(ctx *cli.Context, flag cli.Flag) bool {
    74  	flags := ctx.FlagNames()
    75  	sort.Strings(flags)
    76  	i := sort.SearchStrings(flags, flag.GetName())
    77  	return i != len(flags) && flags[i] == flag.GetName()
    78  }
    79  
    80  // getNodeArg handles the common case of a single node descriptor argument.
    81  func getNodeArg(ctx *cli.Context) *enode.Node {
    82  	if ctx.NArg() != 1 {
    83  		exit("missing node as command-line argument")
    84  	}
    85  	n, err := parseNode(ctx.Args()[0])
    86  	if err != nil {
    87  		exit(err)
    88  	}
    89  	return n
    90  }
    91  
    92  func exit(err interface{}) {
    93  	if err == nil {
    94  		os.Exit(0)
    95  	}
    96  	fmt.Fprintln(os.Stderr, err)
    97  	os.Exit(1)
    98  }