mirror of
https://github.com/wavetermdev/waveterm.git
synced 2024-12-22 16:48:23 +01:00
updates, groundwork for tail, better parsing logic
This commit is contained in:
parent
1a3886c437
commit
65094ad0ec
51
pkg/cmdtail/cmdtail.go
Normal file
51
pkg/cmdtail/cmdtail.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
// Copyright 2022 Dashborg Inc
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package cmdtail
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/fsnotify/fsnotify"
|
||||||
|
"github.com/scripthaus-dev/sh2-runner/pkg/packet"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TailPos struct {
|
||||||
|
CmdKey CmdKey
|
||||||
|
Pos int
|
||||||
|
RunOut bool
|
||||||
|
RunOutPos int
|
||||||
|
}
|
||||||
|
|
||||||
|
type CmdKey struct {
|
||||||
|
SessionId string
|
||||||
|
CmdId string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Tailer struct {
|
||||||
|
Lock *sync.Mutex
|
||||||
|
WatchList map[CmdKey]TailPos
|
||||||
|
Sessions map[string]bool
|
||||||
|
Watcher *fsnotify.Watcher
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeTailer() (*Tailer, error) {
|
||||||
|
rtn := &Tailer{
|
||||||
|
Lock: &sync.Mutex{},
|
||||||
|
WatchList: make(map[CmdKey]TailPos),
|
||||||
|
Sessions: make(map[string]bool),
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
rtn.Watcher, err = fsnotify.NewWatcher()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return rtn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddWatch(getPacket *packet.GetCmdPacketType) error {
|
||||||
|
return nil
|
||||||
|
}
|
@ -11,6 +11,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,6 +22,32 @@ const ErrorPacketStr = "error"
|
|||||||
const MessagePacketStr = "message"
|
const MessagePacketStr = "message"
|
||||||
const CmdStartPacketStr = "cmdstart"
|
const CmdStartPacketStr = "cmdstart"
|
||||||
const CmdDonePacketStr = "cmddone"
|
const CmdDonePacketStr = "cmddone"
|
||||||
|
const ListCmdPacketStr = "lscmd"
|
||||||
|
const GetCmdPacketStr = "getcmd"
|
||||||
|
|
||||||
|
var TypeStrToFactory map[string]reflect.Type
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
TypeStrToFactory = make(map[string]reflect.Type)
|
||||||
|
TypeStrToFactory[RunPacketStr] = reflect.TypeOf(RunPacketType{})
|
||||||
|
TypeStrToFactory[PingPacketStr] = reflect.TypeOf(PingPacketType{})
|
||||||
|
TypeStrToFactory[DonePacketStr] = reflect.TypeOf(DonePacketType{})
|
||||||
|
TypeStrToFactory[ErrorPacketStr] = reflect.TypeOf(ErrorPacketType{})
|
||||||
|
TypeStrToFactory[MessagePacketStr] = reflect.TypeOf(MessagePacketType{})
|
||||||
|
TypeStrToFactory[CmdStartPacketStr] = reflect.TypeOf(CmdStartPacketType{})
|
||||||
|
TypeStrToFactory[CmdDonePacketStr] = reflect.TypeOf(CmdDonePacketType{})
|
||||||
|
TypeStrToFactory[ListCmdPacketStr] = reflect.TypeOf(ListCmdPacketType{})
|
||||||
|
TypeStrToFactory[GetCmdPacketStr] = reflect.TypeOf(GetCmdPacketType{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakePacket(packetType string) (PacketType, error) {
|
||||||
|
rtype := TypeStrToFactory[packetType]
|
||||||
|
if rtype == nil {
|
||||||
|
return nil, fmt.Errorf("invalid packet type '%s'", packetType)
|
||||||
|
}
|
||||||
|
rtn := reflect.New(rtype)
|
||||||
|
return rtn.Interface().(PacketType), nil
|
||||||
|
}
|
||||||
|
|
||||||
type PingPacketType struct {
|
type PingPacketType struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
@ -34,6 +61,35 @@ func MakePingPacket() *PingPacketType {
|
|||||||
return &PingPacketType{Type: PingPacketStr}
|
return &PingPacketType{Type: PingPacketStr}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetCmdPacketType struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
SessionId string `json:"sessionid"`
|
||||||
|
CmdId string `json:"cmdid"`
|
||||||
|
Tail bool `json:"tail,omitempty"`
|
||||||
|
RunOut bool `json:"runout,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*GetCmdPacketType) GetType() string {
|
||||||
|
return GetCmdPacketStr
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeGetCmdPacket() *GetCmdPacketType {
|
||||||
|
return &GetCmdPacketType{Type: GetCmdPacketStr}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ListCmdPacketType struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
SessionId string `json:"sessionid"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*ListCmdPacketType) GetType() string {
|
||||||
|
return ListCmdPacketStr
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeListCmdPacket(sessionId string) *ListCmdPacketType {
|
||||||
|
return &ListCmdPacketType{Type: ListCmdPacketStr, SessionId: sessionId}
|
||||||
|
}
|
||||||
|
|
||||||
type MessagePacketType struct {
|
type MessagePacketType struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
@ -104,6 +160,10 @@ func (ct *RunPacketType) GetType() string {
|
|||||||
return RunPacketStr
|
return RunPacketStr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MakeRunPacket() *RunPacketType {
|
||||||
|
return &RunPacketType{Type: RunPacketStr}
|
||||||
|
}
|
||||||
|
|
||||||
type BarePacketType struct {
|
type BarePacketType struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
}
|
}
|
||||||
@ -139,45 +199,15 @@ func ParseJsonPacket(jsonBuf []byte) (PacketType, error) {
|
|||||||
if bareCmd.Type == "" {
|
if bareCmd.Type == "" {
|
||||||
return nil, fmt.Errorf("received packet with no type")
|
return nil, fmt.Errorf("received packet with no type")
|
||||||
}
|
}
|
||||||
if bareCmd.Type == RunPacketStr {
|
pk, err := MakePacket(bareCmd.Type)
|
||||||
var runPacket RunPacketType
|
if err != nil {
|
||||||
err = json.Unmarshal(jsonBuf, &runPacket)
|
return nil, err
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &runPacket, nil
|
|
||||||
}
|
}
|
||||||
if bareCmd.Type == PingPacketStr {
|
err = json.Unmarshal(jsonBuf, pk)
|
||||||
return MakePingPacket(), nil
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
if bareCmd.Type == DonePacketStr {
|
return pk, nil
|
||||||
return MakeDonePacket(), nil
|
|
||||||
}
|
|
||||||
if bareCmd.Type == ErrorPacketStr {
|
|
||||||
var errorPacket ErrorPacketType
|
|
||||||
err = json.Unmarshal(jsonBuf, &errorPacket)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &errorPacket, nil
|
|
||||||
}
|
|
||||||
if bareCmd.Type == CmdStartPacketStr {
|
|
||||||
var startPacket CmdStartPacketType
|
|
||||||
err = json.Unmarshal(jsonBuf, &startPacket)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &startPacket, nil
|
|
||||||
}
|
|
||||||
if bareCmd.Type == CmdDonePacketStr {
|
|
||||||
var donePacket CmdDonePacketType
|
|
||||||
err = json.Unmarshal(jsonBuf, &donePacket)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &donePacket, nil
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("invalid packet-type '%s'", bareCmd.Type)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendPacket(w io.Writer, packet PacketType) error {
|
func SendPacket(w io.Writer, packet PacketType) error {
|
||||||
|
Loading…
Reference in New Issue
Block a user