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

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

Closed
hypergig opened this issue Apr 21, 2020 · 3 comments · Fixed by #894
Closed

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

hypergig opened this issue Apr 21, 2020 · 3 comments · Fixed by #894

Comments

@hypergig
Copy link

Apologies if my implementation is busted but if I do the following:

var rootCmd = &cobra.Command{
    Use:   "root",
    Run: func(cmd *cobra.Command, args []string) {
      cmd.Println("PRINT 1")
      cmd.PrintErrln("PRINTERR 1")
      cmd.SetOut(os.Stdout)
      cmd.SetErr(os.Stderr)
      cmd.Println("PRINT 2")
      cmd.PrintErrln("PRINTERR 2")
    },
  }

I see this behaviour:

13:57:20 ▶ root
PRINT 1
PRINTERR 1
PRINT 2
PRINTERR 2
13:57:29 ▶ root > /dev/null
PRINT 1
PRINTERR 1
13:57:33 ▶ root 2> /dev/null
PRINT 2
PRINTERR 2

I know cmd.Println sends to stderr by default, so I expect ^this to look more like this maybe:

13:57:20 ▶ root
PRINT 1
PRINTERR 1
PRINT 2
PRINTERR 2
13:57:29 ▶ root > /dev/null
PRINT 1
PRINTERR 1
PRINTERR 2
13:57:33 ▶ root 2> /dev/null
PRINT 2

Am I using the Set functions incorrectly or is this a bug?

@marckhouzam
Copy link
Collaborator

It's a bug in PrintErrln()

cobra/command.go

Lines 1181 to 1183 in 090d94f

func (c *Command) PrintErrln(i ...interface{}) {
c.Print(fmt.Sprintln(i...))
}

#894 wants to fix it but there are concerns about breaking existing scripts that have adapted to this bug.

@github-actions
Copy link

This issue is being marked as stale due to a long period of inactivity

@hypergig
Copy link
Author

Not really stale yet Mr. Bot... waiting for #894

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 a pull request may close this issue.

2 participants