Skip to content

feat(cli): emit the object itself for a request that named one resource - #372

Open
shaked-bouktus wants to merge 2 commits into
mainfrom
feat/cli-single-item-output
Open

shaked-bouktus wants to merge 2 commits into
mainfrom
feat/cli-single-item-output

Conversation

@shaked-bouktus

@shaked-bouktus shaked-bouktus commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Every result came back wrapped in the {items, count} envelope, so
"kli definitions NAME -o yaml" handed you a list of one. Piping that into
validate or kubectl apply meant unwrapping it first, which is a lot of
ceremony for something you already asked for by name.

The shape now follows what you asked for rather than what came back, same as
kubectl. Name a thing and you get the thing: "definitions NAME",
"get TYPE/NAME". Ask for a list and you get the envelope, even when only one
result matches. That last bit is the point of keying off the request instead
of the count - a script doing "jq .items[]" would otherwise break the day a
filter happened to match exactly once.

Render still wraps, RenderNamed does not, and both run through one unexported
render that takes the payload already shaped, so there is still only one
format switch. A named get goes through client.Get, which either returns one
object or exits not-found, so the renderer never has to guess which view to
emit.

Related issue(s)

Fixes #

Checklist

  • All commits are signed off with DCO (git commit -s)
  • New/modified files have SPDX license and copyright headers
  • Documentation updated (if applicable)
  • Tests pass (make check)
  • No proprietary or internal information included

Summary by CodeRabbit

  • New Features

    • Named definitions and workloads now return a single JSON or YAML object without an items wrapper.
    • Collection requests continue using the items envelope.
    • Table output remains consistent for named and collection requests.
  • Documentation

    • Updated kli definitions documentation to clarify machine-readable output and validation.
    • Added an example for piping named YAML definitions to kli validate.
  • Bug Fixes

    • Improved consistency between JSON and YAML output for named resources.
    • Empty or multi-item results retain collection formatting.

The machine formats wrapped every result in the {items, count} envelope, so
"kli definitions NAME -o yaml" emitted a list of one and did not pipe into
validate or kubectl apply without an unwrapping step.

The shape now follows the request rather than the result size, the way kubectl's
does: "definitions NAME" and "get TYPE/NAME" emit the object, while a filter or a
bare list keeps the envelope however many results come back. Keying off the
request and not the count is what makes the shape a function of the command line,
so a consumer walking items is never broken by a filter that happens to match
once.

Render keeps the envelope and RenderNamed emits the item, both over one
unexported render that takes the payload already shaped, so the two differ in
shape without duplicating the format switch. A named get resolves through a
client.Get that returns exactly one object or exits not-found, so the renderer
never sees an ambiguous named result.

Signed-off-by: sbouktus <sbouktus@nvidia.com>
@coderabbitai

coderabbitai Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Enterprise

Run ID: 703a0fb0-e42d-46f6-8818-08b4488c3c60

📥 Commits

Reviewing files that changed from the base of the PR and between b41ffd1 and 05b2e2e.

📒 Files selected for processing (7)
  • cli/cmd/definitions.go
  • cli/cmd/definitions_test.go
  • cli/cmd/get.go
  • cli/pkg/generator/render.go
  • cli/pkg/generator/render_test.go
  • cli/pkg/generator/workload.go
  • cli/pkg/generator/workload_test.go

Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review.


📝 Walkthrough

Walkthrough

Changes

Named definition and workload requests now emit standalone JSON or YAML objects when one item resolves. Collection requests retain the items envelope. Rendering uses one Render path with a named-result flag, and tests verify output shape and validation.

Named output rendering

Layer / File(s) Summary
Unified resource rendering
cli/pkg/generator/render.go, cli/pkg/generator/render_test.go
Render now selects direct single-item output with byName=true. Empty and multi-item results retain collection envelopes. Tests cover serialization, table callbacks, and unsupported formats.
Named workload routing
cli/pkg/generator/workload.go, cli/pkg/generator/workload_test.go, cli/cmd/get.go, cli/cmd/get_test.go
Options.ByName carries named workload requests to Render. Tests verify standalone named output, collection output, and empty-result behavior.
Definition command integration
cli/cmd/definitions.go, cli/cmd/definitions_test.go
The definitions command passes named-request state to Render. Documentation and tests cover standalone JSON/YAML output, cross-format equivalence, and validation.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant CLI
  participant RenderWorkloads
  participant Render
  participant JSONYAML
  CLI->>RenderWorkloads: request named workload
  RenderWorkloads->>Render: pass ByName and resolved items
  Render->>JSONYAML: serialize one item directly
  JSONYAML-->>CLI: return standalone JSON or YAML
Loading

Merge Risk: ⚪ Minimal · up to 05b2e

Named JSON/YAML output is consistently shaped as documented, with collection output preserved for list requests. The change is ready to merge.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 54.55% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 11 functions across 8 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: named resource requests now emit the resource object directly instead of a collection envelope.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/cli-single-item-output

Comment @coderabbitai help to get the list of available commands.

@shaked-bouktus
shaked-bouktus marked this pull request as ready for review September 15, 2026 10:47
Comment thread cli/pkg/generator/workload.go Outdated
// AllNamespaces drops the namespace from the empty-result message.
AllNamespaces bool
// Named reports that the request addressed one workload by name.
Named bool

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ByName?

Comment thread cli/pkg/generator/workload.go Outdated
Comment on lines +56 to +59
if opts.Named && len(views) == 1 {
return RenderNamed(out, format, views[0], table)
}
return Render(out, format, views, table)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider handling this inside render.go so that outside callers don't need to check this and decide whether to call RenderNamed or Render.

Comment thread cli/cmd/definitions.go Outdated
Comment on lines +136 to +139
if len(args) == 1 {
return generator.RenderNamed(cmd.OutOrStdout(), output.Get(), kartas[0], table)
}
return generator.Render(cmd.OutOrStdout(), output.Get(), kartas, table)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as bellow comment

Render now takes a byName bool and decides the shape itself, so a caller hands
it the result and the flag rather than picking between two functions and
indexing into its own slice to call one of them. RenderNamed is gone.

Options.Named becomes Options.ByName to match the parameter.

Addresses review feedback on PR #372.

Signed-off-by: sbouktus <sbouktus@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants