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