main.go 2.4 KB

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