From 9c5981acc385b7b0a905141747f488e68f3505f0 Mon Sep 17 00:00:00 2001 From: Gerdriaan Mulder Date: Sat, 27 Oct 2018 18:08:28 +0200 Subject: [PATCH 1/2] Add formatting options for ZoneRecord --- cmd/domasimu/format.go | 59 ++++++++++++++++++++++++++++++++++++++++++ cmd/domasimu/main.go | 18 ++++++++----- 2 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 cmd/domasimu/format.go diff --git a/cmd/domasimu/format.go b/cmd/domasimu/format.go new file mode 100644 index 0000000..cf07aaa --- /dev/null +++ b/cmd/domasimu/format.go @@ -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 | %7s | %-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 +} diff --git a/cmd/domasimu/main.go b/cmd/domasimu/main.go index 683d000..e019be4 100644 --- a/cmd/domasimu/main.go +++ b/cmd/domasimu/main.go @@ -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() { @@ -35,6 +36,15 @@ func main() { } flag.Parse() + switch *format { + case "plain": + case "table": + case "json": + default: + fmt.Println(os.Stderr, "could not use specified format", *format) + return + } + if len(os.Args) == 1 { flag.Usage() return @@ -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)) } } From 74347921ca36e53fa02c9d8ece7315043aed9423 Mon Sep 17 00:00:00 2001 From: Gerdriaan Mulder Date: Sat, 27 Oct 2018 18:31:34 +0200 Subject: [PATCH 2/2] Fix Sprintf and Fprintln issue --- cmd/domasimu/format.go | 2 +- cmd/domasimu/main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/domasimu/format.go b/cmd/domasimu/format.go index cf07aaa..ffd5d2e 100644 --- a/cmd/domasimu/format.go +++ b/cmd/domasimu/format.go @@ -20,7 +20,7 @@ func FormatZoneRecord(zone dnsimple.ZoneRecord, format string) (string, error) { } return string(enc), nil case "table": - return fmt.Sprintf("| %-30s | %-5s | %7s | %-30s |\n", zone.Name, zone.Type, zone.TTL, zone.Content), nil + 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) } diff --git a/cmd/domasimu/main.go b/cmd/domasimu/main.go index e019be4..5aeac58 100644 --- a/cmd/domasimu/main.go +++ b/cmd/domasimu/main.go @@ -41,7 +41,7 @@ func main() { case "table": case "json": default: - fmt.Println(os.Stderr, "could not use specified format", *format) + fmt.Fprintln(os.Stderr, "could not use specified format", *format) return }