main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "sort"
  22. "github.com/ethereum/go-ethereum/internal/debug"
  23. "github.com/ethereum/go-ethereum/p2p/enode"
  24. "github.com/ethereum/go-ethereum/params"
  25. "gopkg.in/urfave/cli.v1"
  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. return debug.Setup(ctx)
  44. }
  45. app.After = func(ctx *cli.Context) error {
  46. debug.Exit()
  47. return nil
  48. }
  49. app.CommandNotFound = func(ctx *cli.Context, cmd string) {
  50. fmt.Fprintf(os.Stderr, "No such command: %s\n", cmd)
  51. os.Exit(1)
  52. }
  53. // Add subcommands.
  54. app.Commands = []cli.Command{
  55. enrdumpCommand,
  56. keyCommand,
  57. discv4Command,
  58. discv5Command,
  59. dnsCommand,
  60. nodesetCommand,
  61. rlpxCommand,
  62. }
  63. }
  64. func main() {
  65. exit(app.Run(os.Args))
  66. }
  67. // commandHasFlag returns true if the current command supports the given flag.
  68. func commandHasFlag(ctx *cli.Context, flag cli.Flag) bool {
  69. flags := ctx.FlagNames()
  70. sort.Strings(flags)
  71. i := sort.SearchStrings(flags, flag.GetName())
  72. return i != len(flags) && flags[i] == flag.GetName()
  73. }
  74. // getNodeArg handles the common case of a single node descriptor argument.
  75. func getNodeArg(ctx *cli.Context) *enode.Node {
  76. if ctx.NArg() < 1 {
  77. exit("missing node as command-line argument")
  78. }
  79. n, err := parseNode(ctx.Args()[0])
  80. if err != nil {
  81. exit(err)
  82. }
  83. return n
  84. }
  85. func exit(err interface{}) {
  86. if err == nil {
  87. os.Exit(0)
  88. }
  89. fmt.Fprintln(os.Stderr, err)
  90. os.Exit(1)
  91. }