From 0a127cac91548da303abd67e5ba5f97796170f90 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Sun, 15 Oct 2023 11:39:10 +0300 Subject: [PATCH] Migrate from goterm/term to creack/pty --- cli/cli.go | 2 +- cli/executor/executor.go | 53 ++++++++++++++++++++++++++-------------- go.mod | 2 +- go.sum | 4 +-- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 453c1d91..e0040272 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -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" ) diff --git a/cli/executor/executor.go b/cli/executor/executor.go index ffa34ff2..0cee2cd1 100644 --- a/cli/executor/executor.go +++ b/cli/executor/executor.go @@ -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" @@ -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 } // ////////////////////////////////////////////////////////////////////////////////// // @@ -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 { @@ -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 @@ -332,6 +355,7 @@ func execCommand(c *recipe.Command) (*CommandEnv, error) { err = cmdEnv.cmd.Start() if err != nil { + cmdEnv.term.Close() return nil, err } @@ -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: @@ -436,25 +460,18 @@ 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 @@ -462,14 +479,14 @@ 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 } } diff --git a/go.mod b/go.mod index 2b5089bd..7c332253 100644 --- a/go.mod +++ b/go.mod @@ -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 ( diff --git a/go.sum b/go.sum index 957543da..0562d89d 100644 --- a/go.sum +++ b/go.sum @@ -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=