Skip to content
Myr

Fix stderr printing functions (#894)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* 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

authored by Alessio Treglia · committed by GitHub · parent 0bc8bfb · Browse files · 3 files+50 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Thanks: @bukowa for the patch.

Diff stats

3 files+50 added−8 removed

  • other:+50 added−8 removed

3 files+50 added−8 removed

modified:command_test.go+42 added
2 unmodified lines
3import (4 "bytes"5 "context"6 "fmt"7 "io/ioutil"8 "os"9 "reflect"10 "strings"11 "testing"
1624 unmodified lines
1636 t.Errorf("Expected usage string to consider both stdout and stderr")1637 }1638}16391640func TestCommandPrintRedirection(t *testing.T) {1641 errBuff, outBuff := bytes.NewBuffer(nil), bytes.NewBuffer(nil)1642 root := &Command{1643 Run: func(cmd *Command, args []string) {16441645 cmd.PrintErr("PrintErr")1646 cmd.PrintErrln("PrintErr", "line")1647 cmd.PrintErrf("PrintEr%s", "r")16481649 cmd.Print("Print")1650 cmd.Println("Print", "line")1651 cmd.Printf("Prin%s", "t")1652 },1653 }16541655 root.SetErr(errBuff)1656 root.SetOut(outBuff)16571658 if err := root.Execute(); err != nil {1659 t.Error(err)1660 }16611662 gotErrBytes, err := ioutil.ReadAll(errBuff)1663 if err != nil {1664 t.Error(err)1665 }16661667 gotOutBytes, err := ioutil.ReadAll(outBuff)1668 if err != nil {1669 t.Error(err)1670 }16711672 if wantErr := []byte("PrintErrPrintErr line\nPrintErr"); !bytes.Equal(gotErrBytes, wantErr) {1673 t.Errorf("got: '%s' want: '%s'", gotErrBytes, wantErr)1674 }16751676 if wantOut := []byte("PrintPrint line\nPrint"); !bytes.Equal(gotOutBytes, wantOut) {1677 t.Errorf("got: '%s' want: '%s'", gotOutBytes, wantOut)1678 }1679}16801681func TestFlagErrorFunc(t *testing.T) {1682 c := &Command{Use: "c", Run: emptyRun}16831684 expectedFmt := "This is expected: %v"
349 unmodified lines
modified:command.go+7 added-7 removed
365 unmodified lines
366 return func(c *Command) error {367 c.mergePersistentFlags()368 err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)369 if err != nil {370 c.Println(err)370 c.PrintErrln(err)371 }372 return err373 }374}
19 unmodified lines
394 // The help should be sent to stdout395 // See https://github.com/spf13/cobra/issues/1002396 err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)397 if err != nil {398 c.Println(err)398 c.PrintErrln(err)399 }400 }401}402
534 unmodified lines
937 if cmd != nil {938 c = cmd939 }940 if !c.SilenceErrors {941 c.Println("Error:", err.Error())942 c.Printf("Run '%v --help' for usage.\n", c.CommandPath())941 c.PrintErrln("Error:", err.Error())942 c.PrintErrf("Run '%v --help' for usage.\n", c.CommandPath())943 }944 return c, err945 }946
19 unmodified lines
966967 // If root command has SilentErrors flagged,968 // all subcommands should respect it969 if !cmd.SilenceErrors && !c.SilenceErrors {970 c.Println("Error:", err.Error())970 c.PrintErrln("Error:", err.Error())971 }972973 // If root command has SilentUsage flagged,974 // all subcommands should respect it
233 unmodified lines
1208}12091210// PrintErrln is a convenience method to Println to the defined Err output, fallback to Stderr if not set.1211func (c *Command) PrintErrln(i ...interface{}) {1212 c.Print(fmt.Sprintln(i...))1212 c.PrintErr(fmt.Sprintln(i...))1213}12141215// PrintErrf is a convenience method to Printf to the defined Err output, fallback to Stderr if not set.1216func (c *Command) PrintErrf(format string, i ...interface{}) {1217 c.Print(fmt.Sprintf(format, i...))1217 c.PrintErr(fmt.Sprintf(format, i...))1218}12191220// CommandPath returns the full path to this command.1221func (c *Command) CommandPath() string {
443 unmodified lines
modified:README.md+1 added-1 removed
175 unmodified lines
176}177178func Execute() {179 if err := rootCmd.Execute(); err != nil {180 fmt.Println(err)180 fmt.Fprintln(os.Stderr, err)181 os.Exit(1)182 }183}184```
576 unmodified lines