diff --git a/cmd/gbc/artifact/plugin.go b/cmd/gbc/artifact/plugin.go index c34bcf3..4b47491 100644 --- a/cmd/gbc/artifact/plugin.go +++ b/cmd/gbc/artifact/plugin.go @@ -129,7 +129,7 @@ func (plugin Plugin) install() (string, error) { return pair }) task := fmt.Sprintf("%s installation", plugin.Name()) - if err := StreamCmdOutput(cmd, task, func(msg string) string { + if err := PtyCmdOutput(cmd, task, func(msg string) string { return "" }); err != nil { return tempGoPath, err @@ -160,7 +160,7 @@ func (plugin Plugin) Execute() error { } // always use absolute path pCmd := exec.Command(filepath.Join(GoPath(), plugin.Binary()), strings.Split(plugin.Args, " ")...) //nolint #gosec - if err := StreamCmdOutput(pCmd, plugin.taskName(), nil); err != nil { + if err := PtyCmdOutput(pCmd, plugin.taskName(), nil); err != nil { return err } if pCmd.ProcessState.ExitCode() != 0 { diff --git a/cmd/gbc/artifact/project.go b/cmd/gbc/artifact/project.go index 4b5a06b..646c63b 100644 --- a/cmd/gbc/artifact/project.go +++ b/cmd/gbc/artifact/project.go @@ -92,7 +92,7 @@ func init() { log.Fatal(color.RedString("please execute command in project root directory %s", string(output))) } cfg := &packages.Config{ - Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedFiles | packages.NeedTypesInfo | packages.NeedDeps | packages.NeedImports | packages.NeedSyntax, + Mode: packages.NeedName | packages.NeedTypes | packages.NeedFiles | packages.NeedTypesInfo, Dir: project.root, } project.pkgs, err = packages.Load(cfg, "./...") diff --git a/cmd/gbc/artifact/multiple_writer.go b/cmd/gbc/artifact/pty_writer.go similarity index 85% rename from cmd/gbc/artifact/multiple_writer.go rename to cmd/gbc/artifact/pty_writer.go index ec04831..4d26f52 100644 --- a/cmd/gbc/artifact/multiple_writer.go +++ b/cmd/gbc/artifact/pty_writer.go @@ -3,6 +3,7 @@ package artifact import ( "bufio" "fmt" + "io" "os" "os/exec" "path/filepath" @@ -18,17 +19,19 @@ import ( type consoleFormatter func(msg string) string -func StreamCmdOutput(cmd *exec.Cmd, task string, formatter consoleFormatter) error { +func PtyCmdOutput(cmd *exec.Cmd, task string, formatter consoleFormatter) error { // Start the command with a pty - var scanner *bufio.Scanner - if ptmx, err := pty.Start(cmd); err == nil { - scanner = bufio.NewScanner(ptmx) - defer ptmx.Close() - } else if rd, err := cmd.StdoutPipe(); err == nil { - scanner = bufio.NewScanner(rd) - } else { + rc, err := func() (io.ReadCloser, error) { + if Windows() { + return cmd.StdoutPipe() + } + return pty.Start(cmd) + }() + if err != nil { return err } + defer rc.Close() + scanner := bufio.NewScanner(rc) color.Green("start %s ......\n", task) // Create a file to save the output log, err := os.Create(filepath.Join(CurProject().Target(), fmt.Sprintf("%s.log", strings.ReplaceAll(task, " ", "_")))) diff --git a/cmd/gbc/command/build_action.go b/cmd/gbc/command/build_action.go index ff057ed..9be5c0f 100644 --- a/cmd/gbc/command/build_action.go +++ b/cmd/gbc/command/build_action.go @@ -126,7 +126,7 @@ func cleanAction(_ *cobra.Command, _ ...string) error { func testAction(_ *cobra.Command, _ ...string) error { coverProfile := fmt.Sprintf("-coverprofile=%s/cover.out", artifact.CurProject().Target()) testCmd := exec.Command("go", []string{"test", "-v", coverProfile, "./..."}...) //nolint - return artifact.StreamCmdOutput(testCmd, "test", nil) + return artifact.PtyCmdOutput(testCmd, "test", nil) } func coverReport(_ *cobra.Command, _ ...string) error {