Skip to content

Commit

Permalink
Merge pull request #55 from kcmvp/ptmx
Browse files Browse the repository at this point in the history
#53:fix the bug /dev/ptmx is not available in docker
  • Loading branch information
kcmvp authored Jan 14, 2024
2 parents 0241644 + b8d7393 commit f0fe069
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions cmd/shared/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package shared
import (
"bufio"
"fmt"
"github.com/fatih/color" //nolint
"os"
"os/exec"
"regexp"
Expand All @@ -12,12 +13,18 @@ import (

func StreamCmdOutput(cmd *exec.Cmd, file string) error {
// Start the command with a pty
ptmx, err := pty.Start(cmd)
if err != nil {
fmt.Println("Error starting command:", err)
return err
var scanner *bufio.Scanner
if ptmx, err := pty.Start(cmd); err == nil {
scanner = bufio.NewScanner(ptmx)
defer ptmx.Close()
} else {
color.Yellow("device not support tty will fall back plain model")
if rd, err := cmd.StdoutPipe(); err == nil {
scanner = bufio.NewScanner(rd)
} else {
return err
}
}
defer ptmx.Close()

// Create a file to save the output
log, err := os.Create(file)
Expand All @@ -31,7 +38,6 @@ func StreamCmdOutput(cmd *exec.Cmd, file string) error {
colorRegex := regexp.MustCompile(`\x1b\[[0-9;]*m`)
// Goroutine to remove color escape sequences, print the colored output, and write the modified output to the file
go func() {
scanner := bufio.NewScanner(ptmx)
for scanner.Scan() {
line := scanner.Text()

Expand Down

0 comments on commit f0fe069

Please sign in to comment.