update.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. endpoint6 string
  14. name string
  15. listenPort int
  16. mtu int
  17. isStaticPort bool
  18. isStatic bool
  19. isDefault bool
  20. keepAlive int
  21. )
  22. var hostUpdateCmd = &cobra.Command{
  23. Use: "update DeviceID/HostID",
  24. Args: cobra.ExactArgs(1),
  25. Short: "Update a device",
  26. Long: `Update a device`,
  27. Run: func(cmd *cobra.Command, args []string) {
  28. apiHost := &models.ApiHost{}
  29. if apiHostFilePath != "" {
  30. content, err := os.ReadFile(apiHostFilePath)
  31. if err != nil {
  32. log.Fatal("Error when opening file: ", err)
  33. }
  34. if err := json.Unmarshal(content, apiHost); err != nil {
  35. log.Fatal(err)
  36. }
  37. } else {
  38. apiHost.ID = args[0]
  39. apiHost.EndpointIP = endpoint
  40. apiHost.EndpointIPv6 = endpoint6
  41. apiHost.Name = name
  42. apiHost.ListenPort = listenPort
  43. apiHost.MTU = mtu
  44. apiHost.IsStaticPort = isStaticPort
  45. apiHost.IsStatic = isStatic
  46. apiHost.IsDefault = isDefault
  47. apiHost.PersistentKeepalive = keepAlive
  48. }
  49. functions.PrettyPrint(functions.UpdateHost(args[0], apiHost))
  50. },
  51. }
  52. func init() {
  53. hostUpdateCmd.Flags().StringVar(&apiHostFilePath, "file", "", "Path to host_definition.json")
  54. hostUpdateCmd.Flags().StringVar(&endpoint, "endpoint", "", "Endpoint of the Device")
  55. hostUpdateCmd.Flags().StringVar(&endpoint6, "endpoint6", "", "IPv6 Endpoint of the Device")
  56. hostUpdateCmd.Flags().StringVar(&name, "name", "", "Device name")
  57. hostUpdateCmd.Flags().IntVar(&listenPort, "listen_port", 0, "Listen port of the device")
  58. hostUpdateCmd.Flags().IntVar(&mtu, "mtu", 0, "Device MTU size")
  59. hostUpdateCmd.Flags().IntVar(&keepAlive, "keep_alive", 0, "Interval (seconds) in which packets are sent to keep connections open with peers")
  60. hostUpdateCmd.Flags().BoolVar(&isStaticPort, "static_port", false, "Make Device Static Port?")
  61. hostUpdateCmd.Flags().BoolVar(&isStatic, "static_endpoint", false, "Make Device Static Endpoint?")
  62. hostUpdateCmd.Flags().BoolVar(&isDefault, "default", false, "Make Device Default ?")
  63. rootCmd.AddCommand(hostUpdateCmd)
  64. }