2
0

update.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package ext_client
  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. extClientUpdateFile string
  12. )
  13. var extClientUpdateCmd = &cobra.Command{
  14. Use: "update [NETWORK NAME] [EXTERNAL CLIENT ID]",
  15. Args: cobra.ExactArgs(2),
  16. Short: "Update an External Client",
  17. Long: `Update an External Client`,
  18. Run: func(cmd *cobra.Command, args []string) {
  19. var (
  20. network = args[0]
  21. clientID = args[1]
  22. extClient = &models.CustomExtClient{}
  23. )
  24. if extClientUpdateFile != "" {
  25. content, err := os.ReadFile(extClientUpdateFile)
  26. if err != nil {
  27. log.Fatal("Error when opening file: ", err)
  28. }
  29. if err := json.Unmarshal(content, extClient); err != nil {
  30. log.Fatal(err)
  31. }
  32. } else {
  33. extClient.ClientID = extClientID
  34. extClient.PublicKey = publicKey
  35. extClient.DNS = dns
  36. }
  37. functions.PrettyPrint(functions.UpdateExtClient(network, clientID, extClient))
  38. },
  39. }
  40. func init() {
  41. extClientUpdateCmd.Flags().StringVar(&extClientID, "id", "", "updated ID of the external client")
  42. extClientUpdateCmd.Flags().StringVar(&extClientUpdateFile, "file", "", "Filepath of updated external client definition in JSON")
  43. extClientUpdateCmd.Flags().StringVar(&publicKey, "public_key", "", "updated public key of the external client")
  44. extClientUpdateCmd.Flags().StringVar(&dns, "dns", "", "updated DNS of the external client")
  45. extClientUpdateCmd.Flags().StringSliceVar(&allowedips, "allowedips", []string{}, "updated extra allowed IPs of the external client")
  46. rootCmd.AddCommand(extClientUpdateCmd)
  47. }