Skip to content

Commit

Permalink
Fix stderr printing functions (#894)
Browse files Browse the repository at this point in the history
* Fix stderr printing functions

Follow-up of #822

* Errors go to stderr as per POSIX

* use PrintErrf() instead of extra call to Sprintf()

* Error messages should always be printed to os.Stderr.

* add test case for Print* redirection

Thanks: @bukowa for the patch.
  • Loading branch information
Alessio Treglia committed Oct 1, 2020
1 parent 0bc8bfb commit 40d34bc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -177,7 +177,7 @@ var rootCmd = &cobra.Command{

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Expand Down
14 changes: 7 additions & 7 deletions command.go
Expand Up @@ -367,7 +367,7 @@ func (c *Command) UsageFunc() (f func(*Command) error) {
c.mergePersistentFlags()
err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)
if err != nil {
c.Println(err)
c.PrintErrln(err)
}
return err
}
Expand Down Expand Up @@ -395,7 +395,7 @@ func (c *Command) HelpFunc() func(*Command, []string) {
// See https://github.com/spf13/cobra/issues/1002
err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)
if err != nil {
c.Println(err)
c.PrintErrln(err)
}
}
}
Expand Down Expand Up @@ -938,8 +938,8 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
c = cmd
}
if !c.SilenceErrors {
c.Println("Error:", err.Error())
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
c.PrintErrln("Error:", err.Error())
c.PrintErrf("Run '%v --help' for usage.\n", c.CommandPath())
}
return c, err
}
Expand Down Expand Up @@ -967,7 +967,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
// If root command has SilentErrors flagged,
// all subcommands should respect it
if !cmd.SilenceErrors && !c.SilenceErrors {
c.Println("Error:", err.Error())
c.PrintErrln("Error:", err.Error())
}

// If root command has SilentUsage flagged,
Expand Down Expand Up @@ -1209,12 +1209,12 @@ func (c *Command) PrintErr(i ...interface{}) {

// PrintErrln is a convenience method to Println to the defined Err output, fallback to Stderr if not set.
func (c *Command) PrintErrln(i ...interface{}) {
c.Print(fmt.Sprintln(i...))
c.PrintErr(fmt.Sprintln(i...))
}

// PrintErrf is a convenience method to Printf to the defined Err output, fallback to Stderr if not set.
func (c *Command) PrintErrf(format string, i ...interface{}) {
c.Print(fmt.Sprintf(format, i...))
c.PrintErr(fmt.Sprintf(format, i...))
}

// CommandPath returns the full path to this command.
Expand Down
42 changes: 42 additions & 0 deletions command_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -1636,6 +1637,47 @@ func TestUsageStringRedirected(t *testing.T) {
}
}

func TestCommandPrintRedirection(t *testing.T) {
errBuff, outBuff := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
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)
}

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

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

if wantErr := []byte("PrintErrPrintErr line\nPrintErr"); !bytes.Equal(gotErrBytes, wantErr) {
t.Errorf("got: '%s' want: '%s'", gotErrBytes, wantErr)
}

if wantOut := []byte("PrintPrint line\nPrint"); !bytes.Equal(gotOutBytes, wantOut) {
t.Errorf("got: '%s' want: '%s'", gotOutBytes, wantOut)
}
}

func TestFlagErrorFunc(t *testing.T) {
c := &Command{Use: "c", Run: emptyRun}

Expand Down

0 comments on commit 40d34bc

Please sign in to comment.