diff --git a/cmd/main-server.go b/cmd/main-server.go index ce720d412..a1884287a 100644 --- a/cmd/main-server.go +++ b/cmd/main-server.go @@ -400,7 +400,7 @@ func HandleRunCommand(w http.ResponseWriter, r *http.Request) { WriteJsonError(w, fmt.Errorf("invalid sessionid '%s': %w", commandPk.SessionId, err)) return } - line, err := ProcessFeCommandPacket(&commandPk) + line, err := ProcessFeCommandPacket(r.Context(), &commandPk) if err != nil { WriteJsonError(w, err) return @@ -409,14 +409,17 @@ func HandleRunCommand(w http.ResponseWriter, r *http.Request) { return } -func ProcessFeCommandPacket(pk *scpacket.FeCommandPacketType) (*sstore.LineType, error) { +func ProcessFeCommandPacket(ctx context.Context, pk *scpacket.FeCommandPacketType) (*sstore.LineType, error) { commandStr := strings.TrimSpace(pk.CmdStr) if commandStr == "" { return nil, fmt.Errorf("invalid emtpty command") } if strings.HasPrefix(commandStr, "/comment ") { text := strings.TrimSpace(commandStr[9:]) - rtnLine := sstore.MakeNewLineText(pk.SessionId, pk.WindowId, text) + rtnLine, err := sstore.AddCommentLine(ctx, pk.SessionId, pk.WindowId, pk.UserId, text) + if err != nil { + return nil, err + } return rtnLine, nil } if strings.HasPrefix(commandStr, "cd ") { @@ -430,10 +433,13 @@ func ProcessFeCommandPacket(pk *scpacket.FeCommandPacketType) (*sstore.LineType, } return nil, nil } - rtnLine := sstore.MakeNewLineCmd(pk.SessionId, pk.WindowId) + rtnLine, err := sstore.AddCmdLine(ctx, pk.SessionId, pk.WindowId, pk.UserId) + if err != nil { + return nil, err + } runPacket := packet.MakeRunPacket() runPacket.CK = base.MakeCommandKey(pk.SessionId, rtnLine.CmdId) - runPacket.Cwd = "" + runPacket.Cwd = pk.RemoteState.Cwd runPacket.Env = nil runPacket.Command = commandStr fmt.Printf("run-packet %v\n", runPacket) diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index 5f9dbd864..1becc4a78 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -189,7 +189,7 @@ func InsertLine(ctx context.Context, line *LineType) error { return fmt.Errorf("window not found, cannot insert line[%s/%s]", line.SessionId, line.WindowId) } var maxLineId int - query = `SELECT max(lineid) FROM line WHERE sessionid = ? AND windowid = ?` + query = `SELECT COALESCE(max(lineid), 0) FROM line WHERE sessionid = ? AND windowid = ?` tx.GetWrap(&maxLineId, query, line.SessionId, line.WindowId) line.LineId = maxLineId + 1 query = `INSERT INTO line ( sessionid, windowid, lineid, ts, userid, linetype, text, cmdid) diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index d8696c8fb..e0cfb2d33 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -115,30 +115,46 @@ type CmdType struct { RunOut packet.PacketType `json:"runout"` } -func MakeNewLineCmd(sessionId string, windowId string) *LineType { +func makeNewLineCmd(sessionId string, windowId string, userId string) *LineType { rtn := &LineType{} rtn.SessionId = sessionId rtn.WindowId = windowId - rtn.LineId = GetNextLine() rtn.Ts = time.Now().UnixMilli() - rtn.UserId = "mike" + rtn.UserId = userId rtn.LineType = LineTypeCmd rtn.CmdId = uuid.New().String() return rtn } -func MakeNewLineText(sessionId string, windowId string, text string) *LineType { +func makeNewLineText(sessionId string, windowId string, userId string, text string) *LineType { rtn := &LineType{} rtn.SessionId = sessionId rtn.WindowId = windowId - rtn.LineId = GetNextLine() rtn.Ts = time.Now().UnixMilli() - rtn.UserId = "mike" + rtn.UserId = userId rtn.LineType = LineTypeText rtn.Text = text return rtn } +func AddCommentLine(ctx context.Context, sessionId string, windowId string, userId string, commentText string) (*LineType, error) { + rtnLine := makeNewLineText(sessionId, windowId, userId, commentText) + err := InsertLine(ctx, rtnLine) + if err != nil { + return nil, err + } + return rtnLine, nil +} + +func AddCmdLine(ctx context.Context, sessionId string, windowId string, userId string) (*LineType, error) { + rtnLine := makeNewLineCmd(sessionId, windowId, userId) + err := InsertLine(ctx, rtnLine) + if err != nil { + return nil, err + } + return rtnLine, nil +} + func GetNextLine() int { NextLineLock.Lock() defer NextLineLock.Unlock()