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

View file

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

View file

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

View file

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