github.com/ethxdao/go-ethereum@v0.0.0-20221218102228-5ae34a9cc189/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 24 "github.com/ethxdao/go-ethereum/internal/debug" 25 "github.com/ethxdao/go-ethereum/internal/flags" 26 "github.com/ethxdao/go-ethereum/p2p/enode" 27 "github.com/ethxdao/go-ethereum/params" 28 ) 29 30 var ( 31 // Git information set by linker when building with ci.go. 32 gitCommit string 33 gitDate string 34 app = &cli.App{ 35 Name: filepath.Base(os.Args[0]), 36 Usage: "go-ethereum devp2p tool", 37 Version: params.VersionWithCommit(gitCommit, gitDate), 38 Writer: os.Stdout, 39 HideVersion: true, 40 } 41 ) 42 43 func init() { 44 // Set up the CLI app. 45 app.Flags = append(app.Flags, debug.Flags...) 46 app.Before = func(ctx *cli.Context) error { 47 flags.MigrateGlobalFlags(ctx) 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 keyCommand, 62 discv4Command, 63 discv5Command, 64 dnsCommand, 65 nodesetCommand, 66 rlpxCommand, 67 } 68 } 69 70 func main() { 71 exit(app.Run(os.Args)) 72 } 73 74 // commandHasFlag returns true if the current command supports the given flag. 75 func commandHasFlag(ctx *cli.Context, flag cli.Flag) bool { 76 names := flag.Names() 77 set := make(map[string]struct{}, len(names)) 78 for _, name := range names { 79 set[name] = struct{}{} 80 } 81 for _, fn := range ctx.FlagNames() { 82 if _, ok := set[fn]; ok { 83 return true 84 } 85 } 86 return false 87 } 88 89 // getNodeArg handles the common case of a single node descriptor argument. 90 func getNodeArg(ctx *cli.Context) *enode.Node { 91 if ctx.NArg() < 1 { 92 exit("missing node as command-line argument") 93 } 94 n, err := parseNode(ctx.Args().First()) 95 if err != nil { 96 exit(err) 97 } 98 return n 99 } 100 101 func exit(err interface{}) { 102 if err == nil { 103 os.Exit(0) 104 } 105 fmt.Fprintln(os.Stderr, err) 106 os.Exit(1) 107 }