Skip to content

Commit

Permalink
add output to hook executor (#625)
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Scherba <[email protected]>
  • Loading branch information
miklezzzz authored Jul 4, 2024
1 parent 1b68ec2 commit 6cdf376
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package executor

import (
"bytes"
"encoding/json"
"fmt"
"io"
"os/exec"
"strings"
"syscall"
Expand Down Expand Up @@ -29,6 +32,7 @@ func Run(cmd *exec.Cmd) error {

func RunAndLogLines(cmd *exec.Cmd, logLabels map[string]string) (*CmdUsage, error) {
// TODO observability
stdErr := bytes.NewBuffer(nil)
logEntry := log.WithFields(utils.LabelsToLogFields(logLabels))
stdoutLogEntry := logEntry.WithField("output", "stdout")
stderrLogEntry := logEntry.WithField("output", "stderr")
Expand All @@ -39,14 +43,17 @@ func RunAndLogLines(cmd *exec.Cmd, logLabels map[string]string) (*CmdUsage, erro
plo := &proxyJSONLogger{stdoutLogEntry, make([]byte, 0)}
ple := &proxyJSONLogger{stderrLogEntry, make([]byte, 0)}
cmd.Stdout = plo
cmd.Stderr = ple
cmd.Stderr = io.MultiWriter(ple, stdErr)
} else {
cmd.Stdout = stdoutLogEntry.Writer()
cmd.Stderr = stderrLogEntry.Writer()
cmd.Stderr = io.MultiWriter(stderrLogEntry.Writer(), stdErr)
}

err := cmd.Run()
if err != nil {
if len(stdErr.Bytes()) > 0 {
return nil, fmt.Errorf("%s", stdErr.String())
}
return nil, err
}

Expand Down

0 comments on commit 6cdf376

Please sign in to comment.