diff --git a/internal/cli/serverless/list.go b/internal/cli/serverless/list.go index 3d6a798e..ec8ae872 100644 --- a/internal/cli/serverless/list.go +++ b/internal/cli/serverless/list.go @@ -152,7 +152,7 @@ func ListCmd(h *internal.Helper) *cobra.Command { }, } - listCmd.Flags().StringP(flag.Output, flag.OutputShort, output.HumanFormat, "Output format, One of [\"human\" \"json\"], for the complete result, please use json format") + listCmd.Flags().StringP(flag.Output, flag.OutputShort, output.HumanFormat, "Output format, One of [\"human\" \"json\"]. For the complete result, please use json format") listCmd.Flags().StringP(flag.ProjectID, flag.ProjectIDShort, "", "The ID of the project") return listCmd } diff --git a/internal/cli/serverless/regions.go b/internal/cli/serverless/regions.go index ad355f1d..3f8c0a4a 100644 --- a/internal/cli/serverless/regions.go +++ b/internal/cli/serverless/regions.go @@ -15,8 +15,9 @@ package serverless import ( - "encoding/json" "fmt" + "tidbcloud-cli/internal/flag" + "tidbcloud-cli/internal/output" "tidbcloud-cli/internal" "tidbcloud-cli/internal/config" @@ -39,18 +40,46 @@ func RegionsCmd(h *internal.Helper) *cobra.Command { return err } - regions, err := d.ListProviderRegions(serverlessApi.NewServerlessServiceListRegionsParams()) + format, err := cmd.Flags().GetString(flag.Output) if err != nil { return errors.Trace(err) } - v, err := json.MarshalIndent(regions.Payload, "", " ") + + regions, err := d.ListProviderRegions(serverlessApi.NewServerlessServiceListRegionsParams()) if err != nil { return errors.Trace(err) } - fmt.Fprintln(h.IOStreams.Out, string(v)) + if format == output.JsonFormat || !h.IOStreams.CanPrompt { + err = output.PrintJson(h.IOStreams.Out, regions.Payload.Regions) + if err != nil { + return errors.Trace(err) + } + } else if format == output.HumanFormat { + columns := []output.Column{ + "Name", + "DisplayName", + "Provider", + } + + var rows []output.Row + for _, item := range regions.Payload.Regions { + rows = append(rows, output.Row{ + *item.Name, + item.DisplayName, + string(*item.Provider), + }) + } + err = output.PrintHumanTable(h.IOStreams.Out, columns, rows) + if err != nil { + return errors.Trace(err) + } + } else { + return fmt.Errorf("unsupported output format: %s", format) + } return nil }, } + regionsCmd.Flags().StringP(flag.Output, flag.OutputShort, output.HumanFormat, "Output format, One of [\"human\" \"json\"]") return regionsCmd } diff --git a/internal/cli/serverless/update.go b/internal/cli/serverless/update.go index bfc2f663..4b7551e4 100644 --- a/internal/cli/serverless/update.go +++ b/internal/cli/serverless/update.go @@ -74,10 +74,10 @@ func UpdateCmd(h *internal.Helper) *cobra.Command { $ %[1]s serverless update Update displayName of serverless cluster in non-interactive mode: - $ %[1]s serverless update -c --display-name newClusterName, + $ %[1]s serverless update -c --display-name newClusterName - Update labels of serverless cluster in non-interactive mode: - $ %[1]s serverless update -c --labels "{\"label\":\"value2\"}"`, config.CliName), + Update labels of serverless cluster in non-interactive mode: + $ %[1]s serverless update -c --labels "{\"label1\":\"value1\"}"`, config.CliName), PreRunE: func(cmd *cobra.Command, args []string) error { flags := opts.NonInteractiveFlags() for _, fn := range flags { @@ -204,7 +204,7 @@ func UpdateCmd(h *internal.Helper) *cobra.Command { updateCmd.Flags().StringP(flag.ClusterID, flag.ClusterIDShort, "", "The ID of the cluster to be updated") updateCmd.Flags().StringP(flag.DisplayName, flag.DisplayNameShort, "", "The displayName of the cluster") - updateCmd.Flags().String(flag.ServerlessLabels, "", "The label of the cluster.\nInteractive example: {\"label1\":\"value1\",\"label1\":\"value1\"}\nNonInteractive example: \"{\\\"label1\\\":\\\"value1\\\",\\\"label2\\\":\\\"value2\\\"}\"") + updateCmd.Flags().String(flag.ServerlessLabels, "", "The label of the cluster.\nInteractive example: {\"label1\":\"value1\",\"label2\":\"value2\"}\nNonInteractive example: \"{\\\"label1\\\":\\\"value1\\\",\\\"label2\\\":\\\"value2\\\"}\"") updateCmd.Flags().String(flag.ServerlessAnnotations, "", "The annotations of the cluster.\nInteractive example: {\"annotation1\":\"value1\",\"annotation2\":\"value2\"}\nNonInteractive example: \"{\\\"annotation1\\\":\\\"value1\\\",\\\"annotation2\\\":\\\"value2\\\"}\"") return updateCmd }