diff --git a/cmd/apikey/apikey_list.go b/cmd/apikey/apikey_list.go index 91425fb1..013eb5ea 100644 --- a/cmd/apikey/apikey_list.go +++ b/cmd/apikey/apikey_list.go @@ -3,7 +3,6 @@ package apikey import ( "sort" - "github.com/civo/cli/common" "github.com/civo/cli/config" "github.com/civo/cli/utility" "github.com/spf13/cobra" @@ -42,13 +41,6 @@ Example: civo apikey ls -o custom -f "Name: Key"`, } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/database/database_list.go b/cmd/database/database_list.go index 95a84530..ec92cf48 100644 --- a/cmd/database/database_list.go +++ b/cmd/database/database_list.go @@ -61,14 +61,6 @@ var dbListCmd = &cobra.Command{ } } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - fmt.Println("To get the credentials for a database, use `civo db credential `") - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/domain/domain_list.go b/cmd/domain/domain_list.go index 35b1fc67..c400616d 100644 --- a/cmd/domain/domain_list.go +++ b/cmd/domain/domain_list.go @@ -1,7 +1,6 @@ package domain import ( - "github.com/civo/cli/common" "github.com/civo/cli/config" "github.com/civo/cli/utility" @@ -42,13 +41,6 @@ Example: civo domain ls -o custom -f "ID: Name"`, ow.AppendDataWithLabel("name", domain.Name, "Name") } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/domain/domain_record_list.go b/cmd/domain/domain_record_list.go index 112a790d..896a128a 100644 --- a/cmd/domain/domain_record_list.go +++ b/cmd/domain/domain_record_list.go @@ -4,7 +4,6 @@ import ( "os" "strconv" - "github.com/civo/cli/common" "github.com/civo/cli/config" "github.com/civo/cli/utility" "github.com/spf13/cobra" @@ -63,14 +62,7 @@ If you wish to use a custom format, the available fields are: } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/firewall/firewall_list.go b/cmd/firewall/firewall_list.go index 8a49533f..fa7de5fc 100644 --- a/cmd/firewall/firewall_list.go +++ b/cmd/firewall/firewall_list.go @@ -69,13 +69,6 @@ Example: civo firewall ls -o custom -f "ID: Name"`, ow.AppendDataWithLabel("loadbalancer_count", strconv.Itoa(firewall.LoadBalancerCount), "Total LoadBalancer") } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/firewall/firewall_rule_list.go b/cmd/firewall/firewall_rule_list.go index abf9d972..26cfe4c7 100644 --- a/cmd/firewall/firewall_rule_list.go +++ b/cmd/firewall/firewall_rule_list.go @@ -71,13 +71,6 @@ If you wish to use a custom format, the available fields are: ow.AppendDataWithLabel("label", firewallRule.Label, "Label") } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/instance/instance_list.go b/cmd/instance/instance_list.go index 8ca74db2..744480a9 100644 --- a/cmd/instance/instance_list.go +++ b/cmd/instance/instance_list.go @@ -92,13 +92,6 @@ If you wish to use a custom format, the available fields are: } } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/ip/ip_list.go b/cmd/ip/ip_list.go index 8c4349c2..4dde2266 100644 --- a/cmd/ip/ip_list.go +++ b/cmd/ip/ip_list.go @@ -48,13 +48,6 @@ var ipListCmd = &cobra.Command{ } } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/kfcluster/kfcluster_list.go b/cmd/kfcluster/kfcluster_list.go index 505388c4..199ec2a4 100644 --- a/cmd/kfcluster/kfcluster_list.go +++ b/cmd/kfcluster/kfcluster_list.go @@ -43,13 +43,6 @@ var kfcListCmd = &cobra.Command{ ow.AppendDataWithLabel("dashboard_url", kfc.DashboardURL, "Dashboard URL") } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/kubernetes/kubernetes_list.go b/cmd/kubernetes/kubernetes_list.go index d1756520..f0f9422c 100644 --- a/cmd/kubernetes/kubernetes_list.go +++ b/cmd/kubernetes/kubernetes_list.go @@ -76,13 +76,6 @@ If you wish to use a custom format, the available fields are: } } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/loadbalancer/loadbalancer_list.go b/cmd/loadbalancer/loadbalancer_list.go index d72929ec..b581550c 100644 --- a/cmd/loadbalancer/loadbalancer_list.go +++ b/cmd/loadbalancer/loadbalancer_list.go @@ -74,13 +74,6 @@ If you wish to use a custom format, the available fields are: ow.AppendData("Backends", strings.Join(backendList, ", ")) } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/network/network_list.go b/cmd/network/network_list.go index fe1c8762..d1371c4e 100644 --- a/cmd/network/network_list.go +++ b/cmd/network/network_list.go @@ -56,13 +56,6 @@ If you wish to use a custom format, the available fields are: } } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/objectstore/objectstore_credential_list.go b/cmd/objectstore/objectstore_credential_list.go index a845ffdc..4cdbd5c0 100644 --- a/cmd/objectstore/objectstore_credential_list.go +++ b/cmd/objectstore/objectstore_credential_list.go @@ -41,13 +41,6 @@ var objectStoreCredentialListCmd = &cobra.Command{ ow.AppendDataWithLabel("status", credential.Status, "Status") } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/objectstore/objectstore_list.go b/cmd/objectstore/objectstore_list.go index 0e4fe548..f823c9bd 100644 --- a/cmd/objectstore/objectstore_list.go +++ b/cmd/objectstore/objectstore_list.go @@ -46,13 +46,6 @@ var objectStoreListCmd = &cobra.Command{ ow.AppendDataWithLabel("status", objectStore.Status, "Status") } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/sshkey/ssh_key_list.go b/cmd/sshkey/ssh_key_list.go index a6cd6d11..13d8072a 100644 --- a/cmd/sshkey/ssh_key_list.go +++ b/cmd/sshkey/ssh_key_list.go @@ -3,7 +3,6 @@ package sshkey import ( "os" - "github.com/civo/cli/common" "github.com/civo/cli/config" "github.com/civo/cli/utility" "github.com/spf13/cobra" @@ -44,13 +43,6 @@ Example: civo ssh ls -o custom -f "id: name"`, ow.AppendDataWithLabel("fingerprint", sshkey.Fingerprint, "Finger Print") } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/cmd/volume/volume_list.go b/cmd/volume/volume_list.go index 19f3d3d2..20372480 100644 --- a/cmd/volume/volume_list.go +++ b/cmd/volume/volume_list.go @@ -143,13 +143,6 @@ Example: civo volume ls -o custom -f "ID: Name (SizeGigabytes)`, ow.AppendDataWithLabel("status", volume.Status, "Status") } - switch common.OutputFormat { - case "json": - ow.WriteMultipleObjectsJSON(common.PrettySet) - case "custom": - ow.WriteCustomOutput(common.OutputFields) - default: - ow.WriteTable() - } + ow.FinishAndPrintOutput() }, } diff --git a/utility/output_writer.go b/utility/output_writer.go index a1723c5c..a0974cc4 100644 --- a/utility/output_writer.go +++ b/utility/output_writer.go @@ -6,6 +6,8 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/civo/cli/common" + "github.com/civo/cli/config" "os" "regexp" "sort" @@ -280,3 +282,20 @@ func prettyprint(b []byte) ([]byte, error) { err := json.Indent(&out, b, "", " ") return out.Bytes(), err } + +func (ow *OutputWriter) FinishAndPrintOutput() { + ow.finishExistingLine() + if len(ow.Values) == 0 { + region := config.Current.Meta.DefaultRegion // Ensure this fetches the correct current region. + fmt.Fprintf(os.Stderr, "No resources found in region %s. For a list of regions use the command 'civo region ls'\n", region) + } else { + switch common.OutputFormat { + case "json": + ow.WriteMultipleObjectsJSON(common.PrettySet) + case "custom": + ow.WriteCustomOutput(common.OutputFields) + default: + ow.WriteTable() + } + } +}