diff --git a/db/migrations/000001_init.up.sql b/db/migrations/000001_init.up.sql index 594b12f4..21ae524d 100644 --- a/db/migrations/000001_init.up.sql +++ b/db/migrations/000001_init.up.sql @@ -81,10 +81,13 @@ CREATE TABLE cmd ( ); CREATE TABLE history ( - sessionid varchar(36) NOT NULL, - windowid varchar(36) NOT NULL, - userid varchar(36) NOT NULL, + historyid varchar(36) PRIMARY KEY, ts bigint NOT NULL, - lineid varchar(36) NOT NULL, - PRIMARY KEY (sessionid, windowid, lineid) + userid varchar(36) NOT NULL, + sessionid varchar(36) NOT NULL, + screenid varchar(36) NOT NULL, + windowid varchar(36) NOT NULL, + lineid int NOT NULL, + cmdid varchar(36) NOT NULL, + cmdstr text NOT NULL ); diff --git a/db/schema.sql b/db/schema.sql index be05a168..1acc4fff 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -75,10 +75,13 @@ CREATE TABLE cmd ( PRIMARY KEY (sessionid, cmdid) ); CREATE TABLE history ( - sessionid varchar(36) NOT NULL, - windowid varchar(36) NOT NULL, - userid varchar(36) NOT NULL, + historyid varchar(36) PRIMARY KEY, ts bigint NOT NULL, - lineid varchar(36) NOT NULL, - PRIMARY KEY (sessionid, windowid, lineid) + userid varchar(36) NOT NULL, + sessionid varchar(36) NOT NULL, + screenid varchar(36) NOT NULL, + windowid varchar(36) NOT NULL, + lineid int NOT NULL, + cmdid varchar(36) NOT NULL, + cmdstr text NOT NULL ); diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index 22cc3471..0b27579c 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -278,6 +278,10 @@ func RunCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.U return sstore.LineUpdate{Line: rtnLine, Cmd: cmd}, nil } +func addToHistory(ctx context.Context, pk *scpacket.FeCommandPacketType, cmdStr string) error { + return nil +} + func EvalCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { if len(pk.Args) == 0 { return nil, fmt.Errorf("usage: /eval [command], no command passed to eval") @@ -287,6 +291,9 @@ func EvalCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore. if commandStr == "" { return nil, fmt.Errorf("/eval, invalid emtpty command") } + if !resolveBool(pk.Kwargs["nohist"], false) { + addToHistory(ctx, pk, pk.Args[0]) + } metaCmd := "" metaSubCmd := "" if commandStr == "cd" || strings.HasPrefix(commandStr, "cd ") { @@ -330,6 +337,7 @@ func EvalCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore. newPk.Kwargs[fields[0]] = fields[1] } } + return HandleCommand(ctx, newPk) } diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index a27b7acf..e88d6d97 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -85,6 +85,23 @@ func InsertRemote(ctx context.Context, remote *RemoteType) error { return nil } +func InsertHistoryItem(ctx context.Context, hitem *HistoryItemType) error { + if hitem == nil { + return fmt.Errorf("cannot insert nil history item") + } + db, err := GetDB(ctx) + if err != nil { + return err + } + query := `INSERT INTO history ( historyid, ts, userid, sessionid, screenid, windowid, lineid, cmdid, cmdstr) VALUES + (:historyid,:ts,:userid,:sessionid,:screenid,:windowid,:lineid,:cmdid,:cmdstr)` + _, err = db.NamedExec(query, hitem) + if err != nil { + return err + } + return nil +} + func GetBareSessions(ctx context.Context) ([]*SessionType, error) { var rtn []*SessionType err := WithTx(ctx, func(tx *TxWrap) error { diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index 1ba2bd85..265676c1 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -82,14 +82,13 @@ func (opts WindowOptsType) Value() (driver.Value, error) { } type WindowType struct { - SessionId string `json:"sessionid"` - WindowId string `json:"windowid"` - CurRemote string `json:"curremote"` - WinOpts WindowOptsType `json:"winopts"` - Lines []*LineType `json:"lines"` - Cmds []*CmdType `json:"cmds"` - History []*HistoryItemType `json:"history"` - Remotes []*RemoteInstance `json:"remotes"` + SessionId string `json:"sessionid"` + WindowId string `json:"windowid"` + CurRemote string `json:"curremote"` + WinOpts WindowOptsType `json:"winopts"` + Lines []*LineType `json:"lines"` + Cmds []*CmdType `json:"cmds"` + Remotes []*RemoteInstance `json:"remotes"` // only for updates Remove bool `json:"remove,omitempty"` @@ -158,8 +157,18 @@ type ScreenWindowType struct { } type HistoryItemType struct { - CmdStr string `json:"cmdstr"` - Remove bool `json:"remove"` + HistoryId string `json:"historyid"` + Ts int64 `json:"ts"` + UserId string `json:"userid"` + SessionId string `json:"sessionid"` + ScreenId string `json:"screenid"` + WindowId string `json:"windowid"` + LineId int64 `json:"lineid"` + CmdId string `json:"cmdid"` + CmdStr string `json:"cmdstr"` + + // only for updates + Remove bool `json:"remove"` } type RemoteState struct {