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)
This commit is contained in:
maoyifeng
2026-06-25 16:49:47 +08:00
committed by GitHub
parent 091417980e
commit 6e7aa75e71
3 changed files with 74 additions and 3 deletions

View File

@@ -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)
}

View File

@@ -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)
}
}

View File

@@ -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
}