main.go 2.6 KB

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