mirror of
https://github.com/wavetermdev/waveterm.git
synced 2024-12-22 16:48:23 +01:00
58 lines
1.8 KiB
Go
58 lines
1.8 KiB
Go
// Copyright 2024, Command Line Inc.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package blockservice
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/wavetermdev/thenextwave/pkg/cmdqueue"
|
|
"github.com/wavetermdev/thenextwave/pkg/filestore"
|
|
"github.com/wavetermdev/thenextwave/pkg/tsgen/tsgenmeta"
|
|
"github.com/wavetermdev/thenextwave/pkg/wshutil"
|
|
"github.com/wavetermdev/thenextwave/pkg/wstore"
|
|
)
|
|
|
|
type BlockService struct{}
|
|
|
|
const DefaultTimeout = 2 * time.Second
|
|
|
|
var BlockServiceInstance = &BlockService{}
|
|
|
|
func (bs *BlockService) SendCommand_Meta() tsgenmeta.MethodMeta {
|
|
return tsgenmeta.MethodMeta{
|
|
Desc: "send command to block",
|
|
ArgNames: []string{"blockid", "cmd"},
|
|
}
|
|
}
|
|
|
|
func (bs *BlockService) SendCommand(uiContext wstore.UIContext, blockId string, cmd wshutil.BlockCommand) error {
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
defer cancelFn()
|
|
_, err := cmdqueue.RunCmd(ctx, cmd, wshutil.CmdContextType{BlockId: blockId, TabId: uiContext.ActiveTabId})
|
|
return err
|
|
}
|
|
|
|
func (bs *BlockService) SaveTerminalState(ctx context.Context, blockId string, state string, stateType string, ptyOffset int64) error {
|
|
_, err := wstore.DBMustGet[*wstore.Block](ctx, blockId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if stateType != "full" && stateType != "preview" {
|
|
return fmt.Errorf("invalid state type: %q", stateType)
|
|
}
|
|
// ignore MakeFile error (already exists is ok)
|
|
filestore.WFS.MakeFile(ctx, blockId, "cache:term:"+stateType, nil, filestore.FileOptsType{})
|
|
err = filestore.WFS.WriteFile(ctx, blockId, "cache:term:"+stateType, []byte(state))
|
|
if err != nil {
|
|
return fmt.Errorf("cannot save terminal state: %w", err)
|
|
}
|
|
err = filestore.WFS.WriteMeta(ctx, blockId, "cache:term:"+stateType, filestore.FileMeta{"ptyoffset": ptyOffset}, true)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot save terminal state meta: %w", err)
|
|
}
|
|
return nil
|
|
}
|