Skip to content

Commit

Permalink
cleanup some used code, added code to monitor the spawned process
Browse files Browse the repository at this point in the history
  • Loading branch information
photostorm committed Sep 3, 2023
1 parent 6794a5b commit 3135450
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 81 deletions.
81 changes: 0 additions & 81 deletions cmd_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
package pty

import (
"bytes"
"errors"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -34,91 +31,13 @@ type windowExecCmd struct {
attrList *windows.ProcThreadAttributeListContainer
}

var errProcessNotStarted = errors.New("exec: process has not started yet")

func (c *windowExecCmd) close() error {
c.attrList.Delete()
_ = c.conPty.Close()

return nil
}

func (c *windowExecCmd) Run() error {
err := c.Start()
if err != nil {
return err
}

return c.Wait()
}

func (c *windowExecCmd) Wait() error {
if c.cmd.Process == nil {
return errProcessNotStarted
}

var err error

if c.waitCalled {
return errors.New("exec: wait was already called")
}

c.waitCalled = true
c.cmd.ProcessState, err = c.cmd.Process.Wait()
if err != nil {
return err
}

err = c.close()
if err != nil {
return err
}

if !c.cmd.ProcessState.Success() {
return &exec.ExitError{ProcessState: c.cmd.ProcessState}
}

return nil
}

func (c *windowExecCmd) StdinPipe() (io.WriteCloser, error) {
return nil, ErrUnsupported
}

func (c *windowExecCmd) StdoutPipe() (io.ReadCloser, error) {
return nil, ErrUnsupported
}

func (c *windowExecCmd) StderrPipe() (io.ReadCloser, error) {
return nil, ErrUnsupported
}

func (c *windowExecCmd) Output() ([]byte, error) {
if c.cmd.Stdout != nil {
return nil, errors.New("exec: Stdout already set")
}

var stdout bytes.Buffer
c.cmd.Stdout = &stdout

err := c.Run()
return stdout.Bytes(), err
}

func (c *windowExecCmd) CombinedOutput() ([]byte, error) {
if c.cmd.Stdout != nil {
return nil, errors.New("exec: Stdout already set")
}
if c.cmd.Stderr != nil {
return nil, errors.New("exec: Stderr already set")
}
var b bytes.Buffer
c.cmd.Stdout = &b
c.cmd.Stderr = &b
err := c.Run()
return b.Bytes(), err
}

func (c *windowExecCmd) argv() []string {
if len(c.cmd.Args) > 0 {
return c.cmd.Args
Expand Down
1 change: 1 addition & 0 deletions pty_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (p *WindowsPty) Close() error {
}

_, _, err = closePseudoConsole.Call(uintptr(p.handle))

return err
}

Expand Down
7 changes: 7 additions & 0 deletions run_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ func (c *windowExecCmd) Start() error {
if err != nil {
return err
}

go c.waitProcess(c.cmd.Process)

return nil
}

func (c *windowExecCmd) waitProcess(process *os.Process) {
_, _ = process.Wait()
_ = c.close()
}

0 comments on commit 3135450

Please sign in to comment.