commands.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package command
  2. import (
  3. "strings"
  4. "github.com/gravitl/netmaker/netclient/config"
  5. "github.com/gravitl/netmaker/netclient/daemon"
  6. "github.com/gravitl/netmaker/netclient/functions"
  7. "github.com/gravitl/netmaker/netclient/ncutils"
  8. )
  9. // Join - join command to run from cli
  10. func Join(cfg config.ClientConfig, privateKey string) error {
  11. var err error
  12. err = functions.JoinNetwork(cfg, privateKey)
  13. if err != nil && !cfg.DebugOn {
  14. if !strings.Contains(err.Error(), "ALREADY_INSTALLED") {
  15. ncutils.PrintLog("error installing: "+err.Error(), 1)
  16. err = functions.LeaveNetwork(cfg.Network)
  17. if err != nil {
  18. err = functions.WipeLocal(cfg.Network)
  19. if err != nil {
  20. ncutils.PrintLog("error removing artifacts: "+err.Error(), 1)
  21. }
  22. }
  23. if cfg.Daemon != "off" {
  24. if ncutils.IsLinux() {
  25. err = daemon.RemoveSystemDServices()
  26. }
  27. if err != nil {
  28. ncutils.PrintLog("error removing services: "+err.Error(), 1)
  29. }
  30. if ncutils.IsFreeBSD() {
  31. daemon.RemoveFreebsdDaemon()
  32. }
  33. }
  34. } else {
  35. ncutils.PrintLog("success", 0)
  36. }
  37. if err != nil && strings.Contains(err.Error(), "ALREADY_INSTALLED") {
  38. ncutils.PrintLog(err.Error(), 0)
  39. err = nil
  40. }
  41. return err
  42. }
  43. ncutils.PrintLog("joined "+cfg.Network, 1)
  44. /*
  45. if ncutils.IsWindows() {
  46. ncutils.PrintLog("setting up WireGuard app", 0)
  47. time.Sleep(time.Second >> 1)
  48. functions.Pull(cfg.Network, true)
  49. }
  50. */
  51. return err
  52. }
  53. // Leave - runs the leave command from cli
  54. func Leave(cfg config.ClientConfig) error {
  55. err := functions.LeaveNetwork(cfg.Network)
  56. if err != nil {
  57. ncutils.PrintLog("error attempting to leave network "+cfg.Network, 1)
  58. } else {
  59. ncutils.PrintLog("success", 0)
  60. }
  61. return err
  62. }
  63. // Pull - runs pull command from cli
  64. func Pull(cfg config.ClientConfig) error {
  65. var err error
  66. if cfg.Network == "all" {
  67. ncutils.PrintLog("No network selected. Running Pull for all networks.", 0)
  68. networks, err := ncutils.GetSystemNetworks()
  69. if err != nil {
  70. ncutils.PrintLog("Error retrieving networks. Exiting.", 1)
  71. return err
  72. }
  73. for _, network := range networks {
  74. _, err = functions.Pull(network, true)
  75. if err != nil {
  76. ncutils.PrintLog("Error pulling network config for network: "+network+"\n"+err.Error(), 1)
  77. } else {
  78. ncutils.PrintLog("pulled network config for "+network, 1)
  79. }
  80. }
  81. err = nil
  82. } else {
  83. _, err = functions.Pull(cfg.Network, true)
  84. }
  85. ncutils.PrintLog("reset network and peer configs", 1)
  86. if err == nil {
  87. ncutils.PrintLog("reset network and peer configs", 1)
  88. ncutils.PrintLog("success", 1)
  89. } else {
  90. ncutils.PrintLog("error occurred pulling configs from server", 1)
  91. }
  92. return err
  93. }
  94. // List - runs list command from cli
  95. func List(cfg config.ClientConfig) error {
  96. err := functions.List(cfg.Network)
  97. return err
  98. }
  99. // Uninstall - runs uninstall command from cli
  100. func Uninstall() error {
  101. ncutils.PrintLog("uninstalling netclient...", 0)
  102. err := functions.Uninstall()
  103. ncutils.PrintLog("uninstalled netclient", 0)
  104. return err
  105. }
  106. func Daemon() error {
  107. err := functions.Daemon()
  108. return err
  109. }