updates, groundwork for tail, better parsing logic

This commit is contained in:
sawka 2022-06-14 14:17:36 -07:00
parent 1a3886c437
commit 65094ad0ec
2 changed files with 118 additions and 37 deletions

51
pkg/cmdtail/cmdtail.go Normal file
View 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
}

View File

@ -11,6 +11,7 @@ import (
"encoding/json"
"fmt"
"io"
"reflect"
"sync"
)
@ -21,6 +22,32 @@ const ErrorPacketStr = "error"
const MessagePacketStr = "message"
const CmdStartPacketStr = "cmdstart"
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 string `json:"type"`
@ -34,6 +61,35 @@ func MakePingPacket() *PingPacketType {
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 string `json:"type"`
Message string `json:"message"`
@ -104,6 +160,10 @@ func (ct *RunPacketType) GetType() string {
return RunPacketStr
}
func MakeRunPacket() *RunPacketType {
return &RunPacketType{Type: RunPacketStr}
}
type BarePacketType struct {
Type string `json:"type"`
}
@ -139,45 +199,15 @@ func ParseJsonPacket(jsonBuf []byte) (PacketType, error) {
if bareCmd.Type == "" {
return nil, fmt.Errorf("received packet with no type")
}
if bareCmd.Type == RunPacketStr {
var runPacket RunPacketType
err = json.Unmarshal(jsonBuf, &runPacket)
if err != nil {
return nil, err
}
return &runPacket, nil
pk, err := MakePacket(bareCmd.Type)
if err != nil {
return nil, err
}
if bareCmd.Type == PingPacketStr {
return MakePingPacket(), nil
err = json.Unmarshal(jsonBuf, pk)
if err != nil {
return nil, err
}
if bareCmd.Type == DonePacketStr {
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)
return pk, nil
}
func SendPacket(w io.Writer, packet PacketType) error {