cmds.go 2.6 KB

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