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