update.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package host
  2. import (
  3. "encoding/json"
  4. "log"
  5. "os"
  6. "github.com/spf13/cobra"
  7. "github.com/gravitl/netmaker/cli/functions"
  8. "github.com/gravitl/netmaker/models"
  9. )
  10. var (
  11. apiHostFilePath string
  12. endpoint string
  13. name string
  14. listenPort int
  15. mtu int
  16. isStatic bool
  17. isDefault bool
  18. keepAlive int
  19. )
  20. var hostUpdateCmd = &cobra.Command{
  21. Use: "update HostID",
  22. Args: cobra.ExactArgs(1),
  23. Short: "Update a host",
  24. Long: `Update a host`,
  25. Run: func(cmd *cobra.Command, args []string) {
  26. apiHost := &models.ApiHost{}
  27. if apiHostFilePath != "" {
  28. content, err := os.ReadFile(apiHostFilePath)
  29. if err != nil {
  30. log.Fatal("Error when opening file: ", err)
  31. }
  32. if err := json.Unmarshal(content, apiHost); err != nil {
  33. log.Fatal(err)
  34. }
  35. } else {
  36. apiHost.ID = args[0]
  37. apiHost.EndpointIP = endpoint
  38. apiHost.Name = name
  39. apiHost.ListenPort = listenPort
  40. apiHost.MTU = mtu
  41. apiHost.IsStatic = isStatic
  42. apiHost.IsDefault = isDefault
  43. apiHost.PersistentKeepalive = keepAlive
  44. }
  45. functions.PrettyPrint(functions.UpdateHost(args[0], apiHost))
  46. },
  47. }
  48. func init() {
  49. hostUpdateCmd.Flags().StringVar(&apiHostFilePath, "file", "", "Path to host_definition.json")
  50. hostUpdateCmd.Flags().StringVar(&endpoint, "endpoint", "", "Endpoint of the Host")
  51. hostUpdateCmd.Flags().StringVar(&name, "name", "", "Host name")
  52. hostUpdateCmd.Flags().IntVar(&listenPort, "listen_port", 0, "Listen port of the host")
  53. hostUpdateCmd.Flags().IntVar(&mtu, "mtu", 0, "Host MTU size")
  54. hostUpdateCmd.Flags().IntVar(&keepAlive, "keep_alive", 0, "Interval (seconds) in which packets are sent to keep connections open with peers")
  55. hostUpdateCmd.Flags().BoolVar(&isStatic, "static", false, "Make Host Static ?")
  56. hostUpdateCmd.Flags().BoolVar(&isDefault, "default", false, "Make Host Default ?")
  57. rootCmd.AddCommand(hostUpdateCmd)
  58. }