update.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package host
  2. import (
  3. "encoding/json"
  4. "log"
  5. "os"
  6. "github.com/gravitl/netmaker/cli/functions"
  7. "github.com/gravitl/netmaker/models"
  8. "github.com/spf13/cobra"
  9. )
  10. var (
  11. apiHostFilePath string
  12. endpoint string
  13. name string
  14. listenPort int
  15. proxyListenPort int
  16. mtu int
  17. proxyEnabled bool
  18. isStatic bool
  19. isDefault bool
  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.Name = name
  40. apiHost.ListenPort = listenPort
  41. apiHost.ProxyListenPort = proxyListenPort
  42. apiHost.MTU = mtu
  43. apiHost.ProxyEnabled = proxyEnabled
  44. apiHost.IsStatic = isStatic
  45. apiHost.IsDefault = isDefault
  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(&name, "name", "", "Host name")
  54. hostUpdateCmd.Flags().IntVar(&listenPort, "listen_port", 0, "Listen port of the host")
  55. hostUpdateCmd.Flags().IntVar(&proxyListenPort, "proxy_listen_port", 0, "Proxy listen port of the host")
  56. hostUpdateCmd.Flags().IntVar(&mtu, "mtu", 0, "Host MTU size")
  57. hostUpdateCmd.Flags().BoolVar(&proxyEnabled, "proxy", false, "Enable proxy ?")
  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. }