github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/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 "gopkg.in/urfave/cli.v1" 26 27 "github.com/scroll-tech/go-ethereum/internal/debug" 28 "github.com/scroll-tech/go-ethereum/p2p/enode" 29 "github.com/scroll-tech/go-ethereum/params" 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 keyCommand, 63 discv4Command, 64 discv5Command, 65 dnsCommand, 66 nodesetCommand, 67 rlpxCommand, 68 } 69 } 70 71 func main() { 72 exit(app.Run(os.Args)) 73 } 74 75 // commandHasFlag returns true if the current command supports the given flag. 76 func commandHasFlag(ctx *cli.Context, flag cli.Flag) bool { 77 flags := ctx.FlagNames() 78 sort.Strings(flags) 79 i := sort.SearchStrings(flags, flag.GetName()) 80 return i != len(flags) && flags[i] == flag.GetName() 81 } 82 83 // getNodeArg handles the common case of a single node descriptor argument. 84 func getNodeArg(ctx *cli.Context) *enode.Node { 85 if ctx.NArg() < 1 { 86 exit("missing node as command-line argument") 87 } 88 n, err := parseNode(ctx.Args()[0]) 89 if err != nil { 90 exit(err) 91 } 92 return n 93 } 94 95 func exit(err interface{}) { 96 if err == nil { 97 os.Exit(0) 98 } 99 fmt.Fprintln(os.Stderr, err) 100 os.Exit(1) 101 }