cmds.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package cli_options
  2. import (
  3. "errors"
  4. "github.com/gravitl/netmaker/logger"
  5. "github.com/gravitl/netmaker/netclient/command"
  6. "github.com/gravitl/netmaker/netclient/config"
  7. "github.com/urfave/cli/v2"
  8. )
  9. func parseVerbosity(c *cli.Context) {
  10. if c.Bool("V") {
  11. logger.Verbosity = 1
  12. } else if c.Bool("VV") {
  13. logger.Verbosity = 2
  14. } else if c.Bool("VVV") {
  15. logger.Verbosity = 3
  16. }
  17. }
  18. // GetCommands - return commands that CLI uses
  19. func GetCommands(cliFlags []cli.Flag) []*cli.Command {
  20. return []*cli.Command{
  21. {
  22. Name: "join",
  23. Usage: "Join a Netmaker network.",
  24. Flags: cliFlags,
  25. Action: func(c *cli.Context) error {
  26. parseVerbosity(c)
  27. cfg, pvtKey, err := config.GetCLIConfig(c)
  28. if err != nil {
  29. return err
  30. }
  31. if cfg.Network == "all" {
  32. err = errors.New("no network provided")
  33. return err
  34. }
  35. if cfg.Server.GRPCAddress == "" {
  36. err = errors.New("no server address provided")
  37. return err
  38. }
  39. err = command.Join(&cfg, pvtKey)
  40. return err
  41. },
  42. },
  43. {
  44. Name: "leave",
  45. Usage: "Leave a Netmaker network.",
  46. Flags: cliFlags,
  47. // the action, or code that will be executed when
  48. // we execute our `ns` command
  49. Action: func(c *cli.Context) error {
  50. parseVerbosity(c)
  51. cfg, _, err := config.GetCLIConfig(c)
  52. if err != nil {
  53. return err
  54. }
  55. err = command.Leave(&cfg, c.String("force") == "yes")
  56. return err
  57. },
  58. },
  59. {
  60. Name: "pull",
  61. Usage: "Pull latest configuration and peers from server.",
  62. Flags: cliFlags,
  63. // the action, or code that will be executed when
  64. // we execute our `ns` command
  65. Action: func(c *cli.Context) error {
  66. parseVerbosity(c)
  67. cfg, _, err := config.GetCLIConfig(c)
  68. if err != nil {
  69. return err
  70. }
  71. err = command.Pull(&cfg)
  72. return err
  73. },
  74. },
  75. {
  76. Name: "list",
  77. Usage: "Get list of networks.",
  78. Flags: cliFlags,
  79. // the action, or code that will be executed when
  80. // we execute our `ns` command
  81. Action: func(c *cli.Context) error {
  82. parseVerbosity(c)
  83. cfg, _, err := config.GetCLIConfig(c)
  84. if err != nil {
  85. return err
  86. }
  87. err = command.List(cfg)
  88. return err
  89. },
  90. },
  91. {
  92. Name: "uninstall",
  93. Usage: "Uninstall the netclient system service.",
  94. Flags: cliFlags,
  95. // the action, or code that will be executed when
  96. // we execute our `ns` command
  97. Action: func(c *cli.Context) error {
  98. parseVerbosity(c)
  99. err := command.Uninstall()
  100. return err
  101. },
  102. },
  103. {
  104. Name: "daemon",
  105. Usage: "run netclient as daemon",
  106. Flags: cliFlags,
  107. Action: func(c *cli.Context) error {
  108. // set max verbosity for daemon regardless
  109. logger.Verbosity = 3
  110. err := command.Daemon()
  111. return err
  112. },
  113. },
  114. }
  115. }