update.go 2.0 KB

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