Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add column and limit feature #70

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading