feat: add supervisor prototype with embedded frontend
This commit is contained in:
53
internal/session/process.go
Normal file
53
internal/session/process.go
Normal file
@ -0,0 +1,53 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
|
||||
"github.com/creack/pty"
|
||||
)
|
||||
|
||||
type DefaultPTYFactory struct{}
|
||||
|
||||
type shellProcess struct {
|
||||
cmd *exec.Cmd
|
||||
pty *os.File
|
||||
}
|
||||
|
||||
func (f DefaultPTYFactory) Start(command string) (PTYProcess, error) {
|
||||
cmd := exec.Command("bash", "-lc", command)
|
||||
cmd.Env = os.Environ()
|
||||
ptmx, err := pty.Start(cmd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &shellProcess{cmd: cmd, pty: ptmx}, nil
|
||||
}
|
||||
|
||||
func (p *shellProcess) Read(b []byte) (int, error) {
|
||||
return p.pty.Read(b)
|
||||
}
|
||||
|
||||
func (p *shellProcess) Write(b []byte) (int, error) {
|
||||
return p.pty.Write(b)
|
||||
}
|
||||
|
||||
func (p *shellProcess) Close() error {
|
||||
return p.pty.Close()
|
||||
}
|
||||
|
||||
func (p *shellProcess) Wait() error {
|
||||
return p.cmd.Wait()
|
||||
}
|
||||
|
||||
func (p *shellProcess) Resize(cols, rows uint16) error {
|
||||
return pty.Setsize(p.pty, &pty.Winsize{Cols: cols, Rows: rows})
|
||||
}
|
||||
|
||||
func (p *shellProcess) SignalStop() error {
|
||||
if p.cmd.Process == nil {
|
||||
return nil
|
||||
}
|
||||
return p.cmd.Process.Signal(syscall.SIGTERM)
|
||||
}
|
||||
Reference in New Issue
Block a user