Skip to content

Commit

Permalink
Change OutputWriter to use labels as well as keys
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjeffries committed Mar 3, 2020
1 parent e15de86 commit 52eeee2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 125 deletions.
78 changes: 28 additions & 50 deletions cmd/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,22 @@ Example: civo instance ls -o custom -f "ID: Name (PublicIP)"`,
for _, instance := range instances {
ow.StartLine()

ow.AppendData("ID", instance.ID)
ow.AppendData("Hostname", instance.Hostname)
ow.AppendData("Size", instance.Size)
ow.AppendData("Region", instance.Region)
ow.AppendDataWithLabel("PublicIP", instance.PublicIP, "Public IP")
ow.AppendData("Status", instance.Status)

if OutputFormat == "json" || OutputFormat == "custom" {
ow.AppendData("ID", instance.ID)
ow.AppendData("OpenstackServerID", instance.OpenstackServerID)
ow.AppendData("Hostname", instance.Hostname)
ow.AppendData("Size", instance.Size)
ow.AppendData("Region", instance.Region)
ow.AppendData("NetworkID", instance.NetworkID)
ow.AppendData("PrivateIP", instance.PrivateIP)
ow.AppendData("PublicIP", instance.PublicIP)
ow.AppendData("PseudoIP", instance.PseudoIP)
ow.AppendData("TemplateID", instance.TemplateID)
ow.AppendData("SnapshotID", instance.SnapshotID)
ow.AppendData("InitialUser", instance.InitialUser)
ow.AppendData("SSHKey", instance.SSHKey)
ow.AppendData("Status", instance.Status)
ow.AppendData("Notes", instance.Notes)
ow.AppendData("FirewallID", instance.FirewallID)
ow.AppendData("Tags", strings.Join(instance.Tags, " "))
Expand All @@ -95,13 +96,6 @@ Example: civo instance ls -o custom -f "ID: Name (PublicIP)"`,
ow.AppendData("PrivateIP", instance.PrivateIP)
ow.AppendData("PublicIP", instance.PublicIP)
ow.AppendData("PseudoIP", instance.PseudoIP)
} else {
ow.AppendData("ID", instance.ID)
ow.AppendData("Hostname", instance.Hostname)
ow.AppendData("Size", instance.Size)
ow.AppendData("Region", instance.Region)
ow.AppendData("Public IP", instance.PublicIP)
ow.AppendData("Status", instance.Status)
}
}

Expand Down Expand Up @@ -166,31 +160,29 @@ Example: civo instance show ID/NAME -o custom -f "Key1: Key2"`,
ow := utility.NewOutputWriter()
ow.StartLine()

ow.AppendData("ID", instance.ID)
ow.AppendDataWithLabel("OpenstackServerID", instance.OpenstackServerID, "Openstack Server ID")
ow.AppendData("Hostname", instance.Hostname)
ow.AppendData("Status", instance.Status)
ow.AppendData("Size", instance.Size)
ow.AppendData("Region", instance.Region)
ow.AppendDataWithLabel("NetworkID", instance.NetworkID, "Network ID")
ow.AppendDataWithLabel("TemplateID", instance.TemplateID, "Template ID")
ow.AppendDataWithLabel("SnapshotID", instance.SnapshotID, "Snapshot ID")
ow.AppendDataWithLabel("InitialUser", instance.InitialUser, "Initial User")
ow.AppendDataWithLabel("SSHKey", instance.SSHKey, "SSH Key")
ow.AppendDataWithLabel("FirewallID", instance.FirewallID, "Firewall ID")
ow.AppendData("Tags", strings.Join(instance.Tags, " "))
ow.AppendDataWithLabel("CreatedAt", instance.CreatedAt.Format(time.RFC1123), "Created At")
ow.AppendDataWithLabel("PrivateIP", instance.PrivateIP, "Private IP")

if OutputFormat == "json" || OutputFormat == "custom" {
ow.AppendData("ID", instance.ID)
ow.AppendData("OpenstackServerID", instance.OpenstackServerID)
ow.AppendData("Hostname", instance.Hostname)
ow.AppendData("Size", instance.Size)
ow.AppendData("Region", instance.Region)
ow.AppendData("NetworkID", instance.NetworkID)
ow.AppendData("PrivateIP", instance.PrivateIP)
ow.AppendData("PublicIP", instance.PublicIP)
ow.AppendData("PseudoIP", instance.PseudoIP)
ow.AppendData("TemplateID", instance.TemplateID)
ow.AppendData("SnapshotID", instance.SnapshotID)
ow.AppendData("InitialUser", instance.InitialUser)
ow.AppendData("SSHKey", instance.SSHKey)
ow.AppendData("Status", instance.Status)
ow.AppendDataWithLabel("PublicIP", instance.PublicIP, "Public IP")
ow.AppendDataWithLabel("PseudoIP", instance.PseudoIP, "Pseudo IP")
ow.AppendData("Notes", instance.Notes)
ow.AppendData("FirewallID", instance.FirewallID)
ow.AppendData("Tags", strings.Join(instance.Tags, " "))
ow.AppendData("CivostatsdToken", instance.CivostatsdToken)
ow.AppendData("CivostatsdStats", instance.CivostatsdStats)
ow.AppendData("Script", instance.Script)
ow.AppendData("CreatedAt", instance.CreatedAt.Format(time.RFC1123))

ow.AppendData("ReverseDNS", instance.ReverseDNS)
ow.AppendData("PrivateIP", instance.PrivateIP)
ow.AppendData("PublicIP", instance.PublicIP)
ow.AppendData("PseudoIP", instance.PseudoIP)
if OutputFormat == "json" {
Expand All @@ -199,29 +191,15 @@ Example: civo instance show ID/NAME -o custom -f "Key1: Key2"`,
ow.WriteCustomOutput(OutputFields)
}
} else {
ow.AppendData("ID", instance.ID)
ow.AppendData("Openstack Server ID", instance.OpenstackServerID)
ow.AppendData("Hostname", instance.Hostname)
if instance.Hostname != instance.ReverseDNS && instance.ReverseDNS != "" {
ow.AppendData("Reverse DNS", instance.ReverseDNS)
}
ow.AppendData("Size", instance.Size)
ow.AppendData("Region", instance.Region)
ow.AppendData("Network ID", instance.NetworkID)
if instance.PseudoIP != "" {
PublicIP := fmt.Sprintf("%s => %s", instance.PseudoIP, instance.PublicIP)
ow.AppendData("Public IP", PublicIP)
} else {
ow.AppendData("Public IP", instance.PublicIP)
}
ow.AppendData("Template ID", instance.TemplateID)
ow.AppendData("Snapshot ID", instance.SnapshotID)
ow.AppendData("Initial User", instance.InitialUser)
ow.AppendData("SSH Key", instance.SSHKey)
ow.AppendData("Status", instance.Status)
ow.AppendData("Firewall ID", instance.FirewallID)
ow.AppendData("Tags", strings.Join(instance.Tags, " "))
ow.AppendData("Created At", instance.CreatedAt.Format(time.RFC1123))
if instance.Hostname != instance.ReverseDNS && instance.ReverseDNS != "" {
ow.AppendData("Reverse DNS", instance.ReverseDNS)
}

ow.WriteKeyValues()

Expand Down
92 changes: 17 additions & 75 deletions utility/output_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
// ow.WriteTable()
type OutputWriter struct {
Keys []string
Labels []string
Values [][]string
TempValues []string
}
Expand Down Expand Up @@ -62,8 +63,8 @@ func (ow *OutputWriter) finishExistingLine() {
}
}

// AppendData adds a line of data to the output writer
func (ow *OutputWriter) AppendData(key, value string) {
// AppendDataWithLabel adds a line of data to the output writer
func (ow *OutputWriter) AppendDataWithLabel(key, value, label string) {
found := -1
for i, v := range ow.Keys {
if v == key {
Expand All @@ -73,12 +74,18 @@ func (ow *OutputWriter) AppendData(key, value string) {

if found == -1 {
ow.Keys = append(ow.Keys, key)
ow.Labels = append(ow.Labels, label)
ow.TempValues = append(ow.TempValues, value)
} else {
ow.TempValues[found] = value
}
}

// AppendData adds a line of data to the output writer
func (ow *OutputWriter) AppendData(key, value string) {
ow.AppendDataWithLabel(key, value, key)
}

// WriteSingleObjectJSON writes the JSON for a single object to STDOUT
func (ow *OutputWriter) WriteSingleObjectJSON() {
ow.finishExistingLine()
Expand Down Expand Up @@ -126,15 +133,16 @@ func (ow *OutputWriter) WriteKeyValues() {
ow.finishExistingLine()

longestLabelLength := 0
for _, key := range ow.Keys {
if len(key) > longestLabelLength {
longestLabelLength = len(key)
for _, label := range ow.Labels {
if len(label) > longestLabelLength {
longestLabelLength = len(label)
}
}

for i, key := range ow.Keys {
for i := range ow.Keys {
value := ow.Values[0][i]
fmt.Printf("%"+strconv.Itoa(longestLabelLength)+"s : %s\n", key, value)
label := ow.Labels[i]
fmt.Printf("%"+strconv.Itoa(longestLabelLength)+"s : %s\n", label, value)
}
}

Expand All @@ -145,7 +153,7 @@ func (ow *OutputWriter) WriteTable() {

table := tablewriter.NewWriter(os.Stdout)
if len(ow.Keys) > 0 {
table.SetHeader(ow.Keys)
table.SetHeader(ow.Labels)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(false)
Expand All @@ -159,6 +167,7 @@ func (ow *OutputWriter) WriteTable() {

// WriteCustomOutput prints one or multiple objects using custom formatting
func (ow *OutputWriter) WriteCustomOutput(fields string) {
ow.finishExistingLine()
for _, item := range ow.Values {
output := fields
for index, name := range ow.Keys {
Expand All @@ -177,70 +186,3 @@ func (ow *OutputWriter) WriteSubheader(label string) {
count := (72 - len(label) + 2) / 2
fmt.Println(strings.Repeat("-", count) + " " + label + " " + strings.Repeat("-", count))
}

// package cmd

// import (
// "fmt"
// "os"
// "reflect"
// "strconv"
// "strings"

// "github.com/olekukonko/tablewriter"
// )

// func outputTable(headers []string, data [][]string) {
// if OutputFormat == "custom" {
// for _, items := range data {
// output := OutputFields
// for index, name := range headers {
// if strings.Contains(output, name) {
// output = strings.Replace(output, name, items[index], 1)
// }
// }
// output = strings.Replace(output, "\\t", "\t", -1)
// output = strings.Replace(output, "\\n", "\n", -1)
// fmt.Println(output)
// }
// } else if OutputFormat == "table" || OutputFormat == "human" || OutputFormat == "" {
// table := tablewriter.NewWriter(os.Stdout)
// if len(headers) > 0 {
// table.SetHeader(headers)
// table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
// table.SetAutoWrapText(false)
// table.SetAutoFormatHeaders(false)
// } else {
// table.SetBorder(false)
// }
// table.AppendBulk(data)
// table.Render()
// }
// }

// func outputKeyValue(keys []string, data map[string]string) {
// if OutputFormat == "custom" {
// output := OutputFields
// for _, key := range keys {
// value := data[key]
// if strings.Contains(output, key) {
// output = strings.Replace(output, key, value, 1)
// }
// output = strings.Replace(output, "\\t", "\t", -1)
// output = strings.Replace(output, "\\n", "\n", -1)
// }
// fmt.Println(output)
// } else if OutputFormat == "table" || OutputFormat == "human" || OutputFormat == "" {
// longestLabelLength := 0
// for key := range data {
// if len(key) > longestLabelLength {
// longestLabelLength = len(key)
// }
// }

// for _, key := range keys {
// value := data[key]
// fmt.Printf("%"+strconv.Itoa(longestLabelLength)+"s : %s\n", key, value)
// }
// }
// }

0 comments on commit 52eeee2

Please sign in to comment.