cmds.go 2.7 KB

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