main.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. discv4Command,
  57. discv5Command,
  58. dnsCommand,
  59. nodesetCommand,
  60. }
  61. }
  62. func main() {
  63. exit(app.Run(os.Args))
  64. }
  65. // commandHasFlag returns true if the current command supports the given flag.
  66. func commandHasFlag(ctx *cli.Context, flag cli.Flag) bool {
  67. flags := ctx.FlagNames()
  68. sort.Strings(flags)
  69. i := sort.SearchStrings(flags, flag.GetName())
  70. return i != len(flags) && flags[i] == flag.GetName()
  71. }
  72. // getNodeArg handles the common case of a single node descriptor argument.
  73. func getNodeArg(ctx *cli.Context) *enode.Node {
  74. if ctx.NArg() != 1 {
  75. exit("missing node as command-line argument")
  76. }
  77. n, err := parseNode(ctx.Args()[0])
  78. if err != nil {
  79. exit(err)
  80. }
  81. return n
  82. }
  83. func exit(err interface{}) {
  84. if err == nil {
  85. os.Exit(0)
  86. }
  87. fmt.Fprintln(os.Stderr, err)
  88. os.Exit(1)
  89. }