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 ( import (
"bufio" "bufio"
"bytes"
"context" "context"
"fmt" "fmt"
"io" "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) { func logCommandOutput(logger xlog.Logger, reader io.ReadCloser, outputType string, cmd *exec.Cmd) {
var op string var buf bytes.Buffer
if len(cmd.Args) >= 2 { buf.WriteString(fmt.Sprintf("%s", outputType))
op = fmt.Sprintf("%s.%s.%s", cmd.Args[0], cmd.Args[1], outputType) for _, arg := range cmd.Args {
} else { buf.WriteString(fmt.Sprintf(".%s", arg))
// len(cmd.Args) should always be >= 1.
op = fmt.Sprintf("%s.%s", cmd.Args[0], outputType)
} }
op := buf.String()
r := bufio.NewReader(reader) r := bufio.NewReader(reader)
defer reader.Close() // nolint: errcheck defer reader.Close() // nolint: errcheck
for { for {

View File

@@ -27,7 +27,7 @@ func TestCommandContext(t *testing.T) {
logger := xlogtest.DebugLogger() logger := xlogtest.DebugLogger()
// should pipe command output as // should pipe command output as
// xlog.Logger has a xlog.DebugLevel. // 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) assert.Nil(t, err)
LogBeforeExecute(logger, cmd) LogBeforeExecute(logger, cmd)
// should not pipe command output as // should not pipe command output as