feat: print output with colors

This commit is contained in:
debugtalk
2022-04-22 20:18:01 +08:00
parent 03bdc6020f
commit 44276b6901
5 changed files with 40 additions and 46 deletions

View File

@@ -1,9 +1,6 @@
package hrp
import (
"os/exec"
"strings"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
@@ -25,18 +22,7 @@ func convert2PyTestScripts(paths ...string) error {
}
args := append([]string{"-m", "httprunner", "make"}, paths...)
cmd := exec.Command(python3, args...)
log.Info().Str("cmd", cmd.String()).Msg("convert to pytest scripts")
output, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrap(err, "pytest running failed")
}
out := strings.TrimSpace(string(output))
println(out)
log.Info().Msg("convert to pytest scripts successfully")
return nil
return builtin.ExecCommand(python3, args...)
}
func convert2GoTestScripts(paths ...string) error {

View File

@@ -92,16 +92,37 @@ func EnsurePython3Venv(packages ...string) (string, error) {
return python3, nil
}
func ExecCommand(cmd *exec.Cmd, cwd string) error {
log.Info().Str("cmd", cmd.String()).Str("cwd", cwd).Msg("exec command")
cmd.Dir = cwd
output, err := cmd.CombinedOutput()
out := strings.TrimSpace(string(output))
func ExecCommandInDir(cmd *exec.Cmd, dir string) error {
log.Info().Str("cmd", cmd.String()).Str("dir", dir).Msg("exec command")
cmd.Dir = dir
// print output with colors
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Error().Err(err).Str("output", out).Msg("exec command failed")
} else if len(out) != 0 {
log.Info().Str("output", out).Msg("exec command success")
log.Error().Err(err).Msg("exec command failed")
return err
}
return nil
}
func ExecCommand(cmdName string, args ...string) error {
cmd := exec.Command(cmdName, args...)
log.Info().Str("cmd", cmd.String()).Msg("exec command")
// print output with colors
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Error().Err(err).Msg("exec command failed")
return err
}
return err
}

View File

@@ -1,11 +1,7 @@
package pytest
import (
"os/exec"
"strings"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/hrp/internal/builtin"
)
@@ -17,15 +13,5 @@ func RunPytest(args []string) error {
}
args = append([]string{"-m", "httprunner", "run"}, args...)
cmd := exec.Command(python3, args...)
log.Info().Str("cmd", cmd.String()).Msg("run pytest")
output, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrap(err, "pytest running failed")
}
out := strings.TrimSpace(string(output))
println(out)
return nil
return builtin.ExecCommand(python3, args...)
}

View File

@@ -133,7 +133,7 @@ func CreateScaffold(projectName string, pluginType PluginType) error {
func createGoPlugin(projectName string) error {
log.Info().Msg("start to create hashicorp go plugin")
// check go sdk
if err := builtin.ExecCommand(exec.Command("go", "version"), projectName); err != nil {
if err := builtin.ExecCommandInDir(exec.Command("go", "version"), projectName); err != nil {
return errors.Wrap(err, "go sdk not installed")
}
@@ -149,19 +149,19 @@ func createGoPlugin(projectName string) error {
}
// create go mod
if err := builtin.ExecCommand(exec.Command("go", "mod", "init", "plugin"), pluginDir); err != nil {
if err := builtin.ExecCommandInDir(exec.Command("go", "mod", "init", "plugin"), pluginDir); err != nil {
return err
}
// download plugin dependency
// funplugin version should be locked
funplugin := fmt.Sprintf("github.com/httprunner/funplugin@%s", shared.Version)
if err := builtin.ExecCommand(exec.Command("go", "get", funplugin), pluginDir); err != nil {
if err := builtin.ExecCommandInDir(exec.Command("go", "get", funplugin), pluginDir); err != nil {
return err
}
// build plugin debugtalk.bin
if err := builtin.ExecCommand(exec.Command("go", "build", "-o", filepath.Join("..", "debugtalk.bin"), "debugtalk.go"), pluginDir); err != nil {
if err := builtin.ExecCommandInDir(exec.Command("go", "build", "-o", filepath.Join("..", "debugtalk.bin"), "debugtalk.go"), pluginDir); err != nil {
return err
}

View File

@@ -2,20 +2,21 @@ package hrp
import (
"os"
"os/exec"
"testing"
"time"
"github.com/httprunner/httprunner/hrp/internal/scaffold"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/httprunner/httprunner/hrp/internal/builtin"
"github.com/httprunner/httprunner/hrp/internal/scaffold"
)
func buildHashicorpGoPlugin() {
log.Info().Msg("[init] build hashicorp go plugin")
cmd := exec.Command("go", "build",
err := builtin.ExecCommand("go", "build",
"-o", templatesDir+"debugtalk.bin", templatesDir+"plugin/debugtalk.go")
if err := cmd.Run(); err != nil {
if err != nil {
log.Error().Err(err).Msg("build hashicorp go plugin failed")
os.Exit(1)
}