Skip to content

Commit

Permalink
Merge pull request #13 from mrngm/issue-8
Browse files Browse the repository at this point in the history
Add formatting options for ZoneRecord
  • Loading branch information
jrwren committed Oct 27, 2018
2 parents 84d94fa + 7434792 commit 951e17a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
59 changes: 59 additions & 0 deletions cmd/domasimu/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"encoding/json"
"fmt"
"strings"

"github.com/dnsimple/dnsimple-go/dnsimple"
)

// FormatZoneRecord formats a dnsimple.ZoneRecord in the specified format, or returns an error when it failed.
func FormatZoneRecord(zone dnsimple.ZoneRecord, format string) (string, error) {
switch format {
case "plain":
return fmt.Sprintf("%s %s (%d) %s\n", zone.Name, zone.Type, zone.TTL, zone.Content), nil
case "json":
enc, err := json.Marshal(zone)
if err != nil {
return "", err
}
return string(enc), nil
case "table":
return fmt.Sprintf("| %-30s | %-5s | %7d | %-30s |\n", zone.Name, zone.Type, zone.TTL, zone.Content), nil
default:
return "", fmt.Errorf("invalid format %v", format)
}

}

// FormatZoneRecords takes a slice of dnsimple.ZoneRecord and formats it in the specified format.
func FormatZoneRecords(zones []dnsimple.ZoneRecord, format string) (string, error) {
if format == "json" {
enc, err := json.Marshal(zones)
if err != nil {
return "", err
}
return string(enc), nil
}

var ret string
if format == "table" {
ret = fmt.Sprintf("+-%-30s-+-%-5s-+-%-7s-+-%-30s-+\n", strings.Repeat("-", 30), "-----", "-------", strings.Repeat("-", 30))
ret = fmt.Sprintf("%s| %-30s | %-5s | %-7s | %-30s |\n", ret, "Name", "Type", "TTL", "Content")
ret = fmt.Sprintf("%s+-%-30s-+-%-5s-+-%-7s-+-%-30s-+\n", ret, strings.Repeat("-", 30), "-----", "-------", strings.Repeat("-", 30))
}

for _, zone := range zones {
if formatted, err := FormatZoneRecord(zone, format); err != nil {
return "", fmt.Errorf("error formatting zone %v: %v", zone, err)
} else {
ret = fmt.Sprintf("%s%s", ret, formatted)
}
}
if format == "table" {
ret = fmt.Sprintf("%s+-%-30s-+-%-5s-+-%-7s-+-%-30s-+\n", ret, strings.Repeat("-", 30), "-----", "-------", strings.Repeat("-", 30))
}

return ret, nil
}
18 changes: 11 additions & 7 deletions cmd/domasimu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var verbose = flag.Bool("v", false, "Use verbose output")
var list = flag.Bool("l", false, "List domains.")
var update = flag.String("u", "", "Update or create record. The format is 'domain name type oldvalue newvlaue ttl'.\n(use - for oldvalue to create a new record)")
var del = flag.String("d", "", "Delete record. The format is 'domain name type value'")
var format = flag.String("f", "plain", "Output zones in {plain, json, table} format")

func main() {
flag.Usage = func() {
Expand All @@ -35,6 +36,15 @@ func main() {
}
flag.Parse()

switch *format {
case "plain":
case "table":
case "json":
default:
fmt.Fprintln(os.Stderr, "could not use specified format", *format)
return
}

if len(os.Args) == 1 {
flag.Usage()
return
Expand Down Expand Up @@ -103,13 +113,7 @@ func main() {
continue
}

for _, record := range listZoneRecordsResponse.Data {
if *verbose {
fmt.Println(record.Name, record.Type, record.Content, record.TTL, record.Priority)
} else {
fmt.Println(record.Name, record.Type, record.Content)
}
}
fmt.Println(FormatZoneRecords(listZoneRecordsResponse.Data, *format))
}
}

Expand Down

0 comments on commit 951e17a

Please sign in to comment.