From c76dca0456bb38c16c10bc69088d0bf9ace157c3 Mon Sep 17 00:00:00 2001 From: rigel rozanski Date: Fri, 26 May 2017 13:48:37 -0400 Subject: [PATCH 1/6] cli stderr output for Execute --- cli/setup.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/setup.go b/cli/setup.go index 21b29a49..30fb5e78 100644 --- a/cli/setup.go +++ b/cli/setup.go @@ -93,9 +93,9 @@ func (e Executor) Execute() error { if err != nil { // TODO: something cooler with log-levels if viper.GetBool(TraceFlag) { - fmt.Printf("ERROR: %+v\n", err) + fmt.Fprintf(os.Stderr, "ERROR: %+v\n", err) } else { - fmt.Println("ERROR:", err.Error()) + fmt.Fprintf(os.Stderr, "ERROR: %v\n", err) } } return err From 33d0dd0bfc3451643a78eb267faafd704c224a79 Mon Sep 17 00:00:00 2001 From: rigel rozanski Date: Tue, 6 Jun 2017 18:36:28 -0400 Subject: [PATCH 2/6] add stderr to tests --- .gitignore | 1 + cli/helper.go | 33 ++++++++++++++++++++++----------- cli/setup_test.go | 6 ++++-- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 62f28681..6e098685 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +*.swp vendor .glide diff --git a/cli/helper.go b/cli/helper.go index 79654bc3..959f8a5f 100644 --- a/cli/helper.go +++ b/cli/helper.go @@ -57,28 +57,39 @@ func RunWithArgs(cmd Executable, args []string, env map[string]string) error { // RunCaptureWithArgs executes the given command with the specified command line args // and environmental variables set. It returns whatever was writen to // stdout along with any error returned from cmd.Execute() -func RunCaptureWithArgs(cmd Executable, args []string, env map[string]string) (output string, err error) { - old := os.Stdout // keep backup of the real stdout - r, w, _ := os.Pipe() - os.Stdout = w +func RunCaptureWithArgs(cmd Executable, args []string, env map[string]string) (stdout, stderr string, err error) { + oldout, olderr := os.Stdout, os.Stderr // keep backup of the real stdout + rOut, wOut, _ := os.Pipe() + rErr, wErr, _ := os.Pipe() + os.Stdout, os.Stderr = wOut, wErr defer func() { - os.Stdout = old // restoring the real stdout + os.Stdout, os.Stderr = oldout, olderr // restoring the real stdout }() - outC := make(chan string) // copy the output in a separate goroutine so printing can't block indefinitely + outC := make(chan string) go func() { var buf bytes.Buffer - // io.Copy will end when we call w.Close() below - io.Copy(&buf, r) + // io.Copy will end when we call wOut.Close() below + io.Copy(&buf, rOut) outC <- buf.String() }() + errC := make(chan string) + go func() { + var buf bytes.Buffer + // io.Copy will end when we call wErr.Close() below + io.Copy(&buf, rErr) + errC <- buf.String() + }() + // now run the command err = RunWithArgs(cmd, args, env) // and grab the stdout to return - w.Close() - output = <-outC - return output, err + wOut.Close() + wErr.Close() + stdout = <-outC + stderr = <-errC + return stdout, stderr, err } diff --git a/cli/setup_test.go b/cli/setup_test.go index 8fb4ce14..791bc799 100644 --- a/cli/setup_test.go +++ b/cli/setup_test.go @@ -212,9 +212,11 @@ func TestSetupTrace(t *testing.T) { viper.Reset() args := append([]string{cmd.Use}, tc.args...) - out, err := RunCaptureWithArgs(cmd, args, tc.env) + stdout, stderr, err := RunCaptureWithArgs(cmd, args, tc.env) require.NotNil(err, i) - msg := strings.Split(out, "\n") + require.Equal("", stdout, i) + require.NotEqual("", stderr, i) + msg := strings.Split(stderr, "\n") desired := fmt.Sprintf("ERROR: %s", tc.expected) assert.Equal(desired, msg[0], i) if tc.long && assert.True(len(msg) > 2, i) { From 94c0172618a5d2d856edd2568034dd6b3b69b6c0 Mon Sep 17 00:00:00 2001 From: rigel rozanski Date: Wed, 7 Jun 2017 05:05:11 -0400 Subject: [PATCH 3/6] doc update --- cli/helper.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cli/helper.go b/cli/helper.go index 959f8a5f..12f94ec7 100644 --- a/cli/helper.go +++ b/cli/helper.go @@ -54,9 +54,10 @@ func RunWithArgs(cmd Executable, args []string, env map[string]string) error { return cmd.Execute() } -// RunCaptureWithArgs executes the given command with the specified command line args -// and environmental variables set. It returns whatever was writen to -// stdout along with any error returned from cmd.Execute() +// RunCaptureWithArgs executes the given command with the specified command +// line args and environmental variables set. It returns string fields +// representing output written to stdout and stderr, additionally any error +// from cmd.Execute() is also returned func RunCaptureWithArgs(cmd Executable, args []string, env map[string]string) (stdout, stderr string, err error) { oldout, olderr := os.Stdout, os.Stderr // keep backup of the real stdout rOut, wOut, _ := os.Pipe() From 0a3a08a3bc3d3397c259dc79f6e9bc15dac2bba2 Mon Sep 17 00:00:00 2001 From: rigel rozanski Date: Sat, 17 Jun 2017 18:35:05 -0400 Subject: [PATCH 4/6] stderr PR revisions --- CHANGELOG.md | 6 ++++++ cli/helper.go | 27 ++++++++++++--------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a97aa128..c5b2a155 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Develop-Branch changes (unreleased) + +BREAKING CHANGES: + +- [run] NewBaseService takes the new logger + ## 0.2.1 (June 2, 2017) FEATURES: diff --git a/cli/helper.go b/cli/helper.go index 12f94ec7..8bdb9e49 100644 --- a/cli/helper.go +++ b/cli/helper.go @@ -68,21 +68,18 @@ func RunCaptureWithArgs(cmd Executable, args []string, env map[string]string) (s }() // copy the output in a separate goroutine so printing can't block indefinitely - outC := make(chan string) - go func() { - var buf bytes.Buffer - // io.Copy will end when we call wOut.Close() below - io.Copy(&buf, rOut) - outC <- buf.String() - }() - - errC := make(chan string) - go func() { - var buf bytes.Buffer - // io.Copy will end when we call wErr.Close() below - io.Copy(&buf, rErr) - errC <- buf.String() - }() + copyStd := func(reader *os.File) *(chan string) { + stdC := make(chan string) + go func() { + var buf bytes.Buffer + // io.Copy will end when we call reader.Close() below + io.Copy(&buf, *reader) + stdC <- buf.String() + }() + return stdC + } + outC := copyStd(&rOut) + errC := copyStd(&rErr) // now run the command err = RunWithArgs(cmd, args, env) From 34bcb30f1cb9d14d141fe02be45ced9b082fd403 Mon Sep 17 00:00:00 2001 From: rigel rozanski Date: Tue, 20 Jun 2017 16:40:32 -0400 Subject: [PATCH 5/6] changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5b2a155..3db410cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,14 @@ # Changelog + ## Develop-Branch changes (unreleased) BREAKING CHANGES: - [run] NewBaseService takes the new logger +- [cli] RunCaptureWithArgs now captures stderr and stdout + - +func RunCaptureWithArgs(cmd Executable, args []string, env map[string]string) (stdout, stderr string, err error) + - -func RunCaptureWithArgs(cmd Executable, args []string, env map[string]string) (output string, err error) ## 0.2.1 (June 2, 2017) From f3eaf9b8703db67ddae0c7979a1610d340d6d382 Mon Sep 17 00:00:00 2001 From: rigel rozanski Date: Tue, 20 Jun 2017 16:52:22 -0400 Subject: [PATCH 6/6] quickfix --- cli/helper.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cli/helper.go b/cli/helper.go index 8bdb9e49..845c17db 100644 --- a/cli/helper.go +++ b/cli/helper.go @@ -73,13 +73,13 @@ func RunCaptureWithArgs(cmd Executable, args []string, env map[string]string) (s go func() { var buf bytes.Buffer // io.Copy will end when we call reader.Close() below - io.Copy(&buf, *reader) + io.Copy(&buf, reader) stdC <- buf.String() }() - return stdC + return &stdC } - outC := copyStd(&rOut) - errC := copyStd(&rErr) + outC := copyStd(rOut) + errC := copyStd(rErr) // now run the command err = RunWithArgs(cmd, args, env) @@ -87,7 +87,7 @@ func RunCaptureWithArgs(cmd Executable, args []string, env map[string]string) (s // and grab the stdout to return wOut.Close() wErr.Close() - stdout = <-outC - stderr = <-errC + stdout = <-*outC + stderr = <-*errC return stdout, stderr, err }