-
Notifications
You must be signed in to change notification settings - Fork 276
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
21c20a7
f2cfdd2
731148e
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 { | ||
|
|
@@ -76,22 +77,36 @@ func listCommand(p cli.Params) *cobra.Command { | |
| }, | ||
| SilenceUsage: true, | ||
| RunE: func(cmd *cobra.Command, _ []string) error { | ||
| cs, err := p.Clients() | ||
| output, err := cmd.LocalFlags().GetString("output") | ||
| if err != nil { | ||
| return err | ||
| return fmt.Errorf("output option not set properly: %v", err) | ||
| } | ||
|
|
||
| output, err := cmd.LocalFlags().GetString("output") | ||
| if len(opts.Fields) > 0 && output != "ndjson" { | ||
| return fmt.Errorf("--fields is only supported with --output ndjson") | ||
| } | ||
|
|
||
| cs, err := p.Clients() | ||
| if err != nil { | ||
| return fmt.Errorf("output option not set properly: %v", err) | ||
| return err | ||
| } | ||
|
|
||
| ns := p.Namespace() | ||
| if opts.AllNamespaces { | ||
| 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 == "" { | ||
| 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 +123,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.
Contributor
Author
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. Added if len(opts.Fields) > 0 && output != "ndjson" { return error } to all 8 list commands: pipeline, task, pipelinerun, taskrun, customrun, clustertriggerbinding, triggerbinding, triggertemplate, eventlistener |
||
|
|
||
| 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.
Added TestListPipelineRuns_ndjson, TestListPipelines_ndjson, and TestListTasks_ndjson, each with three sub-tests: valid JSON lines output, --fields narrowing, and --fields without ndjson error