Merge pull request #25 from tendermint/stderr
CLI Execute Error prints to stderr
This commit is contained in:
commit
efb56aaea7
|
@ -1,2 +1,3 @@
|
||||||
|
*.swp
|
||||||
vendor
|
vendor
|
||||||
.glide
|
.glide
|
||||||
|
|
|
@ -2,6 +2,13 @@
|
||||||
|
|
||||||
## Develop-Branch changes (unreleased)
|
## 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)
|
||||||
|
|
||||||
FEATURES:
|
FEATURES:
|
||||||
|
|
||||||
- [common] Date range parsing from string (ex. "2015-12-31:2017-12-31")
|
- [common] Date range parsing from string (ex. "2015-12-31:2017-12-31")
|
||||||
|
|
|
@ -54,31 +54,40 @@ func RunWithArgs(cmd Executable, args []string, env map[string]string) error {
|
||||||
return cmd.Execute()
|
return cmd.Execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunCaptureWithArgs executes the given command with the specified command line args
|
// RunCaptureWithArgs executes the given command with the specified command
|
||||||
// and environmental variables set. It returns whatever was writen to
|
// line args and environmental variables set. It returns string fields
|
||||||
// stdout along with any error returned from cmd.Execute()
|
// representing output written to stdout and stderr, additionally any error
|
||||||
func RunCaptureWithArgs(cmd Executable, args []string, env map[string]string) (output string, err error) {
|
// from cmd.Execute() is also returned
|
||||||
old := os.Stdout // keep backup of the real stdout
|
func RunCaptureWithArgs(cmd Executable, args []string, env map[string]string) (stdout, stderr string, err error) {
|
||||||
r, w, _ := os.Pipe()
|
oldout, olderr := os.Stdout, os.Stderr // keep backup of the real stdout
|
||||||
os.Stdout = w
|
rOut, wOut, _ := os.Pipe()
|
||||||
|
rErr, wErr, _ := os.Pipe()
|
||||||
|
os.Stdout, os.Stderr = wOut, wErr
|
||||||
defer func() {
|
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
|
// copy the output in a separate goroutine so printing can't block indefinitely
|
||||||
|
copyStd := func(reader *os.File) *(chan string) {
|
||||||
|
stdC := make(chan string)
|
||||||
go func() {
|
go func() {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
// io.Copy will end when we call w.Close() below
|
// io.Copy will end when we call reader.Close() below
|
||||||
io.Copy(&buf, r)
|
io.Copy(&buf, reader)
|
||||||
outC <- buf.String()
|
stdC <- buf.String()
|
||||||
}()
|
}()
|
||||||
|
return &stdC
|
||||||
|
}
|
||||||
|
outC := copyStd(rOut)
|
||||||
|
errC := copyStd(rErr)
|
||||||
|
|
||||||
// now run the command
|
// now run the command
|
||||||
err = RunWithArgs(cmd, args, env)
|
err = RunWithArgs(cmd, args, env)
|
||||||
|
|
||||||
// and grab the stdout to return
|
// and grab the stdout to return
|
||||||
w.Close()
|
wOut.Close()
|
||||||
output = <-outC
|
wErr.Close()
|
||||||
return output, err
|
stdout = <-*outC
|
||||||
|
stderr = <-*errC
|
||||||
|
return stdout, stderr, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,9 +97,9 @@ func (e Executor) Execute() error {
|
||||||
err := e.Command.Execute()
|
err := e.Command.Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if viper.GetBool(TraceFlag) {
|
if viper.GetBool(TraceFlag) {
|
||||||
fmt.Printf("ERROR: %+v\n", err)
|
fmt.Fprintf(os.Stderr, "ERROR: %+v\n", err)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("ERROR:", err.Error())
|
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// return error code 1 by default, can override it with a special error type
|
// return error code 1 by default, can override it with a special error type
|
||||||
|
|
|
@ -223,9 +223,11 @@ func TestSetupTrace(t *testing.T) {
|
||||||
|
|
||||||
viper.Reset()
|
viper.Reset()
|
||||||
args := append([]string{cmd.Use}, tc.args...)
|
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)
|
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)
|
desired := fmt.Sprintf("ERROR: %s", tc.expected)
|
||||||
assert.Equal(desired, msg[0], i)
|
assert.Equal(desired, msg[0], i)
|
||||||
if tc.long && assert.True(len(msg) > 2, i) {
|
if tc.long && assert.True(len(msg) > 2, i) {
|
||||||
|
|
Loading…
Reference in New Issue