Skip to content

Commit

Permalink
Merge pull request #70 from yjinjo/v0
Browse files Browse the repository at this point in the history
Add column and limit feature
  • Loading branch information
yjinjo authored Dec 5, 2024
2 parents 1632c81 + 3ba20e8 commit eebc98c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
66 changes: 64 additions & 2 deletions cmd/common/fetchService.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,45 @@ func FetchService(serviceName string, verb string, resourceName string, options
case float64:
return v < jVal.(float64)
case bool:
return !v && jVal.(bool)
return v && !jVal.(bool)
default:
return false
}
})
respMap["results"] = results
}
}

// Apply limit if specified
if options.Limit > 0 && verb == "list" {
if results, ok := respMap["results"].([]interface{}); ok {
if len(results) > options.Limit {
respMap["results"] = results[:options.Limit]
}
}
}

// Filter columns if specified
if options.Columns != "" && verb == "list" {
if results, ok := respMap["results"].([]interface{}); ok {
columns := strings.Split(options.Columns, ",")
filteredResults := make([]interface{}, len(results))

for i, result := range results {
if resultMap, ok := result.(map[string]interface{}); ok {
filteredMap := make(map[string]interface{})
for _, col := range columns {
if val, exists := resultMap[strings.TrimSpace(col)]; exists {
filteredMap[strings.TrimSpace(col)] = val
}
}
filteredResults[i] = filteredMap
}
}
respMap["results"] = filteredResults
}
}

printData(respMap, options, serviceName, resourceName, refClient)
}

Expand Down Expand Up @@ -299,14 +330,45 @@ func FetchService(serviceName string, verb string, resourceName string, options
case float64:
return v < jVal.(float64)
case bool:
return !v && jVal.(bool)
return v && !jVal.(bool)
default:
return false
}
})
respMap["results"] = results
}
}

// Apply limit if specified
if options.Limit > 0 && verb == "list" {
if results, ok := respMap["results"].([]interface{}); ok {
if len(results) > options.Limit {
respMap["results"] = results[:options.Limit]
}
}
}

// Filter columns if specified
if options.Columns != "" && verb == "list" {
if results, ok := respMap["results"].([]interface{}); ok {
columns := strings.Split(options.Columns, ",")
filteredResults := make([]interface{}, len(results))

for i, result := range results {
if resultMap, ok := result.(map[string]interface{}); ok {
filteredMap := make(map[string]interface{})
for _, col := range columns {
if val, exists := resultMap[strings.TrimSpace(col)]; exists {
filteredMap[strings.TrimSpace(col)] = val
}
}
filteredResults[i] = filteredMap
}
}
respMap["results"] = filteredResults
}
}

printData(respMap, options, serviceName, resourceName, refClient)
}

Expand Down
12 changes: 11 additions & 1 deletion cmd/common/fetchVerb.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type FetchOptions struct {
CopyToClipboard bool
SortBy string
MinimalColumns bool
Columns string
Limit int
}

// AddVerbCommands adds subcommands for each verb to the parent command
Expand Down Expand Up @@ -113,8 +115,12 @@ func AddVerbCommands(parentCmd *cobra.Command, serviceName string, groupID strin
}

sortBy := ""
columns := ""
limit := 0
if currentVerb == "list" {
sortBy, _ = cmd.Flags().GetString("sort")
columns, _ = cmd.Flags().GetString("columns")
limit, _ = cmd.Flags().GetInt("limit")
}

options := &FetchOptions{
Expand All @@ -126,6 +132,8 @@ func AddVerbCommands(parentCmd *cobra.Command, serviceName string, groupID strin
CopyToClipboard: copyToClipboard,
SortBy: sortBy,
MinimalColumns: cmd.Flag("minimal").Changed,
Columns: columns,
Limit: limit,
}

if currentVerb == "list" && !cmd.Flags().Changed("output") {
Expand Down Expand Up @@ -155,6 +163,8 @@ func AddVerbCommands(parentCmd *cobra.Command, serviceName string, groupID strin
verbCmd.Flags().BoolP("watch", "w", false, "Watch for changes")
verbCmd.Flags().StringP("sort", "s", "", "Sort by field (e.g. 'name', 'created_at')")
verbCmd.Flags().BoolP("minimal", "m", false, "Show minimal columns")
verbCmd.Flags().StringP("columns", "c", "", "Specific columns (-c id,name)")
verbCmd.Flags().IntP("limit", "l", 0, "Number of rows")
}

// Define flags for verbCmd
Expand All @@ -163,7 +173,7 @@ func AddVerbCommands(parentCmd *cobra.Command, serviceName string, groupID strin
verbCmd.Flags().StringP("file-parameter", "f", "", "YAML file parameter")
verbCmd.Flags().StringP("api-version", "v", "v1", "API Version")
verbCmd.Flags().StringP("output", "o", "yaml", "Output format (yaml, json, table, csv)")
verbCmd.Flags().BoolP("copy", "c", false, "Copy the output to the clipboard (copies any output format)")
verbCmd.Flags().BoolP("copy", "y", false, "Copy the output to the clipboard (copies any output format)")

// Set custom help function
verbCmd.SetHelpFunc(CustomVerbHelpFunc)
Expand Down

0 comments on commit eebc98c

Please sign in to comment.