Skip to content

Commit

Permalink
Merge pull request #65 from topolvm/stderr
Browse files Browse the repository at this point in the history
Embed stderr from running command in error
  • Loading branch information
pluser authored Apr 18, 2023
2 parents 8ef67ba + 24e64d7 commit e15c2d7
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions probe/diskmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"strconv"
)
Expand All @@ -22,26 +20,16 @@ func NewDiskMetrics(path string) DiskMetricsInterface {

func execWrap(stdin []byte, command string, args ...string) ([]byte, error) {
c := exec.Command(command, args...)
c.Stderr = os.Stderr
if stdin != nil {
c.Stdin = bytes.NewReader(stdin)
}
stdout, err := c.StdoutPipe()
if err != nil {
return nil, err
}
if err := c.Start(); err != nil {
return nil, err
}
out, err := io.ReadAll(stdout)
if err != nil {
return nil, err
var stdoutBuf, stderrBuf bytes.Buffer
c.Stdout = &stdoutBuf
c.Stderr = &stderrBuf
if err := c.Run(); err != nil {
return nil, fmt.Errorf("exec failed. stderr=%s, %w", stderrBuf.Bytes(), err)
}
if err := c.Wait(); err != nil {
return nil, err
}

return out, nil
return stdoutBuf.Bytes(), nil
}

func parseFioResult(fioOutput []byte, property string) (float64, error) {
Expand Down

0 comments on commit e15c2d7

Please sign in to comment.