update.go 1.6 KB

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