Created
January 10, 2026 07:13
-
-
Save diamondburned/15fcfee999be99c8e5148c0e5903e728 to your computer and use it in GitHub Desktop.
Convert Netlify DNS JSON export to a DNS zone file. Fork of https://github.com/Samyak2/netlify-dns-zone-file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "log" | |
| "os" | |
| "strings" | |
| ) | |
| type DnsZone struct { | |
| Id string `json:"id"` | |
| Name string `json:"name"` | |
| Records []DnsRecord `json:"records"` | |
| } | |
| type DnsRecord struct { | |
| ID string `json:"id"` | |
| DnsZoneID string `json:"dns_zone_id"` | |
| Hostname string `json:"hostname"` | |
| Type string `json:"type"` | |
| TTL int `json:"ttl"` | |
| Priority int `json:"priority"` | |
| Weight *int `json:"weight,omitempty"` | |
| Port *int `json:"port,omitempty"` | |
| Flag *string `json:"flag,omitempty"` | |
| Tag *string `json:"tag,omitempty"` | |
| Managed bool `json:"managed"` | |
| Value string `json:"value"` | |
| } | |
| func GenerateZoneFile(zone DnsZone) (string, error) { | |
| records := zone.Records | |
| var zoneFile strings.Builder | |
| zoneFile.WriteString(fmt.Sprintf("$ORIGIN %s\n", zone.Name+".")) | |
| for _, record := range records { | |
| if record.Type == "NETLIFY" { | |
| fmt.Printf("ingoring NETLIFY record: %s\n", record.Hostname) | |
| continue | |
| } | |
| name := record.Hostname + "." | |
| value := record.Value | |
| var priority = "" | |
| if record.Priority != 0 { | |
| priority = fmt.Sprintf("\t%d", record.Priority) | |
| } | |
| zoneFile.WriteString( | |
| fmt.Sprintf( | |
| "%s\tIN\t%d\t%s%s\t%s\n", | |
| name, | |
| record.TTL, | |
| record.Type, | |
| priority, | |
| value, | |
| ), | |
| ) | |
| } | |
| return zoneFile.String(), nil | |
| } | |
| func main() { | |
| netlifyZonesFile, err := os.ReadFile("dns-zones.json") | |
| if err != nil { | |
| log.Fatalln(err) | |
| } | |
| var zones []DnsZone | |
| if err := json.Unmarshal(netlifyZonesFile, &zones); err != nil { | |
| log.Fatalln(err) | |
| } | |
| for _, zone := range zones { | |
| zoneContents, err := GenerateZoneFile(zone) | |
| if err != nil { | |
| log.Fatalln(err) | |
| } | |
| fileName := zone.Id + ".zone" | |
| err = os.WriteFile(fileName, []byte(zoneContents), 0644) | |
| if err != nil { | |
| log.Fatalln(err) | |
| } | |
| fmt.Println(fileName) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment