Skip to content

Commit

Permalink
Migrate from goterm/term to creack/pty
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 15, 2023
1 parent 9a6efbc commit 0a127ca
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
// Application info
const (
APP = "bibop"
VER = "7.5.1"
VER = "8.0.0"
DESC = "Utility for testing command-line tools"
)

Expand Down
53 changes: 35 additions & 18 deletions cli/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/essentialkaos/ek/v12/timeutil"
"github.com/essentialkaos/ek/v12/tmp"

"github.com/google/goterm/term"
"github.com/creack/pty"

"github.com/essentialkaos/bibop/action"
"github.com/essentialkaos/bibop/recipe"
Expand Down Expand Up @@ -71,7 +71,13 @@ type ValidationConfig struct {
type CommandEnv struct {
cmd *exec.Cmd
output *action.OutputContainer
pty *term.PTY
term *PTY
}

// PTY contains pseudo-terminal structs
type PTY struct {
pty *os.File
tty *os.File
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -201,6 +207,23 @@ func (e *Executor) Run(rr render.Renderer, r *recipe.Recipe, tags []string) bool

// ////////////////////////////////////////////////////////////////////////////////// //

// Close closes tty and pty
func (t *PTY) Close() {
if t == nil {
return
}

if t.pty != nil {
t.pty.Close()
}

if t.tty != nil {
t.pty.Close()
}
}

// ////////////////////////////////////////////////////////////////////////////////// //

// applyRecipeOptions applies recipe options to executor
func applyRecipeOptions(e *Executor, rr render.Renderer, r *recipe.Recipe) {
if r.HTTPSSkipVerify {
Expand Down Expand Up @@ -319,7 +342,7 @@ func execCommand(c *recipe.Command) (*CommandEnv, error) {
return nil, err
}

cmdEnv.pty, err = createPseudoTerminal(cmdEnv.cmd)
cmdEnv.term, err = createPTY(cmdEnv.cmd)

if err != nil {
return nil, err
Expand All @@ -332,6 +355,7 @@ func execCommand(c *recipe.Command) (*CommandEnv, error) {
err = cmdEnv.cmd.Start()

if err != nil {
cmdEnv.term.Close()
return nil, err
}

Expand Down Expand Up @@ -408,7 +432,7 @@ func runAction(a *recipe.Action, cmdEnv *CommandEnv) error {
case recipe.ACTION_EXPECT:
return action.Expect(a, cmdEnv.output)
case recipe.ACTION_PRINT:
return action.Input(a, cmdEnv.pty.Master, cmdEnv.output)
return action.Input(a, cmdEnv.term.pty, cmdEnv.output)
case recipe.ACTION_WAIT_OUTPUT:
return action.WaitOutput(a, cmdEnv.output)
case recipe.ACTION_OUTPUT_CONTAINS:
Expand Down Expand Up @@ -436,40 +460,33 @@ func runAction(a *recipe.Action, cmdEnv *CommandEnv) error {
return handler(a)
}

// createPseudoTerminal creates pseudo-terminal
func createPseudoTerminal(cmd *exec.Cmd) (*term.PTY, error) {
pty, err := term.OpenPTY()
// createPTY creates pseudo-terminal
func createPTY(cmd *exec.Cmd) (*PTY, error) {
pty, tty, err := pty.Open()

if err != nil {
return nil, err
}

termios := &term.Termios{}
termios.Raw()
termios.Set(pty.Slave)

cmd.Stdin, cmd.Stdout, cmd.Stderr = tty, tty, tty
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true}

cmd.Stdin = pty.Slave
cmd.Stdout = pty.Slave
cmd.Stderr = pty.Slave

return pty, nil
return &PTY{pty: pty, tty: tty}, nil
}

// outputIOLoop reads data from reader and writes it to output store
func outputIOLoop(cmdEnv *CommandEnv) {
buf := make([]byte, 8192)

for {
n, _ := cmdEnv.pty.Master.Read(buf[:cap(buf)])
n, _ := cmdEnv.term.pty.Read(buf[:cap(buf)])

if n > 0 {
cmdEnv.output.Write(buf[:n])
}

if cmdEnv.cmd.ProcessState != nil && cmdEnv.cmd.ProcessState.Exited() {
cmdEnv.pty.Close()
cmdEnv.term.Close()
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ go 1.18

require (
github.com/buger/jsonparser v1.1.1
github.com/creack/pty v1.1.18
github.com/essentialkaos/check v1.4.0
github.com/essentialkaos/depsy v1.1.0
github.com/essentialkaos/ek/v12 v12.80.0
github.com/google/goterm v0.0.0-20200907032337-555d40f16ae2
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/essentialkaos/check v1.4.0 h1:kWdFxu9odCxUqo1NNFNJmguGrDHgwi3A8daXX1nkuKk=
github.com/essentialkaos/check v1.4.0/go.mod h1:LMKPZ2H+9PXe7Y2gEoKyVAwUqXVgx7KtgibfsHJPus0=
github.com/essentialkaos/depsy v1.1.0 h1:U6dp687UkQwXlZU17Hg2KMxbp3nfZAoZ8duaeUFYvJI=
github.com/essentialkaos/depsy v1.1.0/go.mod h1:kpiTAV17dyByVnrbNaMcZt2jRwvuXClUYOzpyJQwtG8=
github.com/essentialkaos/ek/v12 v12.80.0 h1:Yy3VF9J18qdhMZt1Tl7fKtjLvg6PdZX5rcDdk92KNys=
github.com/essentialkaos/ek/v12 v12.80.0/go.mod h1:X0gkyjBCP4QiD+sV4D52aquLDLGUmHteMEL7Rsgbev0=
github.com/google/goterm v0.0.0-20200907032337-555d40f16ae2 h1:CVuJwN34x4xM2aT4sIKhmeib40NeBPhRihNjQmpJsA4=
github.com/google/goterm v0.0.0-20200907032337-555d40f16ae2/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down

0 comments on commit 0a127ca

Please sign in to comment.