-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
base: main
Are you sure you want to change the base?
Conversation
d0b15ff
to
69fccc1
Compare
69fccc1
to
e35bf96
Compare
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.
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.
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>
e35bf96
to
1374825
Compare
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.
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
andSetErrFallback
were not the right ones, I didn't think of anything better. ButSetOutStream
andSetErrStream
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
andSetErrStream
. -
Renamed internal variables
c.outWritter
andc.errWritter
toc.legacyOutWriter
andc.legacyErrWriter
. -
For better clarity when using
Print()
, I deprecated it in favor ofPrintErr
orPrintOut
flavors.
Please, let me know what you think and what I can improve.
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 usesout
but falls back toerr
.If you don't use the new
SetOutFallback / SetErrFallback
, the behavior remains the same as before (no breaking changes)However, by replacing
SetErr
withSetErrFallback
, 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