feat: print stderr output

This commit is contained in:
lilong.129
2023-08-20 10:57:02 +08:00
parent 27e4f29d85
commit 4cd2c28550
+10 -9
View File
@@ -1,6 +1,7 @@
package myexec package myexec
import ( import (
"bytes"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
@@ -53,7 +54,7 @@ func ExecPython3Command(cmdName string, args ...string) error {
} }
func AssertPythonPackage(python3 string, pkgName, pkgVersion string) error { func AssertPythonPackage(python3 string, pkgName, pkgVersion string) error {
out, err := exec.Command( out, err := Command(
python3, "-c", fmt.Sprintf("import %s; print(%s.__version__)", pkgName, pkgName), python3, "-c", fmt.Sprintf("import %s; print(%s.__version__)", pkgName, pkgName),
).Output() ).Output()
if err != nil { if err != nil {
@@ -132,13 +133,13 @@ func RunCommand(cmdName string, args ...string) error {
} }
} }
// print output with colors // print stderr output
cmd.Stdout = os.Stdout var stderr bytes.Buffer
cmd.Stderr = os.Stderr cmd.Stderr = &stderr
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
log.Error().Err(err).Msg("exec command failed") log.Error().Err(err).Str("stderr", stderr.String()).Msg("exec command failed")
return err return err
} }
@@ -149,13 +150,13 @@ func ExecCommandInDir(cmd *exec.Cmd, dir string) error {
log.Info().Str("cmd", cmd.String()).Str("dir", dir).Msg("exec command") log.Info().Str("cmd", cmd.String()).Str("dir", dir).Msg("exec command")
cmd.Dir = dir cmd.Dir = dir
// print output with colors // print stderr output
cmd.Stdout = os.Stdout var stderr bytes.Buffer
cmd.Stderr = os.Stderr cmd.Stderr = &stderr
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
log.Error().Err(err).Msg("exec command failed") log.Error().Err(err).Str("stderr", stderr.String()).Msg("exec command failed")
return err return err
} }