zones.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package cmd
  2. import (
  3. "github.com/spf13/cobra"
  4. "schlundtech-dns/internal/output"
  5. )
  6. var zonesCmd = &cobra.Command{
  7. Use: "zones",
  8. Short: "List the zones the account can see",
  9. Long: `List every DNS zone the account has access to, with the name server that
  10. serves it.
  11. This is also the quickest way to check that credentials, context and network
  12. path are all working: it is read-only and touches nothing.
  13. The name server shown is the value the gateway requires when changing
  14. records, so this is what the write commands rely on.`,
  15. Example: ` # every zone
  16. schlundtech-dns zones
  17. # one zone per line, for scripting
  18. schlundtech-dns zones --output json`,
  19. Args: cobra.NoArgs,
  20. RunE: func(cmd *cobra.Command, _ []string) error {
  21. f, err := format()
  22. if err != nil {
  23. return err
  24. }
  25. c, err := client()
  26. if err != nil {
  27. return err
  28. }
  29. zones, err := c.Zones(cmd.Context())
  30. if err != nil {
  31. return err
  32. }
  33. rows := make([][]string, 0, len(zones))
  34. for _, z := range zones {
  35. rows = append(rows, []string{z.Name, z.SystemNS})
  36. }
  37. return output.Write(cmd.OutOrStdout(), f, []string{"ZONE", "NAMESERVER"}, rows, zones)
  38. },
  39. }
  40. func init() {
  41. rootCmd.AddCommand(zonesCmd)
  42. }