cmds.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package cli_options
  2. import (
  3. "github.com/gravitl/netmaker/logger"
  4. "github.com/gravitl/netmaker/netclient/command"
  5. "github.com/gravitl/netmaker/netclient/config"
  6. "github.com/urfave/cli/v2"
  7. )
  8. // GetCommands - return commands that CLI uses
  9. func GetCommands(cliFlags []cli.Flag) []*cli.Command {
  10. return []*cli.Command{
  11. {
  12. Name: "join",
  13. Usage: "Join a Netmaker network.",
  14. Flags: cliFlags,
  15. Action: func(c *cli.Context) error {
  16. parseVerbosity(c)
  17. cfg, pvtKey, err := config.GetCLIConfig(c)
  18. if err != nil {
  19. return err
  20. }
  21. err = command.Join(&cfg, pvtKey)
  22. return err
  23. },
  24. },
  25. {
  26. Name: "leave",
  27. Usage: "Leave a Netmaker network.",
  28. Flags: cliFlags,
  29. // the action, or code that will be executed when
  30. // we execute our `ns` command
  31. Action: func(c *cli.Context) error {
  32. parseVerbosity(c)
  33. cfg, _, err := config.GetCLIConfig(c)
  34. if err != nil {
  35. return err
  36. }
  37. err = command.Leave(&cfg)
  38. return err
  39. },
  40. },
  41. {
  42. Name: "pull",
  43. Usage: "Pull latest configuration and peers from server.",
  44. Flags: cliFlags,
  45. // the action, or code that will be executed when
  46. // we execute our `ns` command
  47. Action: func(c *cli.Context) error {
  48. parseVerbosity(c)
  49. cfg, _, err := config.GetCLIConfig(c)
  50. if err != nil {
  51. return err
  52. }
  53. err = command.Pull(&cfg)
  54. return err
  55. },
  56. },
  57. {
  58. Name: "list",
  59. Usage: "Get list of networks.",
  60. Flags: cliFlags,
  61. // the action, or code that will be executed when
  62. // we execute our `ns` command
  63. Action: func(c *cli.Context) error {
  64. parseVerbosity(c)
  65. cfg, _, err := config.GetCLIConfig(c)
  66. if err != nil {
  67. return err
  68. }
  69. err = command.List(cfg)
  70. return err
  71. },
  72. },
  73. {
  74. Name: "uninstall",
  75. Usage: "Uninstall the netclient system service.",
  76. Flags: cliFlags,
  77. // the action, or code that will be executed when
  78. // we execute our `ns` command
  79. Action: func(c *cli.Context) error {
  80. parseVerbosity(c)
  81. err := command.Uninstall()
  82. return err
  83. },
  84. },
  85. {
  86. Name: "daemon",
  87. Usage: "run netclient as daemon",
  88. Flags: cliFlags,
  89. Action: func(c *cli.Context) error {
  90. // set max verbosity for daemon regardless
  91. logger.Verbosity = 4
  92. err := command.Daemon()
  93. return err
  94. },
  95. },
  96. {
  97. Name: "install",
  98. Usage: "install binary and daemon",
  99. Flags: cliFlags,
  100. Action: func(c *cli.Context) error {
  101. parseVerbosity(c)
  102. return command.Install()
  103. },
  104. },
  105. {
  106. Name: "connect",
  107. Usage: "connect netclient to a given network if disconnected",
  108. Flags: cliFlags,
  109. Action: func(c *cli.Context) error {
  110. parseVerbosity(c)
  111. cfg, _, err := config.GetCLIConfig(c)
  112. if err != nil {
  113. return err
  114. }
  115. return command.Connect(cfg)
  116. },
  117. },
  118. {
  119. Name: "disconnect",
  120. Usage: "disconnect netclient from a given network if connected",
  121. Flags: cliFlags,
  122. Action: func(c *cli.Context) error {
  123. parseVerbosity(c)
  124. cfg, _, err := config.GetCLIConfig(c)
  125. if err != nil {
  126. return err
  127. }
  128. return command.Disconnect(cfg)
  129. },
  130. },
  131. }
  132. }
  133. // == Private funcs ==
  134. func parseVerbosity(c *cli.Context) {
  135. if c.Bool("v") {
  136. logger.Verbosity = 1
  137. } else if c.Bool("vv") {
  138. logger.Verbosity = 2
  139. } else if c.Bool("vvv") {
  140. logger.Verbosity = 3
  141. } else if c.Bool("vvvv") {
  142. logger.Verbosity = 4
  143. }
  144. }