refactor: Use IOStreams for command output instead of os.Stdout and log#372
Conversation
c0b2e22 to
bfe75a8
Compare
There was a problem hiding this comment.
Pull request overview
This PR standardizes user-facing CLI output by routing it through Kubernetes IOStreams (instead of writing directly to os.Stdout / fmt.Printf / log.Printf), aligning subcommands with the CLI’s existing stream-injection pattern.
Changes:
- Update
versionsubcommand to acceptIOStreamsand write toioStreams.Out. - Switch multiple
listsubcommands’ tabwriter output fromos.Stdouttoio.Out. - Replace
log.Printf/os.Stdoutoutput inbuild uploadwithfmt.Fprintf(ioStreams.Out, ...)and thread streams through helpers.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/shp/cmd/version/version.go | Accepts IOStreams and writes version output to ioStreams.Out. |
| pkg/shp/cmd/root.go | Passes ioStreams into the version command constructor. |
| pkg/shp/cmd/clusterbuildstrategy/list.go | Writes tabular list output to io.Out instead of os.Stdout. |
| pkg/shp/cmd/buildstrategy/list.go | Writes tabular list output to io.Out instead of os.Stdout. |
| pkg/shp/cmd/buildrun/list.go | Writes tabular list output to io.Out instead of os.Stdout. |
| pkg/shp/cmd/build/upload.go | Uses IOStreams for user-facing progress/info output during buildrun creation and streaming. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Verify check failing because of gosec, adding |
|
Ready to test again @adambkaplan |
|
/ok-to-test |
pkg/shp/cmd/build/upload.go
Outdated
| // Run executes the primary business logic of this subcommand, by starting to watch over the build | ||
| // pod status and react accordingly. | ||
| func (u *UploadCommand) Run(p *params.Params, ioStreams *genericclioptions.IOStreams) error { | ||
| u.ioStreams = ioStreams |
There was a problem hiding this comment.
Do we need this? Wasn’t u.ioStreams initialized before?
There was a problem hiding this comment.
we need that initialization @IrvingMg
u.ioStreams is only declared in the struct and never initialized before Run()
in Complete(), the second parameter is _ *genericclioptions.IOStreams and is intentionally ignored, so u.ioStreams is still nil there.
Methods like createBuildRun and performDataStreaming use u.ioStreams, and they are called from Run
There was a problem hiding this comment.
I see. Thanks for checking.
Then I’d just make a small suggestion: move u.ioStreams = ioStreams to Complete, as that method seems more appropriate for initializing the fields.
There was a problem hiding this comment.
Yup, Thanks for the review @IrvingMg
suggested changes has been made and ready for review again 😄
Signed-off-by: karthik balasubramanian <karthikbalasubramanian08@gmail.com>
IrvingMg
left a comment
There was a problem hiding this comment.
As I mentioned in #90 (comment), it looks like genericclioptions.IOStreams is the common approach for handling output in CLIs. Since shp already uses it in most commands, these changes look good to me.
Still, it would be great to hear feedback from other @shipwright-io/contributors.
/lgtm
pkg/shp/cmd/buildrun/list.go
Outdated
| // find out more in kubectl libraries and use them | ||
|
|
||
| writer := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0) | ||
| writer := tabwriter.NewWriter(io.Out, 0, 8, 2, '\t', 0) |
There was a problem hiding this comment.
Can you rename io to ioStreams as in upload.go. I think we should not use variable names that match Golang library packages.
Same in other files.
There was a problem hiding this comment.
I have made the requested changes and it's ready for review @SaschaSchwarze0
Signed-off-by: karthik balasubramanian <karthikbalasubramanian08@gmail.com>
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: SaschaSchwarze0 The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Changes
Fixes #90
The original issue was about inconsistent user facing logging.
Turns out 95% of the codebase is already following the best practice as the same as kubectl.
Following @IrvingMg's suggestion,
I've updated some inconsistencies to follow the same structure as the rest of the commands.
/kind feature
Submitter Checklist
See the contributor guide
for details on coding conventions, github and prow interactions, and the code review process.
Release Notes