From 6e7aa75e7116da88746d767ff8f102a1c31a5efd Mon Sep 17 00:00:00 2001 From: maoyifeng Date: Thu, 25 Jun 2026 16:49:47 +0800 Subject: [PATCH] Go:CLI add new response function (#16347) ### What problem does this PR solve? add new response function ### Type of change - [ ] New Feature (non-breaking change which adds functionality) --- internal/cli/admin_command.go | 6 ++--- internal/cli/response.go | 42 +++++++++++++++++++++++++++++++++++ internal/cli/table.go | 29 ++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/internal/cli/admin_command.go b/internal/cli/admin_command.go index 90307c5be1..29cdf69a15 100644 --- a/internal/cli/admin_command.go +++ b/internal/cli/admin_command.go @@ -1972,7 +1972,7 @@ func (c *CLI) AdminShowUserSummaryCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to get user summary: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) } - var result CommonDataResponse + var result OrderedCommonDataResponse if err = json.Unmarshal(resp.Body, &result); err != nil { return nil, fmt.Errorf("get user summary failed: invalid JSON (%w)", err) } @@ -2191,7 +2191,7 @@ func (c *CLI) AdminShowUsersSummaryCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to get users summary: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) } - var result CommonDataResponse + var result OrderedCommonDataResponse if err = json.Unmarshal(resp.Body, &result); err != nil { return nil, fmt.Errorf("get users summary failed: invalid JSON (%w)", err) } @@ -2349,7 +2349,7 @@ func (c *CLI) AdminListUsersConditionCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list users: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) } - var result CommonResponse + var result OrderedCommonResponse if err = json.Unmarshal(resp.Body, &result); err != nil { return nil, fmt.Errorf("list users failed: invalid JSON (%w)", err) } diff --git a/internal/cli/response.go b/internal/cli/response.go index fbc121a20a..d9dafd254b 100644 --- a/internal/cli/response.go +++ b/internal/cli/response.go @@ -1081,3 +1081,45 @@ func (r *UserQuotaResponse) PrintOut() { PrintTableSimpleByFormatWithOrder(summaryTable, []string{"Metric", "Used", "Limit"}, r.OutputFormat) } } + +type OrderedCommonResponse struct { + CommonResponse +} + +func (r *OrderedCommonResponse) PrintOut() { + if r.Code == 0 { + if colNames, cleanData, ok := ExtractColumnsAndCleanData(r.Data); ok { + PrintTableSimpleByFormatWithOrder(cleanData, colNames, r.OutputFormat) + } else { + PrintTableSimpleByFormat(r.Data, r.OutputFormat) + } + } else { + fmt.Println("ERROR") + fmt.Printf("%d, %s\n", r.Code, r.Message) + } +} + +type OrderedCommonDataResponse struct { + CommonDataResponse +} + +func (r *OrderedCommonDataResponse) PrintOut() { + if r.Code == 0 { + if table := r.orderedMetricTable(); len(table) > 0 { + PrintTableSimpleByFormat(table, r.OutputFormat) + } else { + table := make([]map[string]interface{}, 0) + for key, value := range r.Data { + elem := map[string]interface{}{ + "field": key, + "value": value, + } + table = append(table, elem) + } + PrintTableSimpleByFormat(table, r.OutputFormat) + } + } else { + fmt.Println("ERROR") + fmt.Printf("%d, %s\n", r.Code, r.Message) + } +} diff --git a/internal/cli/table.go b/internal/cli/table.go index 494c2bbe18..cafb955f0d 100644 --- a/internal/cli/table.go +++ b/internal/cli/table.go @@ -404,3 +404,32 @@ func isHalfWidth(r rune) bool { } return false } + +func ExtractColumnsAndCleanData(data []map[string]interface{}) ([]string, []map[string]interface{}, bool) { + if len(data) == 0 { + return nil, nil, false + } + columnsRaw, ok := data[0]["_columns"] + if !ok { + return nil, nil, false + } + columns, ok := columnsRaw.([]interface{}) + if !ok { + return nil, nil, false + } + colNames := make([]string, 0, len(columns)) + for _, c := range columns { + colNames = append(colNames, fmt.Sprintf("%v", c)) + } + cleanData := make([]map[string]interface{}, 0, len(data)) + for _, row := range data { + cleanRow := make(map[string]interface{}, len(row)) + for k, v := range row { + if k != "_columns" { + cleanRow[k] = v + } + } + cleanData = append(cleanData, cleanRow) + } + return colNames, cleanData, true +}