-
Notifications
You must be signed in to change notification settings - Fork 275
feat: add ndjson output format with field selection to list commands #3068
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,7 @@ NAME AGE LAST RUN STARTED DURATION STATUS | |
| type ListOptions struct { | ||
| AllNamespaces bool | ||
| NoHeaders bool | ||
| Fields []string | ||
| } | ||
|
|
||
| func listCommand(p cli.Params) *cobra.Command { | ||
|
|
@@ -91,7 +92,17 @@ func listCommand(p cli.Params) *cobra.Command { | |
| ns = "" | ||
| } | ||
|
|
||
| if output != "" { | ||
| if output == "ndjson" { | ||
| var pl *v1.PipelineList | ||
| if err := actions.ListV1(pipelineGroupResource, cs, metav1.ListOptions{}, ns, &pl); err != nil { | ||
| scope := fmt.Sprintf("namespace %q", ns) | ||
| if ns == "" { | ||
|
Comment on lines
+95
to
+99
|
||
| scope = "all namespaces" | ||
| } | ||
| return fmt.Errorf("failed to list Pipelines from %s: %w", scope, err) | ||
| } | ||
| return formatted.PrintNDJSON(cmd.OutOrStdout(), pl, opts.Fields) | ||
| } else if output != "" { | ||
| p, err := f.ToPrinter() | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -108,6 +119,7 @@ func listCommand(p cli.Params) *cobra.Command { | |
| f.AddFlags(c) | ||
| c.Flags().BoolVarP(&opts.AllNamespaces, "all-namespaces", "A", opts.AllNamespaces, "list Pipelines from all namespaces") | ||
| c.Flags().BoolVarP(&opts.NoHeaders, "no-headers", "", opts.NoHeaders, "do not print column headers with output (default print column headers with output)") | ||
| c.Flags().StringSliceVar(&opts.Fields, "fields", opts.Fields, "Comma-separated list of fields to include in output (e.g. metadata.name,status.startTime); only used with --output ndjson") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a user passes if len(opts.Fields) > 0 && output != "ndjson" {
return fmt.Errorf("--fields is only supported with --output ndjson")
}This should apply to all list commands. |
||
|
|
||
| return c | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,7 @@ NAME DESCRIPTION AGE | |
| type ListOptions struct { | ||
| AllNamespaces bool | ||
| NoHeaders bool | ||
| Fields []string | ||
| } | ||
|
|
||
| func listCommand(p cli.Params) *cobra.Command { | ||
|
|
@@ -80,7 +81,17 @@ func listCommand(p cli.Params) *cobra.Command { | |
| ns = "" | ||
| } | ||
|
|
||
| if output != "" { | ||
| if output == "ndjson" { | ||
| var tl *v1.TaskList | ||
| if err := actions.ListV1(taskGroupResource, cs, metav1.ListOptions{}, ns, &tl); err != nil { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same concern as above — this makes a second API call instead of reusing the already-fetched list. Can we align with the approach used in the other commands. |
||
| scope := fmt.Sprintf("namespace %q", ns) | ||
| if ns == "" { | ||
| scope = "all namespaces" | ||
| } | ||
| return fmt.Errorf("failed to list Tasks from %s: %w", scope, err) | ||
| } | ||
| return formatted.PrintNDJSON(cmd.OutOrStdout(), tl, opts.Fields) | ||
| } else if output != "" { | ||
| p, err := f.ToPrinter() | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -97,6 +108,7 @@ func listCommand(p cli.Params) *cobra.Command { | |
| f.AddFlags(c) | ||
| c.Flags().BoolVarP(&opts.AllNamespaces, "all-namespaces", "A", opts.AllNamespaces, "list Tasks from all namespaces") | ||
| c.Flags().BoolVarP(&opts.NoHeaders, "no-headers", "", opts.NoHeaders, "do not print column headers with output (default print column headers with output)") | ||
| c.Flags().StringSliceVar(&opts.Fields, "fields", opts.Fields, "Comma-separated list of fields to include in output (e.g. metadata.name,status.startTime); only used with --output ndjson") | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ const ( | |
| type listOptions struct { | ||
| AllNamespaces bool | ||
| NoHeaders bool | ||
| Fields []string | ||
| } | ||
|
|
||
| func listCommand(p cli.Params) *cobra.Command { | ||
|
|
@@ -87,7 +88,10 @@ or | |
| Err: cmd.OutOrStderr(), | ||
| } | ||
|
|
||
| if output == "name" && tbs != nil { | ||
| switch { | ||
| case output == "ndjson": | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also guard against |
||
| return formatted.PrintNDJSON(stream.Out, tbs, opts.Fields) | ||
| case output == "name" && tbs != nil: | ||
| w := cmd.OutOrStdout() | ||
| for _, pr := range tbs.Items { | ||
| _, err := fmt.Fprintf(w, "triggerbinding.triggers.tekton.dev/%s\n", pr.Name) | ||
|
|
@@ -96,7 +100,7 @@ or | |
| } | ||
| } | ||
| return nil | ||
| } else if output != "" { | ||
| case output != "": | ||
| p, err := f.ToPrinter() | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -115,6 +119,7 @@ or | |
| f.AddFlags(c) | ||
| c.Flags().BoolVarP(&opts.AllNamespaces, "all-namespaces", "A", opts.AllNamespaces, "list TriggerBindings from all namespaces") | ||
| c.Flags().BoolVar(&opts.NoHeaders, "no-headers", opts.NoHeaders, "do not print column headers with output (default print column headers with output)") | ||
| c.Flags().StringSliceVar(&opts.Fields, "fields", opts.Fields, "Comma-separated list of fields to include in output (e.g. metadata.name,status.startTime); only used with --output ndjson") | ||
| return c | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlike other commands in this PR (e.g.,
pipelinerun,taskrun,customrun) which guard with&& prs != nilor similar, this branch passestbsdirectly without a nil check. Iftbsis nil,runtime.DefaultUnstructuredConverter.ToUnstructured(nil)will panic.Can we to something like: