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

fix: stderr/stdout handling when custom err/out is defined (SetErrFallback) #2002

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

tcarreira
Copy link

@tcarreira tcarreira commented Jul 22, 2023

Fix: Allows capturing cmd output and errors
The current behavior of OutOrStderr() is inconsistent. This PR introduces a feature to achieve the desired behavior without causing any breaking changes.


If I understood correctly, the problem is caused by using OutOrStderr(), which uses out but falls back to err.
If you don't use the new SetOutFallback / SetErrFallback, the behavior remains the same as before (no breaking changes)
However, by replacing SetErr with SetErrFallback, we can achieve the desired behavior.

I'm not sure if SetOutFallback should be included. I added it for consistency, but it is not necessary in this context.
@mislav, please let me know what you think, as you were on the frontline with this one.


Closes: #1708

@CLAassistant
Copy link

CLAassistant commented Jul 22, 2023

CLA assistant check
All committers have signed the CLA.

@tcarreira tcarreira marked this pull request as ready for review August 8, 2023 15:23
@tcarreira
Copy link
Author

Hi @jpmcb
Let me know if this PR solves the #1708 correctly ;)

Copy link

@mislav mislav left a comment

Choose a reason for hiding this comment

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

I think this is a move in a correct direction, but I am not sure about the naming of the new APIs and the current behavior if both SetOut and SetOutFallback (for example) are used at the same time.

The crux of the original issue—as I see it—was that the outcomes of SetOut and SetErr were 1) poorly documented, and 2) surprising. If this PR was meant to address that, then the new APIs should have directly improved on both these issues. As it currently stands, I find the naming of the new APIs (e.g. SetErrFallback) a bit confusing, and their documentation is not any better than of the old APIs.

The names SetOutFallback and SetErrFallback indicate that they set a "fallback" of sorts, but that doesn't make sense to me. The purpose of these methods is to override output & error streams from os.Stdout & os.Stderr to custom writers defined by the user. So they are not setting a "fallback", they are directly defining the streams that should be written to. Since the names SetOut and SetErr are already taken (by now deprecated methods), why not name the new methods SetOutStream and SetErrStream? Then, their documentation could be more expressive and detail what kind of content will actually be written to these streams, and what is their default value.

Most of all, I find this documentation confusing:

SetOutFallback sets the destination for usage messages when SetOut() was not used.

This line implies that SetOutFallback is an API secondary to SetOut. However, since SetOut is now deprecated, SetOutFallback is now the primary API, and SetOut should not be used nor referenced from documentation of non-deprecated methods.

If both the new APIs and the old APIs are used at the same time (e.g. if SetOut is used along with SetOutFallback), the behavior of the new APIs should "win" over the old, deprecated ones. Right now it's the other way around.

command.go Show resolved Hide resolved
command.go Outdated Show resolved Hide resolved
tcarreira and others added 4 commits October 1, 2023 13:48
OutOrStderr() introduces an inconsistent behavior.
This commit adds a feature to get the right behavior, without breaking changes

closes: spf13#1708

Co-authored-by: Mislav Marohnić <git@mislav.net>
Copy link
Author

@tcarreira tcarreira left a comment

Choose a reason for hiding this comment

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

Thanks for your input @mislav, I find all your comments very helpful.
I've made some changes, on multiple commits for easier review:

  • Indeed, SetOutFallback and SetErrFallback were not the right ones, I didn't think of anything better. But SetOutStream and SetErrStream feel like perfect to me, thanks.

  • I also prefer your suggestion about making the new API having priority over the old one. Still not a breaking change, until someone "opts in" on the new API.

  • I added more documentation on SetOutStream and SetErrStream .

  • Renamed internal variables c.outWritter and c.errWritter to c.legacyOutWriter and c.legacyErrWriter.

  • For better clarity when using Print(), I deprecated it in favor of PrintErr or PrintOut flavors.

Please, let me know what you think and what I can improve.

command.go Show resolved Hide resolved
command.go Outdated Show resolved Hide resolved
@jpmcb jpmcb added this to the 1.8.0 milestone Oct 29, 2023
@marckhouzam marckhouzam modified the milestones: 1.8.0, 1.9.0 Nov 15, 2023
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.

Unclear how "Out" and "Err" streams should be used
5 participants