Add CSV output formatter

This commit is contained in:
Finn 2021-01-30 23:23:14 -08:00
parent 9284d35b80
commit c3a66ad01f
4 changed files with 33 additions and 12 deletions

View file

@ -57,17 +57,17 @@ var (
}
switch common.OutputFormat {
case "json":
case common.OutputFormatJSON:
err := json.NewEncoder(os.Stdout).Encode(accounts)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case "yaml":
case common.OutputFormatYAML:
err := yaml.NewEncoder(os.Stdout).Encode(accounts)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case "table":
case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault:
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Phone Number", "UUID", "Device ID", "Subscribed"})
@ -76,7 +76,11 @@ var (
t.AppendRow(table.Row{account.Username, account.UUID, account.DeviceID, account.Subscribed})
}
if common.OutputFormat == common.OutputFormatCSV {
t.RenderCSV()
} else {
t.Render()
}
default:
log.Fatal("Unsupported output format")
}

View file

@ -67,17 +67,17 @@ var (
}
switch common.OutputFormat {
case "json":
case common.OutputFormatJSON:
err := json.NewEncoder(os.Stdout).Encode(resp)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case "yaml":
case common.OutputFormatYAML:
err := yaml.NewEncoder(os.Stdout).Encode(resp)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case "table":
case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault:
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"ID", "Title", "Members"})
@ -90,7 +90,11 @@ var (
t.AppendRow(table.Row{group.GroupId, group.Name, len(group.Members)})
}
if common.OutputFormat == common.OutputFormatCSV {
t.RenderCSV()
} else {
t.Render()
}
default:
log.Fatal("Unsupported output format")
}

View file

@ -51,23 +51,27 @@ var versionCmd = &cobra.Command{
},
}
switch common.OutputFormat {
case "json":
case common.OutputFormatJSON:
err := json.NewEncoder(os.Stdout).Encode(output)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case "yaml":
case common.OutputFormatYAML:
err := yaml.NewEncoder(os.Stdout).Encode(output)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case "table":
case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault:
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Name", "Version", "Branch", "Commit"})
t.AppendRow(table.Row{common.Name, common.Version, common.Branch, common.Commit})
t.AppendRow(table.Row{response.Name, response.Version, response.Branch, response.Commit})
if common.OutputFormat == common.OutputFormatCSV {
t.RenderCSV()
} else {
t.Render()
}
default:
log.Fatal("Unsupported output format")
}

View file

@ -0,0 +1,9 @@
package common
const (
OutputFormatDefault = "default"
OutputFormatCSV = "csv"
OutputFormatTable = "table"
OutputFormatJSON = "json"
OutputFormatYAML = "yaml"
)