From 44276b6901d578a650900a0ae7d5bd8030b09bb4 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 22 Apr 2022 20:18:01 +0800 Subject: [PATCH] feat: print output with colors --- hrp/convert.go | 16 +-------------- hrp/internal/builtin/utils.go | 37 +++++++++++++++++++++++++++-------- hrp/internal/pytest/main.go | 16 +-------------- hrp/internal/scaffold/main.go | 8 ++++---- hrp/runner_test.go | 9 +++++---- 5 files changed, 40 insertions(+), 46 deletions(-) diff --git a/hrp/convert.go b/hrp/convert.go index 2194dd51..828d3794 100644 --- a/hrp/convert.go +++ b/hrp/convert.go @@ -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 { diff --git a/hrp/internal/builtin/utils.go b/hrp/internal/builtin/utils.go index f328f1c4..a7ab624d 100644 --- a/hrp/internal/builtin/utils.go +++ b/hrp/internal/builtin/utils.go @@ -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 } diff --git a/hrp/internal/pytest/main.go b/hrp/internal/pytest/main.go index 667213d0..5b77eeef 100644 --- a/hrp/internal/pytest/main.go +++ b/hrp/internal/pytest/main.go @@ -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...) } diff --git a/hrp/internal/scaffold/main.go b/hrp/internal/scaffold/main.go index 808f3440..cb05cbe0 100644 --- a/hrp/internal/scaffold/main.go +++ b/hrp/internal/scaffold/main.go @@ -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 } diff --git a/hrp/runner_test.go b/hrp/runner_test.go index 9510a975..54c82f05 100644 --- a/hrp/runner_test.go +++ b/hrp/runner_test.go @@ -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) }