Refactor main.go to make easier to read

master
Buddy Sandidge 8 years ago
parent d0d4730c64
commit f77229703a

@ -108,61 +108,32 @@ func action(context *cli.Context) error {
kind := context.Generic("type").(*typeFlag).value
ttl := context.Int("ttl")
wg.Add(1)
ipClient := &ip.Client{
URLv4: context.String("get-ipv4-url"),
URLv6: context.String("get-ipv6-url"),
}
var getIPError error
var content string
var getContentError error
wg.Add(1)
go func() {
defer wg.Done()
var err error
result := context.String("content")
switch kind {
case "A":
if result == "" {
result, err = ipClient.GetIPv4()
}
case "AAAA":
if result == "" {
result, err = ipClient.GetIPv6()
}
default:
if result == "" {
err = errors.Errorf("could not get content for type %s", kind)
return
}
}
if err != nil {
getIPError = errors.Wrap(err, "could not get IP address")
} else {
content = result
ipClient := &ip.Client{
URLv4: context.String("get-ipv4-url"),
URLv6: context.String("get-ipv6-url"),
}
content, getContentError = getContent(ipClient, kind, context.String("content"))
}()
client := dns.New(context.String("dnsimple-token"))
dnsClient := dns.New(context.String("dnsimple-token"))
records := []dns.Record{}
var getRecordsError error
wg.Add(1)
go func() {
defer wg.Done()
if results, err := client.GetRecords(host, kind); err != nil {
getRecordsError = errors.Wrap(err, "could not get records for host")
} else {
records = results
}
records, getRecordsError = dnsClient.GetRecords(host, kind)
}()
wg.Wait()
if getIPError != nil {
return errors.Wrap(getIPError, "could no get ip address")
if getContentError != nil {
return errors.Wrap(getContentError, "could no get ip address")
}
if getRecordsError != nil {
@ -175,7 +146,7 @@ func action(context *cli.Context) error {
}
newRecord := dns.Record{Type: kind, Content: content, TTL: ttl}
if err := client.CreateRecord(host, newRecord); err != nil {
if err := dnsClient.CreateRecord(host, newRecord); err != nil {
return errors.Wrap(err, "could not create record")
}
@ -187,10 +158,25 @@ func action(context *cli.Context) error {
go func(r dns.Record) {
defer deleteWG.Done()
fmt.Printf("%s record for %s being deleting %s\n", r.Type, host, r.Content)
client.DeleteRecord(r)
dnsClient.DeleteRecord(r)
}(record)
}
deleteWG.Wait()
return nil
}
func getContent(client *ip.Client, kind, content string) (string, error) {
if content != "" {
return content, nil
}
switch kind {
case "A":
return client.GetIPv4()
case "AAAA":
return client.GetIPv6()
default:
return "", errors.Errorf("could not get content for type %s", kind)
}
}

Loading…
Cancel
Save