comment flow working

This commit is contained in:
sawka 2022-07-05 10:51:47 -07:00
parent 5b2e88ec32
commit 123fdfe3bb
3 changed files with 34 additions and 12 deletions

View File

@ -400,7 +400,7 @@ func HandleRunCommand(w http.ResponseWriter, r *http.Request) {
WriteJsonError(w, fmt.Errorf("invalid sessionid '%s': %w", commandPk.SessionId, err)) WriteJsonError(w, fmt.Errorf("invalid sessionid '%s': %w", commandPk.SessionId, err))
return return
} }
line, err := ProcessFeCommandPacket(&commandPk) line, err := ProcessFeCommandPacket(r.Context(), &commandPk)
if err != nil { if err != nil {
WriteJsonError(w, err) WriteJsonError(w, err)
return return
@ -409,14 +409,17 @@ func HandleRunCommand(w http.ResponseWriter, r *http.Request) {
return 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) commandStr := strings.TrimSpace(pk.CmdStr)
if commandStr == "" { if commandStr == "" {
return nil, fmt.Errorf("invalid emtpty command") return nil, fmt.Errorf("invalid emtpty command")
} }
if strings.HasPrefix(commandStr, "/comment ") { if strings.HasPrefix(commandStr, "/comment ") {
text := strings.TrimSpace(commandStr[9:]) 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 return rtnLine, nil
} }
if strings.HasPrefix(commandStr, "cd ") { if strings.HasPrefix(commandStr, "cd ") {
@ -430,10 +433,13 @@ func ProcessFeCommandPacket(pk *scpacket.FeCommandPacketType) (*sstore.LineType,
} }
return nil, nil 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 := packet.MakeRunPacket()
runPacket.CK = base.MakeCommandKey(pk.SessionId, rtnLine.CmdId) runPacket.CK = base.MakeCommandKey(pk.SessionId, rtnLine.CmdId)
runPacket.Cwd = "" runPacket.Cwd = pk.RemoteState.Cwd
runPacket.Env = nil runPacket.Env = nil
runPacket.Command = commandStr runPacket.Command = commandStr
fmt.Printf("run-packet %v\n", runPacket) fmt.Printf("run-packet %v\n", runPacket)

View File

@ -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) return fmt.Errorf("window not found, cannot insert line[%s/%s]", line.SessionId, line.WindowId)
} }
var maxLineId int 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) tx.GetWrap(&maxLineId, query, line.SessionId, line.WindowId)
line.LineId = maxLineId + 1 line.LineId = maxLineId + 1
query = `INSERT INTO line ( sessionid, windowid, lineid, ts, userid, linetype, text, cmdid) query = `INSERT INTO line ( sessionid, windowid, lineid, ts, userid, linetype, text, cmdid)

View File

@ -115,30 +115,46 @@ type CmdType struct {
RunOut packet.PacketType `json:"runout"` RunOut packet.PacketType `json:"runout"`
} }
func MakeNewLineCmd(sessionId string, windowId string) *LineType { func makeNewLineCmd(sessionId string, windowId string, userId string) *LineType {
rtn := &LineType{} rtn := &LineType{}
rtn.SessionId = sessionId rtn.SessionId = sessionId
rtn.WindowId = windowId rtn.WindowId = windowId
rtn.LineId = GetNextLine()
rtn.Ts = time.Now().UnixMilli() rtn.Ts = time.Now().UnixMilli()
rtn.UserId = "mike" rtn.UserId = userId
rtn.LineType = LineTypeCmd rtn.LineType = LineTypeCmd
rtn.CmdId = uuid.New().String() rtn.CmdId = uuid.New().String()
return rtn 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 := &LineType{}
rtn.SessionId = sessionId rtn.SessionId = sessionId
rtn.WindowId = windowId rtn.WindowId = windowId
rtn.LineId = GetNextLine()
rtn.Ts = time.Now().UnixMilli() rtn.Ts = time.Now().UnixMilli()
rtn.UserId = "mike" rtn.UserId = userId
rtn.LineType = LineTypeText rtn.LineType = LineTypeText
rtn.Text = text rtn.Text = text
return rtn 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 { func GetNextLine() int {
NextLineLock.Lock() NextLineLock.Lock()
defer NextLineLock.Unlock() defer NextLineLock.Unlock()