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 printing functions #894

Merged
merged 6 commits into from
Oct 1, 2020
Merged

Conversation

alessio
Copy link
Contributor

@alessio alessio commented Jun 24, 2019

Follow-up of #822

This fixes stderr redirection.

Closes: #1100

@spf13 @eparis

@alessio
Copy link
Contributor Author

alessio commented Jun 24, 2019

CC'ing @jleni

@amdprophet
Copy link

Should https://github.com/spf13/cobra/blob/master/command.go#L926 use PrintErr? We're seeing errors output to stdout after calling SetOut and SetErr.

@alessio
Copy link
Contributor Author

alessio commented Jun 25, 2019

Yes, I think it should. Will amend my PR ASAP

@alessio
Copy link
Contributor Author

alessio commented Jun 26, 2019

@amdprophet done, please review

@jleni
Copy link
Contributor

jleni commented Jun 26, 2019

@alessio for some reason, the changes seem to break TestBashCompletions

@alessio
Copy link
Contributor Author

alessio commented Jun 26, 2019

Will try to reproduce and fix shortly

@PapaCharlie
Copy link
Contributor

The shellcheck warnings are being addressed in #889

@alessio
Copy link
Contributor Author

alessio commented Jul 5, 2019

Point taken: mapfile is available in Bash >= 4. Happy to incorporate #889 commits instead of going for my solution

@alessio alessio force-pushed the fix-stderr-redirection branch from 3299407 to 0da6ddb Compare August 10, 2019 06:17
@alessio
Copy link
Contributor Author

alessio commented Aug 10, 2019

Conflicts have been resolved, please review

@portertech
Copy link

Please review 🙏

@amdprophet
Copy link

@spf13 @eparis @jleni any chance of this getting reviewed soon? 🙏

@alessio
Copy link
Contributor Author

alessio commented Dec 10, 2019

Hi!

Any news? @spf13 @eparis @jleni

@jleni
Copy link
Contributor

jleni commented Dec 10, 2019

Sorry @alessio.. but there isn't much I can do myself! I don't have any permissions in this repo

@alessio
Copy link
Contributor Author

alessio commented Dec 18, 2019

@jharshman bump :)

1 similar comment
@portertech
Copy link

@jharshman bump :)

@alessio
Copy link
Contributor Author

alessio commented Mar 30, 2020

@jharshman Hi! Any news on this? :)

@alessio
Copy link
Contributor Author

alessio commented Apr 10, 2020

@marckhouzam bump :)

@marckhouzam
Copy link
Collaborator

@marckhouzam bump :)

I'm nobody 😉

But if I may suggest something based on my experience. To help get any PR merged (in any project) you should make the review as easy as possible for the reviewer. So, in the PR description, you really need to explain what problem this fixes. If you can mention why this is valuable (the impact of the bug), it would also help prioritize this PR over the many others. Finally, mentioning if there are any backwards-compatibility issues or not would probably help build confidence in your change (that's what makes me unsure of this change, considering my limited experience with Cobra).

Just my opinion though.

command.go Outdated Show resolved Hide resolved
command.go Outdated Show resolved Hide resolved
@alessio alessio requested a review from jharshman April 12, 2020 09:01
@alessio
Copy link
Contributor Author

alessio commented Apr 12, 2020

I amended the PR as requested. I've just tweaked the README.me file (error messages should always go to STDERR, even when a program fails).

@jharshman @marckhouzam please review again when you spare some time.

Thanks for considering.

Copy link

@fedekunze fedekunze 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 the fix @alessio. I have run into the same issue

@ethanfrey
Copy link

Thank you, this would really help us with proper output so we can use the cli tools in scripts more easily.

@jgimeno
Copy link

jgimeno commented Aug 19, 2020

I have been waiting this solution for a long time, how come is still not merged? I hope this is being merged soon.

I don't know what is the problem to release this in a minor version (patch will be more accurate). Only people that upgrades will see the problem and other will update when they have bandwidth to solve the issue.

@jharshman jharshman added kind/bug A bug in cobra; unintended behavior and removed kind/stale labels Aug 23, 2020
@jharshman jharshman modified the milestone: 1.0.0 Aug 23, 2020
Copy link
Collaborator

@jpmcb jpmcb left a comment

Choose a reason for hiding this comment

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

This looks good to me! Thanks for waiting on this. We will get this merged very soon! 🚀

@bukowa
Copy link

bukowa commented Sep 8, 2020

func TestCommand_Print(t *testing.T) {
	errBuff, outBuff := bytes.NewBuffer(nil), bytes.NewBuffer(nil)

	wantErr := []byte("PrintErrPrintErr line\nPrintErr")
	wantOut := []byte("PrintPrint line\nPrint")

	root := &Command{
		Run: func(cmd *Command, args []string) {

			cmd.PrintErr("PrintErr")
			cmd.PrintErrln("PrintErr", "line")
			cmd.PrintErrf("PrintEr%s", "r")

			cmd.Print("Print")
			cmd.Println("Print", "line")
			cmd.Printf("Prin%s", "t")
		},
	}

	root.SetErr(errBuff)
	root.SetOut(outBuff)

	if err := root.Execute(); err != nil {
		t.Error(err)
	}

	errBytes, err := ioutil.ReadAll(errBuff)
	if err != nil {
		t.Error(err)
	}

	outBytes, err := ioutil.ReadAll(outBuff)
	if err != nil {
		t.Error(err)
	}

	if !bytes.Equal(errBytes, wantErr) {
		t.Errorf("got: '%s' want: '%s'", errBytes, wantErr)
	}

	if !bytes.Equal(outBytes, wantOut) {
		t.Errorf("got: '%s' want: '%s'", outBytes, wantOut)
	}
}

@alessio
Copy link
Contributor Author

alessio commented Sep 8, 2020

@bukowa I've integrated your test case in this PR. Thanks!

Copy link
Collaborator

@jpmcb jpmcb left a comment

Choose a reason for hiding this comment

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

This is great - thanks so much for the patience and time spent on this. And thanks so much for adding a test case!

@jpmcb jpmcb merged commit 40d34bc into spf13:master Oct 1, 2020
mattmoyer added a commit to mattmoyer/pinniped that referenced this pull request Dec 17, 2020
It now correctly prints errors to stderr (spf13/cobra#894).

Signed-off-by: Matt Moyer <moyerm@vmware.com>
mattmoyer added a commit to mattmoyer/pinniped that referenced this pull request Dec 17, 2020
It now correctly prints errors to stderr (spf13/cobra#894).

Signed-off-by: Matt Moyer <moyerm@vmware.com>
daeMOn63 pushed a commit to fetchai/cosmos-sdk that referenced this pull request May 5, 2021
client/input/input.go: GetConfirmation() should communicate
with the user via the io.Writer instance passed in as
argument.

client/keys/add.go: replace cmd.PrintErrln() calls with
fmt.Fprintln(cmd.ErrOrStderr(), ...) because of cobra's
PrintErr* functions broken behaviour. For more information
please see spf13/cobra#894

Closes: #6601
Thanks: @noandrea for pointing this out.
itaysk added a commit to erikgb/kubectl-neat that referenced this pull request Jun 19, 2022
since bumbping cobra version the upstream bug is now resolved
spf13/cobra#894
erikgb pushed a commit to erikgb/kubectl-neat that referenced this pull request Jun 19, 2022
since bumbping cobra version the upstream bug is now resolved
spf13/cobra#894
erikgb pushed a commit to erikgb/kubectl-neat that referenced this pull request Jun 19, 2022
since bumbping cobra version the upstream bug is now resolved
spf13/cobra#894
erikgb pushed a commit to erikgb/kubectl-neat that referenced this pull request Jun 19, 2022
since bumbping cobra version the upstream bug is now resolved
spf13/cobra#894
itaysk added a commit to itaysk/kubectl-neat that referenced this pull request Jun 20, 2022
since bumbping cobra version the upstream bug is now resolved
spf13/cobra#894
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug A bug in cobra; unintended behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SetOut and SetErr don't seem to work as expeceted?