support printing records to stdout

This commit is contained in:
Balakrishnan Balasubramanian 2024-03-31 12:06:21 -04:00
parent 0a0c8474b8
commit c2fc6d587d

14
main.go
View File

@ -112,7 +112,7 @@ func main() {
flag.StringVar(&token, "a", token, "Cloudflare API Token. env var: CF_TOKEN") flag.StringVar(&token, "a", token, "Cloudflare API Token. env var: CF_TOKEN")
flag.TextVar(&ip, "i", ip, "IP address") flag.TextVar(&ip, "i", ip, "IP address")
flag.StringVar(&cname, "c", cname, "CNAME target") flag.StringVar(&cname, "c", cname, "CNAME target")
flag.StringVar(&path, "o", path, "Path to save all records as json, e.g. ./records.json") flag.StringVar(&path, "o", path, "Path to save all records as json, e.g. ./records.json, '-' for stdout")
flag.BoolVar(&del, "x", del, "Delete records of subdomain") flag.BoolVar(&del, "x", del, "Delete records of subdomain")
flag.BoolVar(&mx, "m", mx, "Set mx record with cname value") flag.BoolVar(&mx, "m", mx, "Set mx record with cname value")
flag.Parse() flag.Parse()
@ -169,15 +169,21 @@ func main() {
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
} }
data, err := json.Marshal(recs)
data, err := json.Marshal(recs)
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
} }
err = os.WriteFile(path, data, 0644)
if err != nil { if path == "-" {
if _, err = os.Stdout.Write(data); err != nil {
log.Panicln(err) log.Panicln(err)
} }
} else {
if err = os.WriteFile(path, data, 0644); err != nil {
log.Panicln(err)
}
}
} }
} }