update.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.EndpointIP = endpoint
  38. apiHost.Name = name
  39. apiHost.ListenPort = listenPort
  40. apiHost.ProxyListenPort = proxyListenPort
  41. apiHost.MTU = mtu
  42. apiHost.ProxyEnabled = proxyEnabled
  43. apiHost.IsStatic = isStatic
  44. apiHost.IsDefault = isDefault
  45. }
  46. functions.PrettyPrint(functions.UpdateHost(args[0], apiHost))
  47. },
  48. }
  49. func init() {
  50. hostUpdateCmd.Flags().StringVar(&apiHostFilePath, "file", "", "Path to host_definition.json")
  51. hostUpdateCmd.Flags().StringVar(&endpoint, "endpoint", "", "Endpoint of the Host")
  52. hostUpdateCmd.Flags().StringVar(&name, "name", "", "Host name")
  53. hostUpdateCmd.Flags().IntVar(&listenPort, "listen_port", 0, "Listen port of the host")
  54. hostUpdateCmd.Flags().IntVar(&proxyListenPort, "proxy_listen_port", 0, "Proxy listen port of the host")
  55. hostUpdateCmd.Flags().IntVar(&mtu, "mtu", 0, "Host MTU size")
  56. hostUpdateCmd.Flags().BoolVar(&proxyEnabled, "proxy", false, "Enable proxy ?")
  57. hostUpdateCmd.Flags().BoolVar(&isStatic, "static", false, "Make Host Static ?")
  58. hostUpdateCmd.Flags().BoolVar(&isDefault, "default", false, "Make Host Default ?")
  59. rootCmd.AddCommand(hostUpdateCmd)
  60. }