From c3a66ad01fb785c425359c2b846406b14eeb9f6e Mon Sep 17 00:00:00 2001 From: Finn Date: Sat, 30 Jan 2021 23:23:14 -0800 Subject: [PATCH] Add CSV output formatter --- cmd/signaldctl/cmd/get/account/account.go | 12 ++++++++---- cmd/signaldctl/cmd/get/group/group.go | 12 ++++++++---- cmd/signaldctl/cmd/version.go | 12 ++++++++---- cmd/signaldctl/common/output-formats.go | 9 +++++++++ 4 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 cmd/signaldctl/common/output-formats.go diff --git a/cmd/signaldctl/cmd/get/account/account.go b/cmd/signaldctl/cmd/get/account/account.go index 80c53d0..ceb425e 100644 --- a/cmd/signaldctl/cmd/get/account/account.go +++ b/cmd/signaldctl/cmd/get/account/account.go @@ -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}) } - t.Render() + if common.OutputFormat == common.OutputFormatCSV { + t.RenderCSV() + } else { + t.Render() + } default: log.Fatal("Unsupported output format") } diff --git a/cmd/signaldctl/cmd/get/group/group.go b/cmd/signaldctl/cmd/get/group/group.go index 5dd5f1b..b1d8c90 100644 --- a/cmd/signaldctl/cmd/get/group/group.go +++ b/cmd/signaldctl/cmd/get/group/group.go @@ -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)}) } - t.Render() + if common.OutputFormat == common.OutputFormatCSV { + t.RenderCSV() + } else { + t.Render() + } default: log.Fatal("Unsupported output format") } diff --git a/cmd/signaldctl/cmd/version.go b/cmd/signaldctl/cmd/version.go index 855a82e..e1e4b02 100644 --- a/cmd/signaldctl/cmd/version.go +++ b/cmd/signaldctl/cmd/version.go @@ -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}) - t.Render() + if common.OutputFormat == common.OutputFormatCSV { + t.RenderCSV() + } else { + t.Render() + } default: log.Fatal("Unsupported output format") } diff --git a/cmd/signaldctl/common/output-formats.go b/cmd/signaldctl/common/output-formats.go new file mode 100644 index 0000000..c73af1b --- /dev/null +++ b/cmd/signaldctl/common/output-formats.go @@ -0,0 +1,9 @@ +package common + +const ( + OutputFormatDefault = "default" + OutputFormatCSV = "csv" + OutputFormatTable = "table" + OutputFormatJSON = "json" + OutputFormatYAML = "yaml" +)