better logical operation for xexec command output

This commit is contained in:
Julien Neuhart
2019-07-23 16:25:55 +02:00
parent c7d9e24c00
commit 0b2312a407
2 changed files with 7 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ package xexec
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
@@ -75,13 +76,12 @@ func pipe(logger xlog.Logger, cmd *exec.Cmd) error {
}
func logCommandOutput(logger xlog.Logger, reader io.ReadCloser, outputType string, cmd *exec.Cmd) {
var op string
if len(cmd.Args) >= 2 {
op = fmt.Sprintf("%s.%s.%s", cmd.Args[0], cmd.Args[1], outputType)
} else {
// len(cmd.Args) should always be >= 1.
op = fmt.Sprintf("%s.%s", cmd.Args[0], outputType)
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("%s", outputType))
for _, arg := range cmd.Args {
buf.WriteString(fmt.Sprintf(".%s", arg))
}
op := buf.String()
r := bufio.NewReader(reader)
defer reader.Close() // nolint: errcheck
for {

View File

@@ -27,7 +27,7 @@ func TestCommandContext(t *testing.T) {
logger := xlogtest.DebugLogger()
// should pipe command output as
// xlog.Logger has a xlog.DebugLevel.
cmd, err := CommandContext(context.Background(), logger, "echo")
cmd, err := CommandContext(context.Background(), logger, "echo", "Hello", "World")
assert.Nil(t, err)
LogBeforeExecute(logger, cmd)
// should not pipe command output as