Add stdout trace for sh commands
This commit is contained in:
parent
f65d4ad879
commit
5ce73d2fc5
36
pew.go
36
pew.go
@ -5,6 +5,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -146,13 +147,38 @@ func successRate(state runstate) float64 {
|
|||||||
|
|
||||||
const pathDevNull = "/dev/null"
|
const pathDevNull = "/dev/null"
|
||||||
|
|
||||||
func sh(workdir, cmd string) (output string, err error) {
|
func sh(workdir, command string) (output string, err error) {
|
||||||
command := exec.Command("sh", "-c", "cd "+workdir+" && "+cmd)
|
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 {
|
if err != nil {
|
||||||
e := fmt.Sprintf("%v %v output: %v", cmd, err, output)
|
e := fmt.Sprintf("%v %v output: %v", cmd, err, output)
|
||||||
err = errors.New(e)
|
err = errors.New(e)
|
||||||
|
Loading…
Reference in New Issue
Block a user