1
0

Add stdout trace for sh commands

This commit is contained in:
dump_stack() 2023-04-06 18:14:24 +00:00
parent f65d4ad879
commit 5ce73d2fc5
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC

36
pew.go
View File

@ -5,6 +5,7 @@
package main
import (
"bufio"
"database/sql"
"errors"
"fmt"
@ -146,13 +147,38 @@ func successRate(state runstate) float64 {
const pathDevNull = "/dev/null"
func sh(workdir, cmd string) (output string, err error) {
command := exec.Command("sh", "-c", "cd "+workdir+" && "+cmd)
func sh(workdir, command string) (output string, err error) {
flog := log.With().
Str("workdir", workdir).
Str("command", command).
Logger()
log.Debug().Msgf("%v", command)
cmd := exec.Command("sh", "-c", "cd "+workdir+" && "+command)
flog.Debug().Msgf("%v", cmd)
stdout, err := cmd.StdoutPipe()
if err != nil {
return
}
cmd.Stderr = cmd.Stdout
err = cmd.Start()
if err != nil {
return
}
go func() {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
m := scanner.Text()
output += m + "\n"
flog.Trace().Str("stdout", m).Msg("")
}
}()
err = cmd.Wait()
raw, err := command.CombinedOutput()
output = string(raw)
if err != nil {
e := fmt.Sprintf("%v %v output: %v", cmd, err, output)
err = errors.New(e)