mirror of
https://github.com/wavetermdev/backup.git
synced 2026-08-05 13:57:07 -07:00
resize observer + run an ls command
This commit is contained in:
@@ -7,10 +7,14 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
"sync"
|
||||
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
"github.com/wavetermdev/thenextwave/pkg/eventbus"
|
||||
"github.com/wavetermdev/thenextwave/pkg/shellexec"
|
||||
"github.com/wavetermdev/thenextwave/pkg/util/shellutil"
|
||||
)
|
||||
|
||||
var globalLock = &sync.Mutex{}
|
||||
@@ -29,6 +33,16 @@ func (mc *MessageCommand) GetCommand() string {
|
||||
return "message"
|
||||
}
|
||||
|
||||
type RunCommand struct {
|
||||
Command string `json:"command"`
|
||||
CmdStr string `json:"cmdstr"`
|
||||
TermSize shellexec.TermSize `json:"termsize"`
|
||||
}
|
||||
|
||||
func (rc *RunCommand) GetCommand() string {
|
||||
return "run"
|
||||
}
|
||||
|
||||
type BlockController struct {
|
||||
BlockId string
|
||||
InputCh chan BlockCommand
|
||||
@@ -51,11 +65,45 @@ func ParseCmdMap(cmdMap map[string]any) (BlockCommand, error) {
|
||||
return nil, fmt.Errorf("error unmarshalling message command: %w", err)
|
||||
}
|
||||
return &cmd, nil
|
||||
case "run":
|
||||
var cmd RunCommand
|
||||
err := json.Unmarshal(mapJson, &cmd)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error unmarshalling run command: %w", err)
|
||||
}
|
||||
return &cmd, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown command type %q", cmdType)
|
||||
}
|
||||
}
|
||||
|
||||
func (bc *BlockController) StartShellCommand(rc *RunCommand) error {
|
||||
cmdStr := rc.CmdStr
|
||||
shellPath := shellutil.DetectLocalShellPath()
|
||||
ecmd := exec.Command(shellPath, "-c", cmdStr)
|
||||
log.Printf("running shell command: %q %q\n", shellPath, cmdStr)
|
||||
barr, err := shellexec.RunSimpleCmdInPty(ecmd, rc.TermSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for len(barr) > 0 {
|
||||
part := barr
|
||||
if len(part) > 4096 {
|
||||
part = part[:4096]
|
||||
}
|
||||
eventbus.SendEvent(application.WailsEvent{
|
||||
Name: "block:ptydata",
|
||||
Data: map[string]any{
|
||||
"blockid": bc.BlockId,
|
||||
"blockfile": "main",
|
||||
"ptydata": base64.StdEncoding.EncodeToString(part),
|
||||
},
|
||||
})
|
||||
barr = barr[len(part):]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (bc *BlockController) Run() {
|
||||
defer func() {
|
||||
eventbus.SendEvent(application.WailsEvent{
|
||||
@@ -81,6 +129,14 @@ func (bc *BlockController) Run() {
|
||||
"ptydata": base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("message %d\r\n", messageCount))),
|
||||
},
|
||||
})
|
||||
case *RunCommand:
|
||||
fmt.Printf("RUN: %s | %q\n", bc.BlockId, cmd.CmdStr)
|
||||
go func() {
|
||||
err := bc.StartShellCommand(cmd)
|
||||
if err != nil {
|
||||
log.Printf("error running shell command: %v\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
default:
|
||||
fmt.Printf("unknown command type %T\n", cmd)
|
||||
|
||||
Reference in New Issue
Block a user