waveterm/pkg/sstore/sstore.go

80 lines
1.8 KiB
Go
Raw Normal View History

2022-06-13 20:11:56 +02:00
package sstore
import (
2022-07-01 02:02:19 +02:00
"path"
2022-06-21 06:57:23 +02:00
"sync"
2022-06-13 20:11:56 +02:00
"time"
"github.com/google/uuid"
2022-07-01 02:02:19 +02:00
"github.com/scripthaus-dev/sh2-server/pkg/scbase"
2022-06-13 20:11:56 +02:00
)
var NextLineId = 10
2022-06-21 06:57:23 +02:00
var NextLineLock = &sync.Mutex{}
2022-06-13 20:11:56 +02:00
const LineTypeCmd = "cmd"
const LineTypeText = "text"
2022-07-01 02:02:19 +02:00
const DBFileName = "scripthaus.db"
func GetSessionDBName(sessionId string) string {
scHome := scbase.GetScHomeDir()
return path.Join(scHome, DBFileName)
}
2022-06-13 20:11:56 +02:00
2022-06-21 06:57:23 +02:00
type SessionType struct {
SessionId string `json:"sessionid"`
Remote string `json:"remote"`
Cwd string `json:"cwd"`
}
type WindowType struct {
SessionId string `json:"sessionid"`
WindowId string `json:"windowid"`
}
2022-06-13 20:11:56 +02:00
type LineType struct {
2022-06-17 00:51:41 +02:00
SessionId string `json:"sessionid"`
WindowId string `json:"windowid"`
2022-06-13 20:11:56 +02:00
LineId int `json:"lineid"`
Ts int64 `json:"ts"`
UserId string `json:"userid"`
LineType string `json:"linetype"`
Text string `json:"text,omitempty"`
CmdId string `json:"cmdid,omitempty"`
CmdText string `json:"cmdtext,omitempty"`
CmdRemote string `json:"cmdremote,omitempty"`
2022-06-21 06:57:23 +02:00
CmdCwd string `json:"cmdcwd,omitempty"`
2022-06-13 20:11:56 +02:00
}
2022-06-21 06:57:23 +02:00
func MakeNewLineCmd(sessionId string, windowId string) *LineType {
2022-06-13 20:11:56 +02:00
rtn := &LineType{}
2022-06-17 00:51:41 +02:00
rtn.SessionId = sessionId
rtn.WindowId = windowId
2022-06-21 06:57:23 +02:00
rtn.LineId = GetNextLine()
2022-06-13 20:11:56 +02:00
rtn.Ts = time.Now().UnixMilli()
rtn.UserId = "mike"
rtn.LineType = LineTypeCmd
rtn.CmdId = uuid.New().String()
return rtn
}
2022-06-17 00:51:41 +02:00
func MakeNewLineText(sessionId string, windowId string, text string) *LineType {
2022-06-13 20:11:56 +02:00
rtn := &LineType{}
2022-06-17 00:51:41 +02:00
rtn.SessionId = sessionId
rtn.WindowId = windowId
2022-06-21 06:57:23 +02:00
rtn.LineId = GetNextLine()
2022-06-13 20:11:56 +02:00
rtn.Ts = time.Now().UnixMilli()
rtn.UserId = "mike"
rtn.LineType = LineTypeText
rtn.Text = text
return rtn
}
2022-06-21 06:57:23 +02:00
func GetNextLine() int {
NextLineLock.Lock()
defer NextLineLock.Unlock()
rtn := NextLineId
NextLineId++
return rtn
}