Skip to content

Commit

Permalink
Merge pull request #114 from kcmvp/pty
Browse files Browse the repository at this point in the history
#113: disable pty on windows
  • Loading branch information
kcmvp authored May 21, 2024
2 parents b1dd884 + 9b460f0 commit f46c1ec
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cmd/gbc/artifact/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/gbc/artifact/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "./...")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package artifact
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand All @@ -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, " ", "_"))))
Expand Down
2 changes: 1 addition & 1 deletion cmd/gbc/command/build_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f46c1ec

Please sign in to comment.