refactor and log

This commit is contained in:
Balakrishnan Balasubramanian 2023-03-23 15:30:55 -04:00
parent 89a8a36b51
commit 48514a61be

49
main.go
View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"flag"
"fmt"
"log"
"net"
"os"
@ -35,40 +36,38 @@ func main() {
provider := cloudflare.Provider{APIToken: token}
zone := domain + "."
fmt.Println(zone, sub, cname)
setRecords := func() ([]libdns.Record, error) {
makeRecord := func() (libdns.Record, error) {
switch {
case cname != "":
return provider.SetRecords(ctx, zone, []libdns.Record{
{
Type: "CNAME",
Name: sub,
Value: cname,
},
})
return libdns.Record{
Type: "CNAME",
Name: sub,
Value: cname,
}, nil
case len(ip) == net.IPv4len:
return provider.SetRecords(ctx, zone, []libdns.Record{
{
Type: "A",
Name: sub,
Value: ip.To4().String(),
},
})
return libdns.Record{
Type: "A",
Name: sub,
Value: ip.To4().String(),
}, nil
case len(ip) == net.IPv6len:
return provider.SetRecords(ctx, zone, []libdns.Record{
{
Type: "AAAA",
Name: sub,
Value: ip.To16().String(),
},
})
return libdns.Record{
Type: "AAAA",
Name: sub,
Value: ip.To16().String(),
}, nil
}
return nil, fmt.Errorf("neither cname nor valid ip is set")
return libdns.Record{}, fmt.Errorf("neither cname nor valid ip is set")
}
if sub != "<UNSET>" {
newRecs, err := setRecords()
record, err := makeRecord()
if err != nil {
panic(err)
}
log.Printf("setting record, %+v", record)
newRecs, err := provider.SetRecords(ctx, zone, []libdns.Record{record})
if err != nil {
fmt.Printf("%#v\n", err)
panic(err)
}
fmt.Println(newRecs)