From ed607237204d2d0aa172871100ff7515634dde63 Mon Sep 17 00:00:00 2001 From: sawka Date: Tue, 14 Mar 2023 16:37:22 -0700 Subject: [PATCH] remove windowid --- cmd/main-server.go | 2 +- db/migrations/000010_removewindowid.down.sql | 0 db/migrations/000010_removewindowid.up.sql | 17 ++ db/schema.sql | 117 ++++----- pkg/cmdrunner/cmdrunner.go | 145 ++++++----- pkg/cmdrunner/resolver.go | 41 +--- pkg/cmdrunner/shparse.go | 4 +- pkg/remote/remote.go | 18 +- pkg/scpacket/scpacket.go | 1 - pkg/sstore/dbops.go | 240 +++++++++---------- pkg/sstore/migrate.go | 2 +- pkg/sstore/sstore.go | 40 +--- pkg/sstore/updatebus.go | 2 +- 13 files changed, 302 insertions(+), 327 deletions(-) create mode 100644 db/migrations/000010_removewindowid.down.sql create mode 100644 db/migrations/000010_removewindowid.up.sql diff --git a/cmd/main-server.go b/cmd/main-server.go index f6c2e374..57f3a618 100644 --- a/cmd/main-server.go +++ b/cmd/main-server.go @@ -546,7 +546,7 @@ func main() { } err = sstore.ReInitFocus(context.Background()) if err != nil { - log.Printf("[error] resetting window focus: %v\n", err) + log.Printf("[error] resetting screen focus: %v\n", err) } log.Printf("PCLOUD_ENDPOINT=%s\n", pcloud.GetEndpoint()) diff --git a/db/migrations/000010_removewindowid.down.sql b/db/migrations/000010_removewindowid.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/db/migrations/000010_removewindowid.up.sql b/db/migrations/000010_removewindowid.up.sql new file mode 100644 index 00000000..c131c7b5 --- /dev/null +++ b/db/migrations/000010_removewindowid.up.sql @@ -0,0 +1,17 @@ +ALTER TABLE remote_instance RENAME COLUMN windowid TO screenid; +ALTER TABLE line RENAME COLUMN windowid TO screenid; + +UPDATE remote_instance +SET screenid = (SELECT screen.screenid FROM screen WHERE screen.windowid = remote_instance.screenid) +WHERE screenid <> '' +; + +UPDATE line +SET screenid = (SELECT screen.screenid FROM screen WHERE screen.windowid = line.screenid) +WHERE screenid <> '' +; + +ALTER TABLE history DROP COLUMN windowid; +ALTER TABLE screen DROP COLUMN windowid; + + diff --git a/db/schema.sql b/db/schema.sql index aef3ecc2..059edcc6 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -16,53 +16,12 @@ CREATE TABLE session ( notifynum int NOT NULL, archived boolean NOT NULL, archivedts bigint NOT NULL, - ownerid varchar(36) NOT NULL, - sharemode varchar(12) NOT NULL, - accesskey varchar(36) NOT NULL -); -CREATE TABLE window ( - sessionid varchar(36) NOT NULL, - windowid varchar(36) NOT NULL, - curremoteownerid varchar(36) NOT NULL, - curremoteid varchar(36) NOT NULL, - curremotename varchar(50) NOT NULL, - nextlinenum int NOT NULL, - winopts json NOT NULL, - ownerid varchar(36) NOT NULL, - sharemode varchar(12) NOT NULL, - shareopts json NOT NULL, - PRIMARY KEY (sessionid, windowid) -); -CREATE TABLE screen ( - sessionid varchar(36) NOT NULL, - screenid varchar(36) NOT NULL, - name varchar(50) NOT NULL, - activewindowid varchar(36) NOT NULL, - screenidx int NOT NULL, - screenopts json NOT NULL, - ownerid varchar(36) NOT NULL, - sharemode varchar(12) NOT NULL, - incognito boolean NOT NULL, - archived boolean NOT NULL, - archivedts bigint NOT NULL, - PRIMARY KEY (sessionid, screenid) -); -CREATE TABLE screen_window ( - sessionid varchar(36) NOT NULL, - screenid varchar(36) NOT NULL, - windowid varchar(36) NOT NULL, - name varchar(50) NOT NULL, - layout json NOT NULL, - selectedline int NOT NULL, - anchor json NOT NULL, - focustype varchar(12) NOT NULL, - PRIMARY KEY (sessionid, screenid, windowid) -); + sharemode varchar(12) NOT NULL); CREATE TABLE remote_instance ( riid varchar(36) PRIMARY KEY, name varchar(50) NOT NULL, sessionid varchar(36) NOT NULL, - windowid varchar(36) NOT NULL, + screenid varchar(36) NOT NULL, remoteownerid varchar(36) NOT NULL, remoteid varchar(36) NOT NULL, festate json NOT NULL, @@ -84,7 +43,7 @@ CREATE TABLE state_diff ( ); CREATE TABLE line ( sessionid varchar(36) NOT NULL, - windowid varchar(36) NOT NULL, + screenid varchar(36) NOT NULL, userid varchar(36) NOT NULL, lineid varchar(36) NOT NULL, ts bigint NOT NULL, @@ -98,7 +57,7 @@ CREATE TABLE line ( contentheight int NOT NULL, star int NOT NULL, archived boolean NOT NULL, renderer varchar(50) NOT NULL DEFAULT '', bookmarked boolean NOT NULL DEFAULT 0, pinned boolean NOT NULL DEFAULT 0, - PRIMARY KEY (sessionid, windowid, lineid) + PRIMARY KEY (sessionid, screenid, lineid) ); CREATE TABLE remote ( remoteid varchar(36) PRIMARY KEY, @@ -145,7 +104,6 @@ CREATE TABLE history ( userid varchar(36) NOT NULL, sessionid varchar(36) NOT NULL, screenid varchar(36) NOT NULL, - windowid varchar(36) NOT NULL, lineid int NOT NULL, remoteownerid varchar(36) NOT NULL, remoteid varchar(36) NOT NULL, @@ -156,15 +114,6 @@ CREATE TABLE history ( ismetacmd boolean, incognito boolean ); -CREATE TABLE activity ( - day varchar(20) PRIMARY KEY, - uploaded boolean NOT NULL, - tdata json NOT NULL, - tzname varchar(50) NOT NULL, - tzoffset int NOT NULL, - clientversion varchar(20) NOT NULL, - clientarch varchar(20) NOT NULL -, buildtime varchar(20) NOT NULL DEFAULT '-', osrelease varchar(20) NOT NULL DEFAULT '-'); CREATE TABLE bookmark ( bookmarkid varchar(36) PRIMARY KEY, createdts bigint NOT NULL, @@ -185,3 +134,61 @@ CREATE TABLE bookmark_cmd ( cmdid varchar(36) NOT NULL, PRIMARY KEY (bookmarkid, sessionid, cmdid) ); +CREATE TABLE activity ( + day varchar(20) PRIMARY KEY, + uploaded boolean NOT NULL, + tdata json NOT NULL, + tzname varchar(50) NOT NULL, + tzoffset int NOT NULL, + clientversion varchar(50) NOT NULL, + clientarch varchar(50) NOT NULL +, buildtime varchar(20) NOT NULL DEFAULT '-', osrelease varchar(20) NOT NULL DEFAULT '-'); +CREATE TABLE playbook ( + playbookid varchar(36) PRIMARY KEY, + playbookname varchar(100) NOT NULL, + description text NOT NULL, + entryids json NOT NULL +); +CREATE TABLE playbook_entry ( + entryid varchar(36) PRIMARY KEY, + playbookid varchar(36) NOT NULL, + description text NOT NULL, + alias varchar(50) NOT NULL, + cmdstr text NOT NULL, + createdts bigint NOT NULL, + updatedts bigint NOT NULL +); +CREATE TABLE cloud_session ( + sessionid varchar(36) PRIMARY KEY, + viewkey varchar(50) NOT NULL, + writekey varchar(50) NOT NULL, + enckey varchar(100) NOT NULL, + enctype varchar(50) NOT NULL, + vts bigint NOT NULL, + acl json NOT NULL +); +CREATE TABLE cloud_update ( + updateid varchar(36) PRIMARY KEY, + ts bigint NOT NULL, + updatetype varchar(50) NOT NULL, + updatekeys json NOT NULL +); +CREATE TABLE IF NOT EXISTS "screen" ( + sessionid varchar(36) NOT NULL, + screenid varchar(36) NOT NULL, + name varchar(50) NOT NULL, + screenidx int NOT NULL, + screenopts json NOT NULL, + ownerid varchar(36) NOT NULL, + sharemode varchar(12) NOT NULL, + curremoteownerid varchar(36) NOT NULL, + curremoteid varchar(36) NOT NULL, + curremotename varchar(50) NOT NULL, + nextlinenum int NOT NULL, + selectedline int NOT NULL, + anchor json NOT NULL, + focustype varchar(12) NOT NULL, + archived boolean NOT NULL, + archivedts bigint NOT NULL, + PRIMARY KEY (sessionid, screenid) +); diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index 29e97303..332c8779 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -28,7 +28,7 @@ import ( ) const ( - HistoryTypeWindow = "window" + HistoryTypeScreen = "screen" HistoryTypeSession = "session" HistoryTypeGlobal = "global" ) @@ -51,25 +51,23 @@ var ColorNames = []string{"black", "red", "green", "yellow", "blue", "magenta", var RemoteColorNames = []string{"red", "green", "yellow", "blue", "magenta", "cyan", "white", "orange"} var RemoteSetArgs = []string{"alias", "connectmode", "key", "password", "autoinstall", "color"} -var WindowCmds = []string{"run", "comment", "cd", "cr", "clear", "sw", "reset", "signal"} +var ScreenCmds = []string{"run", "comment", "cd", "cr", "clear", "sw", "reset", "signal"} var NoHistCmds = []string{"_compgen", "line", "history", "_killserver"} var GlobalCmds = []string{"session", "screen", "remote", "set", "client", "telemetry", "bookmark", "bookmarks"} var SetVarNameMap map[string]string = map[string]string{ "tabcolor": "screen.tabcolor", - "pterm": "window.pterm", - "anchor": "sw.anchor", - "focus": "sw.focus", - "line": "sw.line", + "pterm": "screen.pterm", + "anchor": "screen.anchor", + "focus": "screen.focus", + "line": "screen.line", } var SetVarScopes = []SetVarScope{ SetVarScope{ScopeName: "global", VarNames: []string{}}, SetVarScope{ScopeName: "client", VarNames: []string{"telemetry"}}, SetVarScope{ScopeName: "session", VarNames: []string{"name", "pos"}}, - SetVarScope{ScopeName: "screen", VarNames: []string{"name", "tabcolor", "pos"}}, - SetVarScope{ScopeName: "window", VarNames: []string{"pterm"}}, - SetVarScope{ScopeName: "sw", VarNames: []string{"anchor", "focus", "line"}}, + SetVarScope{ScopeName: "screen", VarNames: []string{"name", "tabcolor", "pos", "pterm", "anchor", "focus", "line"}}, SetVarScope{ScopeName: "line", VarNames: []string{}}, // connection = remote, remote = remoteinstance SetVarScope{ScopeName: "connection", VarNames: []string{"alias", "connectmode", "key", "password", "autoinstall", "color"}}, @@ -322,7 +320,7 @@ func resolveNonNegInt(arg string, def int) (int, error) { } func RunCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window|R_RemoteConnected) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_RemoteConnected) if err != nil { return nil, fmt.Errorf("/run error: %w", err) } @@ -340,7 +338,7 @@ func RunCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.U } runPacket.Command = strings.TrimSpace(cmdStr) runPacket.ReturnState = resolveBool(pk.Kwargs["rtnstate"], isRtnStateCmd) - cmd, callback, err := remote.RunCommand(ctx, ids.SessionId, ids.WindowId, ids.Remote.RemotePtr, runPacket) + cmd, callback, err := remote.RunCommand(ctx, ids.SessionId, ids.ScreenId, ids.Remote.RemotePtr, runPacket) if callback != nil { defer callback() } @@ -358,7 +356,7 @@ func RunCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.U func addToHistory(ctx context.Context, pk *scpacket.FeCommandPacketType, historyContext historyContextType, isMetaCmd bool, hadError bool) error { cmdStr := firstArg(pk) - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen) if err != nil { return err } @@ -372,7 +370,6 @@ func addToHistory(ctx context.Context, pk *scpacket.FeCommandPacketType, history UserId: DefaultUserId, SessionId: ids.SessionId, ScreenId: ids.ScreenId, - WindowId: ids.WindowId, LineId: historyContext.LineId, HadError: hadError, CmdId: historyContext.CmdId, @@ -547,7 +544,7 @@ func ScreenSetCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ss if screen.SelectedLine > 0 { selectedLineStr = strconv.Itoa(int(screen.SelectedLine)) } - ritem, err := resolveLine(ctx, screen.SessionId, screen.WindowId, pk.Kwargs["line"], selectedLineStr) + ritem, err := resolveLine(ctx, screen.SessionId, screen.ScreenId, pk.Kwargs["line"], selectedLineStr) if err != nil { return nil, fmt.Errorf("/screen:set error resolving line: %v", err) } @@ -559,7 +556,7 @@ func ScreenSetCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ss updateMap[sstore.ScreenField_SelectedLine] = ritem.Num } if pk.Kwargs["anchor"] != "" { - m := swAnchorRe.FindStringSubmatch(pk.Kwargs["anchor"]) + m := screenAnchorRe.FindStringSubmatch(pk.Kwargs["anchor"]) if m == nil { return nil, fmt.Errorf("/screen:set invalid anchor argument (must be [line] or [line]:[offset])") } @@ -613,10 +610,10 @@ func ScreenCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstor return update, nil } -var swAnchorRe = regexp.MustCompile("^(\\d+)(?::(-?\\d+))?$") +var screenAnchorRe = regexp.MustCompile("^(\\d+)(?::(-?\\d+))?$") func RemoteInstallCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window|R_Remote) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Remote) if err != nil { return nil, err } @@ -630,7 +627,7 @@ func RemoteInstallCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) } func RemoteInstallCancelCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window|R_Remote) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Remote) if err != nil { return nil, err } @@ -644,7 +641,7 @@ func RemoteInstallCancelCommand(ctx context.Context, pk *scpacket.FeCommandPacke } func RemoteConnectCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window|R_Remote) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Remote) if err != nil { return nil, err } @@ -657,7 +654,7 @@ func RemoteConnectCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) } func RemoteDisconnectCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window|R_Remote) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Remote) if err != nil { return nil, err } @@ -907,7 +904,7 @@ func RemoteNewCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ss } func RemoteSetCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window|R_Remote) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Remote) if err != nil { return nil, err } @@ -937,7 +934,7 @@ func RemoteSetCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ss } func RemoteShowCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window|R_Remote) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Remote) if err != nil { return nil, err } @@ -1008,7 +1005,7 @@ func ScreenResetCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) ( sessionUpdate := &sstore.SessionType{SessionId: ids.SessionId} ris, err := sstore.ScreenReset(ctx, ids.ScreenId) if err != nil { - return nil, fmt.Errorf("error resetting screen window: %v", err) + return nil, fmt.Errorf("error resetting screen: %v", err) } sessionUpdate.Remotes = append(sessionUpdate.Remotes, ris...) err = sstore.UpdateCurRemote(ctx, ids.ScreenId, rptr) @@ -1032,7 +1029,7 @@ func ScreenResetCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) ( } func RemoteArchiveCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window|R_Remote) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Remote) if err != nil { return nil, err } @@ -1061,7 +1058,7 @@ func RemoteCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstor func crShowCommand(ctx context.Context, pk *scpacket.FeCommandPacketType, ids resolvedIds) (sstore.UpdatePacket, error) { var buf bytes.Buffer - riArr, err := sstore.GetRIsForWindow(ctx, ids.SessionId, ids.WindowId) + riArr, err := sstore.GetRIsForScreen(ctx, ids.SessionId, ids.ScreenId) if err != nil { return nil, fmt.Errorf("cannot get remote instances: %w", err) } @@ -1127,7 +1124,7 @@ func GetFullRemoteDisplayName(rptr *sstore.RemotePtrType, rstate *remote.RemoteR } func CrCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen) if err != nil { return nil, fmt.Errorf("/%s error: %w", GetCmdStr(pk), err) } @@ -1135,7 +1132,7 @@ func CrCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.Up if newRemote == "" { return crShowCommand(ctx, pk, ids) } - _, rptr, rstate, err := resolveRemote(ctx, newRemote, ids.SessionId, ids.WindowId) + _, rptr, rstate, err := resolveRemote(ctx, newRemote, ids.SessionId, ids.ScreenId) if err != nil { return nil, err } @@ -1197,7 +1194,7 @@ func makeStaticCmd(ctx context.Context, metaCmd string, ids resolvedIds, cmdStr } func addLineForCmd(ctx context.Context, metaCmd string, shouldFocus bool, ids resolvedIds, cmd *sstore.CmdType) (*sstore.ModelUpdate, error) { - rtnLine, err := sstore.AddCmdLine(ctx, ids.SessionId, ids.WindowId, DefaultUserId, cmd, "") + rtnLine, err := sstore.AddCmdLine(ctx, ids.SessionId, ids.ScreenId, DefaultUserId, cmd, "") if err != nil { return nil, err } @@ -1323,7 +1320,7 @@ func doCompGen(ctx context.Context, pk *scpacket.FeCommandPacketType, prefix str if !packet.IsValidCompGenType(compType) { return nil, false, fmt.Errorf("/_compgen invalid type '%s'", compType) } - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window|R_RemoteConnected) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_RemoteConnected) if err != nil { return nil, false, fmt.Errorf("/_compgen error: %w", err) } @@ -1396,7 +1393,7 @@ func CompGenCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ssto } func CommentCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen) if err != nil { return nil, fmt.Errorf("/comment error: %w", err) } @@ -1404,7 +1401,7 @@ func CommentCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ssto if strings.TrimSpace(text) == "" { return nil, fmt.Errorf("cannot post empty comment") } - rtnLine, err := sstore.AddCommentLine(ctx, ids.SessionId, ids.WindowId, DefaultUserId, text) + rtnLine, err := sstore.AddCommentLine(ctx, ids.SessionId, ids.ScreenId, DefaultUserId, text) if err != nil { return nil, err } @@ -1415,7 +1412,7 @@ func CommentCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ssto screen, err := sstore.UpdateScreen(ctx, ids.ScreenId, updateMap) if err != nil { // ignore error again (nothing to do) - log.Printf("/comment error updating screen-window selected line: %v\n", err) + log.Printf("/comment error updating screen selected line: %v\n", err) } update := sstore.ModelUpdate{Line: rtnLine, Screens: []*sstore.ScreenType{screen}} return update, nil @@ -1720,7 +1717,7 @@ func SessionCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ssto } func RemoteResetCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen) if err != nil { return nil, err } @@ -1732,7 +1729,7 @@ func RemoteResetCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) ( return nil, fmt.Errorf("invalid initpk received from remote (no remote state)") } feState := sstore.FeStateFromShellState(initPk.State) - remoteInst, err := sstore.UpdateRemoteState(ctx, ids.SessionId, ids.WindowId, ids.Remote.RemotePtr, *feState, initPk.State, nil) + remoteInst, err := sstore.UpdateRemoteState(ctx, ids.SessionId, ids.ScreenId, ids.Remote.RemotePtr, *feState, initPk.State, nil) if err != nil { return nil, err } @@ -1753,7 +1750,7 @@ func RemoteResetCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) ( } func ClearCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen) if err != nil { return nil, err } @@ -1804,7 +1801,7 @@ func HistoryPurgeCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) } lineObj := &sstore.LineType{ SessionId: historyItem.SessionId, - WindowId: historyItem.WindowId, + ScreenId: historyItem.ScreenId, LineId: historyItem.LineId, Remove: true, } @@ -1910,7 +1907,7 @@ func HistoryViewAllCommand(ctx context.Context, pk *scpacket.FeCommandPacketType const DefaultMaxHistoryItems = 10000 func HistoryCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window|R_Remote) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Remote) if err != nil { return nil, err } @@ -1924,22 +1921,22 @@ func HistoryCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ssto if maxItems == 0 { maxItems = DefaultMaxHistoryItems } - htype := HistoryTypeWindow + htype := HistoryTypeScreen hSessionId := ids.SessionId - hWindowId := ids.WindowId + hScreenId := ids.ScreenId if pk.Kwargs["type"] != "" { htype = pk.Kwargs["type"] - if htype != HistoryTypeWindow && htype != HistoryTypeSession && htype != HistoryTypeGlobal { - return nil, fmt.Errorf("invalid history type '%s', valid types: %s", htype, formatStrs([]string{HistoryTypeWindow, HistoryTypeSession, HistoryTypeGlobal}, "or", false)) + if htype != HistoryTypeScreen && htype != HistoryTypeSession && htype != HistoryTypeGlobal { + return nil, fmt.Errorf("invalid history type '%s', valid types: %s", htype, formatStrs([]string{HistoryTypeScreen, HistoryTypeSession, HistoryTypeGlobal}, "or", false)) } } if htype == HistoryTypeGlobal { hSessionId = "" - hWindowId = "" + hScreenId = "" } else if htype == HistoryTypeSession { - hWindowId = "" + hScreenId = "" } - hopts := sstore.HistoryQueryOpts{MaxItems: maxItems, SessionId: hSessionId, WindowId: hWindowId} + hopts := sstore.HistoryQueryOpts{MaxItems: maxItems, SessionId: hSessionId, ScreenId: hScreenId} hresult, err := sstore.GetHistoryItems(ctx, hopts) if err != nil { return nil, err @@ -1955,7 +1952,7 @@ func HistoryCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ssto update.History = &sstore.HistoryInfoType{ HistoryType: htype, SessionId: ids.SessionId, - WindowId: ids.WindowId, + ScreenId: ids.ScreenId, Items: hresult.Items, Show: show, } @@ -1992,25 +1989,25 @@ func resizeRunningCommand(ctx context.Context, cmd *sstore.CmdType, newCols int) } func ScreenResizeCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen) if err != nil { return nil, err } colsStr := pk.Kwargs["cols"] if colsStr == "" { - return nil, fmt.Errorf("/sw:resize requires a numeric 'cols' argument") + return nil, fmt.Errorf("/screen:resize requires a numeric 'cols' argument") } cols, err := strconv.Atoi(colsStr) if err != nil { - return nil, fmt.Errorf("/sw:resize requires a numeric 'cols' argument: %v", err) + return nil, fmt.Errorf("/screen:resize requires a numeric 'cols' argument: %v", err) } if cols <= 0 { - return nil, fmt.Errorf("/sw:resize invalid zero/negative 'cols' argument") + return nil, fmt.Errorf("/screen:resize invalid zero/negative 'cols' argument") } cols = base.BoundInt(cols, shexec.MinTermCols, shexec.MaxTermCols) - runningCmds, err := sstore.GetRunningWindowCmds(ctx, ids.SessionId, ids.WindowId) + runningCmds, err := sstore.GetRunningScreenCmds(ctx, ids.SessionId, ids.ScreenId) if err != nil { - return nil, fmt.Errorf("/sw:resize cannot get running commands: %v", err) + return nil, fmt.Errorf("/screen:resize cannot get running commands: %v", err) } if len(runningCmds) == 0 { return nil, nil @@ -2028,7 +2025,7 @@ func LineCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore. } func LineSetHeightCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen) if err != nil { return nil, err } @@ -2036,7 +2033,7 @@ func LineSetHeightCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) return nil, fmt.Errorf("/line:setheight requires 2 arguments (linearg and height)") } lineArg := pk.Args[0] - lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.WindowId, lineArg) + lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.ScreenId, lineArg) if err != nil { return nil, fmt.Errorf("error looking up lineid: %v", err) } @@ -2074,7 +2071,7 @@ func LineViewCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sst if err != nil { return nil, fmt.Errorf("/line:view could not get screen: %v", err) } - lineRItem, err := resolveLine(ctx, sessionId, screen.WindowId, lineArg, "") + lineRItem, err := resolveLine(ctx, sessionId, screen.ScreenId, lineArg, "") if err != nil { return nil, fmt.Errorf("/line:view invalid line arg: %v", err) } @@ -2179,7 +2176,7 @@ func BookmarkDeleteCommand(ctx context.Context, pk *scpacket.FeCommandPacketType } func LineBookmarkCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen) if err != nil { return nil, err } @@ -2187,14 +2184,14 @@ func LineBookmarkCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) return nil, fmt.Errorf("/line:bookmark requires an argument (line number or id)") } lineArg := pk.Args[0] - lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.WindowId, lineArg) + lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.ScreenId, lineArg) if err != nil { return nil, fmt.Errorf("error looking up lineid: %v", err) } if lineId == "" { return nil, fmt.Errorf("line %q not found", lineArg) } - lineObj, cmdObj, err := sstore.GetLineCmdByLineId(ctx, ids.SessionId, ids.WindowId, lineId) + lineObj, cmdObj, err := sstore.GetLineCmdByLineId(ctx, ids.SessionId, ids.ScreenId, lineId) if err != nil { return nil, fmt.Errorf("/line:bookmark error getting line: %v", err) } @@ -2215,7 +2212,7 @@ func LineBookmarkCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) if err != nil { return nil, fmt.Errorf("cannot insert bookmark: %v", err) } - newLineObj, err := sstore.GetLineById(ctx, ids.SessionId, ids.WindowId, lineId) + newLineObj, err := sstore.GetLineById(ctx, ids.SessionId, ids.ScreenId, lineId) if err != nil { return nil, fmt.Errorf("/line:bookmark error getting line: %v", err) } @@ -2231,7 +2228,7 @@ func LinePinCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ssto } func LineStarCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen) if err != nil { return nil, err } @@ -2242,7 +2239,7 @@ func LineStarCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sst return nil, fmt.Errorf("/line:star only takes up to 2 arguments (line-number and star-value)") } lineArg := pk.Args[0] - lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.WindowId, lineArg) + lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.ScreenId, lineArg) if err != nil { return nil, fmt.Errorf("error looking up lineid: %v", err) } @@ -2260,7 +2257,7 @@ func LineStarCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sst if err != nil { return nil, fmt.Errorf("/line:star error updating star value: %v", err) } - lineObj, err := sstore.GetLineById(ctx, ids.SessionId, ids.WindowId, lineId) + lineObj, err := sstore.GetLineById(ctx, ids.SessionId, ids.ScreenId, lineId) if err != nil { return nil, fmt.Errorf("/line:star error getting line: %v", err) } @@ -2272,7 +2269,7 @@ func LineStarCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sst } func LineArchiveCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen) if err != nil { return nil, err } @@ -2280,7 +2277,7 @@ func LineArchiveCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) ( return nil, fmt.Errorf("/line:archive requires an argument (line number or id)") } lineArg := pk.Args[0] - lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.WindowId, lineArg) + lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.ScreenId, lineArg) if err != nil { return nil, fmt.Errorf("error looking up lineid: %v", err) } @@ -2295,7 +2292,7 @@ func LineArchiveCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) ( if err != nil { return nil, fmt.Errorf("/line:archive error updating hidden status: %v", err) } - lineObj, err := sstore.GetLineById(ctx, ids.SessionId, ids.WindowId, lineId) + lineObj, err := sstore.GetLineById(ctx, ids.SessionId, ids.ScreenId, lineId) if err != nil { return nil, fmt.Errorf("/line:archive error getting line: %v", err) } @@ -2307,7 +2304,7 @@ func LineArchiveCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) ( } func LinePurgeCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen) if err != nil { return nil, err } @@ -2316,7 +2313,7 @@ func LinePurgeCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ss } var lineIds []string for _, lineArg := range pk.Args { - lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.WindowId, lineArg) + lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.ScreenId, lineArg) if err != nil { return nil, fmt.Errorf("error looking up lineid: %v", err) } @@ -2333,7 +2330,7 @@ func LinePurgeCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ss for _, lineId := range lineIds { lineObj := &sstore.LineType{ SessionId: ids.SessionId, - WindowId: ids.WindowId, + ScreenId: ids.ScreenId, LineId: lineId, Remove: true, } @@ -2343,7 +2340,7 @@ func LinePurgeCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ss } func LineShowCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen) if err != nil { return nil, err } @@ -2351,14 +2348,14 @@ func LineShowCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sst return nil, fmt.Errorf("/line:show requires an argument (line number or id)") } lineArg := pk.Args[0] - lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.WindowId, lineArg) + lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.ScreenId, lineArg) if err != nil { return nil, fmt.Errorf("error looking up lineid: %v", err) } if lineId == "" { return nil, fmt.Errorf("line %q not found", lineArg) } - line, cmd, err := sstore.GetLineCmdByLineId(ctx, ids.SessionId, ids.WindowId, lineId) + line, cmd, err := sstore.GetLineCmdByLineId(ctx, ids.SessionId, ids.ScreenId, lineId) if err != nil { return nil, fmt.Errorf("error getting line: %v", err) } @@ -2438,7 +2435,7 @@ func SetCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.U } func SignalCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen|R_Window) + ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen) if err != nil { return nil, err } @@ -2449,11 +2446,11 @@ func SignalCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstor return nil, fmt.Errorf("/signal requires a second argument (signal name)") } lineArg := pk.Args[0] - lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.WindowId, lineArg) + lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.ScreenId, lineArg) if err != nil { return nil, fmt.Errorf("error looking up lineid: %v", err) } - line, cmd, err := sstore.GetLineCmdByLineId(ctx, ids.SessionId, ids.WindowId, lineId) + line, cmd, err := sstore.GetLineCmdByLineId(ctx, ids.SessionId, ids.ScreenId, lineId) if err != nil { return nil, fmt.Errorf("error getting line: %v", err) } @@ -2837,7 +2834,7 @@ func isValidInScope(scopeName string, varName string) bool { } // returns (is-valid, scope, name) -// TODO write a full resolver to allow for indexed arguments. e.g. session[1].screen[1].window.pterm="25x80" +// TODO write a full resolver to allow for indexed arguments. e.g. session[1].screen[1].screen.pterm="25x80" func resolveSetArg(argName string) (bool, string, string) { dotIdx := strings.Index(argName, ".") if dotIdx == -1 { diff --git a/pkg/cmdrunner/resolver.go b/pkg/cmdrunner/resolver.go index 05a11b39..67f3ed6c 100644 --- a/pkg/cmdrunner/resolver.go +++ b/pkg/cmdrunner/resolver.go @@ -17,7 +17,6 @@ import ( const ( R_Session = 1 R_Screen = 2 - R_Window = 4 R_Remote = 8 R_RemoteConnected = 16 ) @@ -25,7 +24,6 @@ const ( type resolvedIds struct { SessionId string ScreenId string - WindowId string Remote *ResolvedRemote } @@ -203,7 +201,6 @@ func resolveUiIds(ctx context.Context, pk *scpacket.FeCommandPacketType, rtype i if uictx != nil { rtn.SessionId = uictx.SessionId rtn.ScreenId = uictx.ScreenId - rtn.WindowId = uictx.WindowId } if pk.Kwargs["session"] != "" { sessionId, err := resolveSessionArg(pk.Kwargs["session"]) @@ -223,15 +220,6 @@ func resolveUiIds(ctx context.Context, pk *scpacket.FeCommandPacketType, rtype i rtn.ScreenId = screenId } } - if pk.Kwargs["window"] != "" { - windowId, err := resolveWindowArg(rtn.SessionId, rtn.ScreenId, pk.Kwargs["window"]) - if err != nil { - return rtn, err - } - if windowId != "" { - rtn.WindowId = windowId - } - } var rptr *sstore.RemotePtrType var err error if pk.Kwargs["remote"] != "" { @@ -250,7 +238,7 @@ func resolveUiIds(ctx context.Context, pk *scpacket.FeCommandPacketType, rtype i if err != nil { return rtn, fmt.Errorf("invalid resolved remote: %v", err) } - rr, err := resolveRemoteFromPtr(ctx, rptr, rtn.SessionId, rtn.WindowId) + rr, err := resolveRemoteFromPtr(ctx, rptr, rtn.SessionId, rtn.ScreenId) if err != nil { return rtn, err } @@ -262,9 +250,6 @@ func resolveUiIds(ctx context.Context, pk *scpacket.FeCommandPacketType, rtype i if rtype&R_Screen > 0 && rtn.ScreenId == "" { return rtn, fmt.Errorf("no screen") } - if rtype&R_Window > 0 && rtn.WindowId == "" { - return rtn, fmt.Errorf("no window") - } if (rtype&R_Remote > 0 || rtype&R_RemoteConnected > 0) && rtn.Remote == nil { return rtn, fmt.Errorf("no remote") } @@ -274,7 +259,7 @@ func resolveUiIds(ctx context.Context, pk *scpacket.FeCommandPacketType, rtype i if err != nil { return rtn, fmt.Errorf("error trying to auto-connect remote [%s]: %w", rtn.Remote.DisplayName, err) } - rrNew, err := resolveRemoteFromPtr(ctx, rptr, rtn.SessionId, rtn.WindowId) + rrNew, err := resolveRemoteFromPtr(ctx, rptr, rtn.SessionId, rtn.ScreenId) if err != nil { return rtn, err } @@ -312,8 +297,8 @@ func resolveSession(ctx context.Context, sessionArg string, curSessionArg string return ritem, nil } -func resolveLine(ctx context.Context, sessionId string, windowId string, lineArg string, curLineArg string) (*ResolveItem, error) { - lines, err := sstore.GetLineResolveItems(ctx, sessionId, windowId) +func resolveLine(ctx context.Context, sessionId string, screenId string, lineArg string, curLineArg string) (*ResolveItem, error) { + lines, err := sstore.GetLineResolveItems(ctx, sessionId, screenId) if err != nil { return nil, fmt.Errorf("could not get lines: %v", err) } @@ -402,16 +387,6 @@ func resolveSessionId(pk *scpacket.FeCommandPacketType) (string, error) { return sessionId, nil } -func resolveWindowArg(sessionId string, screenId string, windowArg string) (string, error) { - if windowArg == "" { - return "", nil - } - if _, err := uuid.Parse(windowArg); err != nil { - return "", fmt.Errorf("invalid window arg specified (must be windowid) '%s'", windowArg) - } - return windowArg, nil -} - func resolveSessionArg(sessionArg string) (string, error) { if sessionArg == "" { return "", nil @@ -471,7 +446,7 @@ func parseFullRemoteRef(fullRemoteRef string) (string, string, string, error) { return fields[0], fields[1], fields[2], nil } -func resolveRemoteFromPtr(ctx context.Context, rptr *sstore.RemotePtrType, sessionId string, windowId string) (*ResolvedRemote, error) { +func resolveRemoteFromPtr(ctx context.Context, rptr *sstore.RemotePtrType, sessionId string, screenId string) (*ResolvedRemote, error) { if rptr == nil || rptr.RemoteId == "" { return nil, nil } @@ -491,8 +466,8 @@ func resolveRemoteFromPtr(ctx context.Context, rptr *sstore.RemotePtrType, sessi StatePtr: nil, FeState: nil, } - if sessionId != "" && windowId != "" { - ri, err := sstore.GetRemoteInstance(ctx, sessionId, windowId, *rptr) + if sessionId != "" && screenId != "" { + ri, err := sstore.GetRemoteInstance(ctx, sessionId, screenId, *rptr) if err != nil { log.Printf("ERROR resolving remote state '%s': %v\n", displayName, err) // continue with state set to nil @@ -510,7 +485,7 @@ func resolveRemoteFromPtr(ctx context.Context, rptr *sstore.RemotePtrType, sessi } // returns (remoteDisplayName, remoteptr, state, rstate, err) -func resolveRemote(ctx context.Context, fullRemoteRef string, sessionId string, windowId string) (string, *sstore.RemotePtrType, *remote.RemoteRuntimeState, error) { +func resolveRemote(ctx context.Context, fullRemoteRef string, sessionId string, screenId string) (string, *sstore.RemotePtrType, *remote.RemoteRuntimeState, error) { if fullRemoteRef == "" { return "", nil, nil, nil } diff --git a/pkg/cmdrunner/shparse.go b/pkg/cmdrunner/shparse.go index f6dfbabe..8d5aea0e 100644 --- a/pkg/cmdrunner/shparse.go +++ b/pkg/cmdrunner/shparse.go @@ -69,8 +69,6 @@ func SubMetaCmd(cmd string) string { switch cmd { case "s": return "screen" - case "w": - return "window" case "r": return "run" case "c": @@ -245,7 +243,7 @@ func EvalMetaCommand(ctx context.Context, origPk *scpacket.FeCommandPacketType) if err != nil { return nil, fmt.Errorf("parsing metacmd, position %v", err) } - envMap := make(map[string]string) // later we can add vars like session, window, screen, remote, and user + envMap := make(map[string]string) // later we can add vars like session, screen, remote, and user cfg := shexec.GetParserConfig(envMap) // process arguments for idx, w := range words { diff --git a/pkg/remote/remote.go b/pkg/remote/remote.go index b0ce19bf..23d3d971 100644 --- a/pkg/remote/remote.go +++ b/pkg/remote/remote.go @@ -117,7 +117,7 @@ type MShellProc struct { type RunCmdType struct { SessionId string - WindowId string + ScreenId string RemotePtr sstore.RemotePtrType RunPacket *packet.RunPacketType } @@ -1250,10 +1250,10 @@ func (msh *MShellProc) removePendingStateCmd(riName string, ck base.CommandKey) } // returns (cmdtype, allow-updates-callback, err) -func RunCommand(ctx context.Context, sessionId string, windowId string, remotePtr sstore.RemotePtrType, runPacket *packet.RunPacketType) (rtnCmd *sstore.CmdType, rtnCallback func(), rtnErr error) { +func RunCommand(ctx context.Context, sessionId string, screenId string, remotePtr sstore.RemotePtrType, runPacket *packet.RunPacketType) (rtnCmd *sstore.CmdType, rtnCallback func(), rtnErr error) { rct := RunCmdType{ SessionId: sessionId, - WindowId: windowId, + ScreenId: screenId, RemotePtr: remotePtr, RunPacket: runPacket, } @@ -1279,7 +1279,7 @@ func RunCommand(ctx context.Context, sessionId string, windowId string, remotePt } ok, existingPSC := msh.testAndSetPendingStateCmd(remotePtr.Name, newPSC) if !ok { - line, _, err := sstore.GetLineCmdByCmdId(ctx, sessionId, windowId, existingPSC.GetCmdId()) + line, _, err := sstore.GetLineCmdByCmdId(ctx, sessionId, screenId, existingPSC.GetCmdId()) if err != nil { return nil, nil, fmt.Errorf("cannot run command while a stateful command is still running: %v", err) } @@ -1298,7 +1298,7 @@ func RunCommand(ctx context.Context, sessionId string, windowId string, remotePt } }() // get current remote-instance state - statePtr, err := sstore.GetRemoteStatePtr(ctx, sessionId, windowId, remotePtr) + statePtr, err := sstore.GetRemoteStatePtr(ctx, sessionId, screenId, remotePtr) if err != nil { return nil, nil, fmt.Errorf("cannot get current remote stateptr: %w", err) } @@ -1371,7 +1371,7 @@ func (msh *MShellProc) reExecSingle(rct RunCmdType) { // TODO fixme ctx, cancelFn := context.WithTimeout(context.Background(), 15*time.Second) defer cancelFn() - _, callback, _ := RunCommand(ctx, rct.SessionId, rct.WindowId, rct.RemotePtr, rct.RunPacket) + _, callback, _ := RunCommand(ctx, rct.SessionId, rct.ScreenId, rct.RemotePtr, rct.RunPacket) if callback != nil { defer callback() } @@ -1501,7 +1501,7 @@ func (msh *MShellProc) handleCmdDonePacket(donePk *packet.CmdDonePacketType) { } screens, err := sstore.UpdateScreensWithCmdFg(context.Background(), donePk.CK.GetSessionId(), donePk.CK.GetCmdId()) if err != nil { - msh.WriteToPtyBuffer("*error trying to update cmd-fg screen windows: %v\n", err) + msh.WriteToPtyBuffer("*error trying to update cmd-fg screens: %v\n", err) // fall-through (nothing to do) } update.Screens = screens @@ -1509,7 +1509,7 @@ func (msh *MShellProc) handleCmdDonePacket(donePk *packet.CmdDonePacketType) { var statePtr *sstore.ShellStatePtr if donePk.FinalState != nil && rct != nil { feState := sstore.FeStateFromShellState(donePk.FinalState) - remoteInst, err := sstore.UpdateRemoteState(context.Background(), rct.SessionId, rct.WindowId, rct.RemotePtr, *feState, donePk.FinalState, nil) + remoteInst, err := sstore.UpdateRemoteState(context.Background(), rct.SessionId, rct.ScreenId, rct.RemotePtr, *feState, donePk.FinalState, nil) if err != nil { msh.WriteToPtyBuffer("*error trying to update remotestate: %v\n", err) // fall-through (nothing to do) @@ -1524,7 +1524,7 @@ func (msh *MShellProc) handleCmdDonePacket(donePk *packet.CmdDonePacketType) { msh.WriteToPtyBuffer("*error trying to update remotestate: %v\n", err) // fall-through (nothing to do) } else { - remoteInst, err := sstore.UpdateRemoteState(context.Background(), rct.SessionId, rct.WindowId, rct.RemotePtr, *feState, nil, donePk.FinalStateDiff) + remoteInst, err := sstore.UpdateRemoteState(context.Background(), rct.SessionId, rct.ScreenId, rct.RemotePtr, *feState, nil, donePk.FinalStateDiff) if err != nil { msh.WriteToPtyBuffer("*error trying to update remotestate: %v\n", err) // fall-through (nothing to do) diff --git a/pkg/scpacket/scpacket.go b/pkg/scpacket/scpacket.go index 8c1b8184..3e2bf633 100644 --- a/pkg/scpacket/scpacket.go +++ b/pkg/scpacket/scpacket.go @@ -52,7 +52,6 @@ func (pk *FeCommandPacketType) GetRawStr() string { type UIContextType struct { SessionId string `json:"sessionid"` ScreenId string `json:"screenid"` - WindowId string `json:"windowid"` Remote *sstore.RemotePtrType `json:"remote,omitempty"` WinSize *packet.WinSize `json:"winsize,omitempty"` Build string `json:"build,omitempty"` diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index 7e3a1eaf..b0986d0d 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -17,7 +17,7 @@ import ( "github.com/scripthaus-dev/sh2-server/pkg/scbase" ) -const HistoryCols = "historyid, ts, userid, sessionid, screenid, windowid, lineid, cmdid, haderror, cmdstr, remoteownerid, remoteid, remotename, ismetacmd, incognito" +const HistoryCols = "historyid, ts, userid, sessionid, screenid, lineid, cmdid, haderror, cmdstr, remoteownerid, remoteid, remotename, ismetacmd, incognito" const DefaultMaxHistoryItems = 1000 type SingleConnDBGetter struct { @@ -187,8 +187,8 @@ func InsertHistoryItem(ctx context.Context, hitem *HistoryItemType) error { } txErr := WithTx(ctx, func(tx *TxWrap) error { query := `INSERT INTO history - ( historyid, ts, userid, sessionid, screenid, windowid, lineid, cmdid, haderror, cmdstr, remoteownerid, remoteid, remotename, ismetacmd, incognito) VALUES - (:historyid,:ts,:userid,:sessionid,:screenid,:windowid,:lineid,:cmdid,:haderror,:cmdstr,:remoteownerid,:remoteid,:remotename,:ismetacmd,:incognito)` + ( historyid, ts, userid, sessionid, screenid, lineid, cmdid, haderror, cmdstr, remoteownerid, remoteid, remotename, ismetacmd, incognito) VALUES + (:historyid,:ts,:userid,:sessionid,:screenid,:lineid,:cmdid,:haderror,:cmdstr,:remoteownerid,:remoteid,:remotename,:ismetacmd,:incognito)` tx.NamedExec(query, hitem.ToMap()) return nil }) @@ -268,17 +268,17 @@ func runHistoryQueryWithFilter(tx *TxWrap, opts HistoryQueryOpts) (*HistoryQuery } func runHistoryQuery(tx *TxWrap, opts HistoryQueryOpts, realOffset int, itemLimit int) ([]*HistoryItemType, error) { - // check sessionid/windowid format because we are directly inserting them into the SQL + // check sessionid/screenid format because we are directly inserting them into the SQL if opts.SessionId != "" { _, err := uuid.Parse(opts.SessionId) if err != nil { return nil, fmt.Errorf("malformed sessionid") } } - if opts.WindowId != "" { - _, err := uuid.Parse(opts.WindowId) + if opts.ScreenId != "" { + _, err := uuid.Parse(opts.ScreenId) if err != nil { - return nil, fmt.Errorf("malformed windowid") + return nil, fmt.Errorf("malformed screenid") } } if opts.RemoteId != "" { @@ -290,8 +290,8 @@ func runHistoryQuery(tx *TxWrap, opts HistoryQueryOpts, realOffset int, itemLimi hnumStr := "" whereClause := "WHERE 1" var queryArgs []interface{} - if opts.SessionId != "" && opts.WindowId != "" { - whereClause += fmt.Sprintf(" AND sessionid = '%s' AND windowid = '%s'", opts.SessionId, opts.WindowId) + if opts.SessionId != "" && opts.ScreenId != "" { + whereClause += fmt.Sprintf(" AND sessionid = '%s' AND screenid = '%s'", opts.SessionId, opts.ScreenId) hnumStr = "w" } else if opts.SessionId != "" { whereClause += fmt.Sprintf(" AND sessionid = '%s'", opts.SessionId) @@ -419,20 +419,20 @@ func GetAllSessions(ctx context.Context) (*ModelUpdate, error) { func GetScreenLinesById(ctx context.Context, screenId string) (*ScreenLinesType, error) { return WithTxRtn(ctx, func(tx *TxWrap) (*ScreenLinesType, error) { - query := `SELECT sessionid, screenid, windowid FROM screen WHERE screenid = ?` + query := `SELECT sessionid, screenid FROM screen WHERE screenid = ?` screen := GetMappable[*ScreenLinesType](tx, query, screenId) if screen == nil { return nil, nil } - query = `SELECT * FROM line WHERE sessionid = ? AND windowid = ? ORDER BY linenum` - tx.Select(&screen.Lines, query, screen.SessionId, screen.WindowId) - query = `SELECT * FROM cmd WHERE cmdid IN (SELECT cmdid FROM line WHERE sessionid = ? AND windowid = ?)` - screen.Cmds = SelectMapsGen[*CmdType](tx, query, screen.SessionId, screen.WindowId) + query = `SELECT * FROM line WHERE sessionid = ? AND screenid = ? ORDER BY linenum` + tx.Select(&screen.Lines, query, screen.SessionId, screen.ScreenId) + query = `SELECT * FROM cmd WHERE cmdid IN (SELECT cmdid FROM line WHERE sessionid = ? AND screenid = ?)` + screen.Cmds = SelectMapsGen[*CmdType](tx, query, screen.SessionId, screen.ScreenId) return screen, nil }) } -// includes archived screens (does not include screen windows) +// includes archived screens func GetSessionScreens(ctx context.Context, sessionId string) ([]*ScreenType, error) { return WithTxRtn(ctx, func(tx *TxWrap) ([]*ScreenType, error) { query := `SELECT * FROM screen WHERE sessionid = ? ORDER BY archived, screenidx, archivedts` @@ -497,7 +497,7 @@ func InsertCloudSession(ctx context.Context, sessionName string, shareMode strin return updateRtn, nil } -// also creates default window, returns sessionId +// returns sessionId // if sessionName == "", it will be generated func InsertSessionWithName(ctx context.Context, sessionName string, shareMode string, activate bool) (*ModelUpdate, error) { var newScreen *ScreenType @@ -622,7 +622,6 @@ func InsertScreen(ctx context.Context, sessionId string, origScreenName string, if localRemoteId == "" { return fmt.Errorf("cannot create screen, no local remote found") } - newWindowId := scbase.GenPromptUUID() maxScreenIdx := tx.GetInt(`SELECT COALESCE(max(screenidx), 0) FROM screen WHERE sessionid = ? AND NOT archived`, sessionId) var screenName string if origScreenName == "" { @@ -635,7 +634,6 @@ func InsertScreen(ctx context.Context, sessionId string, origScreenName string, screen := &ScreenType{ SessionId: sessionId, ScreenId: newScreenId, - WindowId: newWindowId, Name: screenName, ScreenIdx: int64(maxScreenIdx) + 1, ScreenOpts: ScreenOptsType{}, @@ -649,7 +647,7 @@ func InsertScreen(ctx context.Context, sessionId string, origScreenName string, Archived: false, ArchivedTs: 0, } - query = `INSERT INTO screen (sessionid, screenid, windowid, name, screenidx, screenopts, ownerid, sharemode, curremoteownerid, curremoteid, curremotename, nextlinenum, selectedline, anchor, focustype, archived, archivedts) VALUES (:sessionid,:screenid,:windowid,:name,:screenidx,:screenopts,:ownerid,:sharemode,:curremoteownerid,:curremoteid,:curremotename,:nextlinenum,:selectedline,:anchor,:focustype,:archived,:archivedts)` + query = `INSERT INTO screen (sessionid, screenid, name, screenidx, screenopts, ownerid, sharemode, curremoteownerid, curremoteid, curremotename, nextlinenum, selectedline, anchor, focustype, archived, archivedts) VALUES (:sessionid,:screenid,:name,:screenidx,:screenopts,:ownerid,:sharemode,:curremoteownerid,:curremoteid,:curremotename,:nextlinenum,:selectedline,:anchor,:focustype,:archived,:archivedts)` tx.NamedExec(query, screen.ToMap()) if activate { query = `UPDATE session SET activescreenid = ? WHERE sessionid = ?` @@ -684,22 +682,22 @@ func GetScreenById(ctx context.Context, screenId string) (*ScreenType, error) { }) } -func FindLineIdByArg(ctx context.Context, sessionId string, windowId string, lineArg string) (string, error) { +func FindLineIdByArg(ctx context.Context, sessionId string, screenId string, lineArg string) (string, error) { var lineId string txErr := WithTx(ctx, func(tx *TxWrap) error { lineNum, err := strconv.Atoi(lineArg) if err == nil { // valid linenum - query := `SELECT lineid FROM line WHERE sessionid = ? AND windowid = ? AND linenum = ?` - lineId = tx.GetString(query, sessionId, windowId, lineNum) + query := `SELECT lineid FROM line WHERE sessionid = ? AND screenid = ? AND linenum = ?` + lineId = tx.GetString(query, sessionId, screenId, lineNum) } else if len(lineArg) == 8 { // prefix id string match - query := `SELECT lineid FROM line WHERE sessionid = ? AND windowid = ? AND substr(lineid, 1, 8) = ?` - lineId = tx.GetString(query, sessionId, windowId, lineArg) + query := `SELECT lineid FROM line WHERE sessionid = ? AND screenid = ? AND substr(lineid, 1, 8) = ?` + lineId = tx.GetString(query, sessionId, screenId, lineArg) } else { // id match - query := `SELECT lineid FROM line WHERE sessionid = ? AND windowid = ? AND lineid = ?` - lineId = tx.GetString(query, sessionId, windowId, lineArg) + query := `SELECT lineid FROM line WHERE sessionid = ? AND screenid = ? AND lineid = ?` + lineId = tx.GetString(query, sessionId, screenId, lineArg) } return nil }) @@ -709,11 +707,11 @@ func FindLineIdByArg(ctx context.Context, sessionId string, windowId string, lin return lineId, nil } -func GetLineCmdByLineId(ctx context.Context, sessionId string, windowId string, lineId string) (*LineType, *CmdType, error) { +func GetLineCmdByLineId(ctx context.Context, sessionId string, screenId string, lineId string) (*LineType, *CmdType, error) { return WithTxRtn3(ctx, func(tx *TxWrap) (*LineType, *CmdType, error) { var lineVal LineType - query := `SELECT * FROM line WHERE sessionid = ? AND windowid = ? AND lineid = ?` - found := tx.Get(&lineVal, query, sessionId, windowId, lineId) + query := `SELECT * FROM line WHERE sessionid = ? AND screenid = ? AND lineid = ?` + found := tx.Get(&lineVal, query, sessionId, screenId, lineId) if !found { return nil, nil, nil } @@ -726,11 +724,11 @@ func GetLineCmdByLineId(ctx context.Context, sessionId string, windowId string, }) } -func GetLineCmdByCmdId(ctx context.Context, sessionId string, windowId string, cmdId string) (*LineType, *CmdType, error) { +func GetLineCmdByCmdId(ctx context.Context, sessionId string, screenId string, cmdId string) (*LineType, *CmdType, error) { return WithTxRtn3(ctx, func(tx *TxWrap) (*LineType, *CmdType, error) { var lineVal LineType - query := `SELECT * FROM line WHERE sessionid = ? AND windowid = ? AND cmdid = ?` - found := tx.Get(&lineVal, query, sessionId, windowId, cmdId) + query := `SELECT * FROM line WHERE sessionid = ? AND screenid = ? AND cmdid = ?` + found := tx.Get(&lineVal, query, sessionId, screenId, cmdId) if !found { return nil, nil, nil } @@ -751,18 +749,18 @@ func InsertLine(ctx context.Context, line *LineType, cmd *CmdType) error { return fmt.Errorf("line should not hage linenum set") } return WithTx(ctx, func(tx *TxWrap) error { - query := `SELECT screenid FROM screen WHERE sessionid = ? AND windowid = ?` - if !tx.Exists(query, line.SessionId, line.WindowId) { - return fmt.Errorf("screen not found, cannot insert line[%s/%s]", line.SessionId, line.WindowId) + query := `SELECT screenid FROM screen WHERE sessionid = ? AND screenid = ?` + if !tx.Exists(query, line.SessionId, line.ScreenId) { + return fmt.Errorf("screen not found, cannot insert line[%s/%s]", line.SessionId, line.ScreenId) } - query = `SELECT nextlinenum FROM screen WHERE sessionid = ? AND windowid = ?` - nextLineNum := tx.GetInt(query, line.SessionId, line.WindowId) + query = `SELECT nextlinenum FROM screen WHERE sessionid = ? AND screenid = ?` + nextLineNum := tx.GetInt(query, line.SessionId, line.ScreenId) line.LineNum = int64(nextLineNum) - query = `INSERT INTO line ( sessionid, windowid, userid, lineid, ts, linenum, linenumtemp, linelocal, linetype, text, cmdid, renderer, ephemeral, contentheight, star, archived, bookmarked, pinned) - VALUES (:sessionid,:windowid,:userid,:lineid,:ts,:linenum,:linenumtemp,:linelocal,:linetype,:text,:cmdid,:renderer,:ephemeral,:contentheight,:star,:archived,:bookmarked,:pinned)` + query = `INSERT INTO line ( sessionid, screenid, userid, lineid, ts, linenum, linenumtemp, linelocal, linetype, text, cmdid, renderer, ephemeral, contentheight, star, archived, bookmarked, pinned) + VALUES (:sessionid,:screenid,:userid,:lineid,:ts,:linenum,:linenumtemp,:linelocal,:linetype,:text,:cmdid,:renderer,:ephemeral,:contentheight,:star,:archived,:bookmarked,:pinned)` tx.NamedExec(query, line) - query = `UPDATE screen SET nextlinenum = ? WHERE sessionid = ? AND windowid = ?` - tx.Exec(query, nextLineNum+1, line.SessionId, line.WindowId) + query = `UPDATE screen SET nextlinenum = ? WHERE sessionid = ? AND screenid = ?` + tx.Exec(query, nextLineNum+1, line.SessionId, line.ScreenId) if cmd != nil { cmd.OrigTermOpts = cmd.TermOpts cmdMap := cmd.ToMap() @@ -950,17 +948,17 @@ func cleanSessionCmds(ctx context.Context, sessionId string) error { return nil } -func CleanWindow(sessionId string, windowId string) { +func CleanScreen(sessionId string, screenId string) { // NOTE: context.Background() here! (this could take a long time, and is async) txErr := WithTx(context.Background(), func(tx *TxWrap) error { - query := `DELETE FROM history WHERE sessionid = ? AND windowid = ?` - tx.Exec(query, sessionId, windowId) - query = `DELETE FROM line WHERE sessionid = ? AND windowid = ?` - tx.Exec(query, sessionId, windowId) + query := `DELETE FROM history WHERE sessionid = ? AND screenid = ?` + tx.Exec(query, sessionId, screenId) + query = `DELETE FROM line WHERE sessionid = ? AND screenid = ?` + tx.Exec(query, sessionId, screenId) return cleanSessionCmds(tx.Context(), sessionId) }) if txErr != nil { - fmt.Printf("ERROR cleaning session:%s window:%s : %v\n", sessionId, windowId, txErr) + fmt.Printf("ERROR cleaning session:%s screen:%s : %v\n", sessionId, screenId, txErr) } } @@ -1024,28 +1022,28 @@ func UnArchiveScreen(ctx context.Context, sessionId string, screenId string) err } func DeleteScreen(ctx context.Context, screenId string) (UpdatePacket, error) { - var swkeys SWKeys + var sessionId string var isActive bool txErr := WithTx(ctx, func(tx *TxWrap) error { query := `SELECT screenid FROM screen WHERE screenid = ?` if !tx.Exists(query, screenId) { return fmt.Errorf("cannot purge screen (not found)") } - query = `SELECT sessionid, windowid FROM screen WHERE screenid = ?` - tx.Get(&swkeys, query, screenId) - if swkeys.SessionId == "" || swkeys.WindowId == "" { - return fmt.Errorf("cannot purge screen (no windowid)") + query = `SELECT sessionid FROM screen WHERE screenid = ?` + sessionId = tx.GetString(query, screenId) + if sessionId == "" { + return fmt.Errorf("cannot purge screen (no sessionid)") } query = `SELECT count(*) FROM screen WHERE sessionid = ? AND NOT archived` - numScreens := tx.GetInt(query, swkeys.SessionId) + numScreens := tx.GetInt(query, sessionId) if numScreens <= 1 { return fmt.Errorf("cannot purge the last screen in a session") } - isActive = tx.Exists(`SELECT sessionid FROM session WHERE sessionid = ? AND activescreenid = ?`, swkeys.SessionId, screenId) + isActive = tx.Exists(`SELECT sessionid FROM session WHERE sessionid = ? AND activescreenid = ?`, sessionId, screenId) if isActive { - screenIds := tx.SelectStrings(`SELECT screenid FROM screen WHERE sessionid = ? AND NOT archived ORDER BY screenidx`, swkeys.SessionId) + screenIds := tx.SelectStrings(`SELECT screenid FROM screen WHERE sessionid = ? AND NOT archived ORDER BY screenidx`, sessionId) nextId := getNextId(screenIds, screenId) - tx.Exec(`UPDATE session SET activescreenid = ? WHERE sessionid = ?`, nextId, swkeys.SessionId) + tx.Exec(`UPDATE session SET activescreenid = ? WHERE sessionid = ?`, nextId, sessionId) } query = `DELETE FROM screen WHERE screenid = ?` tx.Exec(query, screenId) @@ -1054,11 +1052,11 @@ func DeleteScreen(ctx context.Context, screenId string) (UpdatePacket, error) { if txErr != nil { return nil, txErr } - go CleanWindow(swkeys.SessionId, swkeys.WindowId) + go CleanScreen(sessionId, screenId) update := ModelUpdate{} - update.Screens = []*ScreenType{&ScreenType{SessionId: swkeys.SessionId, ScreenId: screenId, Remove: true}} + update.Screens = []*ScreenType{&ScreenType{SessionId: sessionId, ScreenId: screenId, Remove: true}} if isActive { - bareSession, err := GetBareSessionById(ctx, swkeys.SessionId) + bareSession, err := GetBareSessionById(ctx, sessionId) if err != nil { return nil, err } @@ -1067,8 +1065,8 @@ func DeleteScreen(ctx context.Context, screenId string) (UpdatePacket, error) { return update, nil } -func GetRemoteState(ctx context.Context, sessionId string, windowId string, remotePtr RemotePtrType) (*packet.ShellState, *ShellStatePtr, error) { - ssptr, err := GetRemoteStatePtr(ctx, sessionId, windowId, remotePtr) +func GetRemoteState(ctx context.Context, sessionId string, screenId string, remotePtr RemotePtrType) (*packet.ShellState, *ShellStatePtr, error) { + ssptr, err := GetRemoteStatePtr(ctx, sessionId, screenId, remotePtr) if err != nil { return nil, nil, err } @@ -1082,10 +1080,10 @@ func GetRemoteState(ctx context.Context, sessionId string, windowId string, remo return state, ssptr, err } -func GetRemoteStatePtr(ctx context.Context, sessionId string, windowId string, remotePtr RemotePtrType) (*ShellStatePtr, error) { +func GetRemoteStatePtr(ctx context.Context, sessionId string, screenId string, remotePtr RemotePtrType) (*ShellStatePtr, error) { var ssptr *ShellStatePtr txErr := WithTx(ctx, func(tx *TxWrap) error { - ri, err := GetRemoteInstance(tx.Context(), sessionId, windowId, remotePtr) + ri, err := GetRemoteInstance(tx.Context(), sessionId, screenId, remotePtr) if err != nil { return err } @@ -1101,30 +1099,30 @@ func GetRemoteStatePtr(ctx context.Context, sessionId string, windowId string, r return ssptr, nil } -func validateSessionWindow(tx *TxWrap, sessionId string, windowId string) error { - if windowId == "" { +func validateSessionScreen(tx *TxWrap, sessionId string, screenId string) error { + if screenId == "" { query := `SELECT sessionid FROM session WHERE sessionid = ?` if !tx.Exists(query, sessionId) { return fmt.Errorf("no session found") } return nil } else { - query := `SELECT windowid FROM screen WHERE sessionid = ? AND windowid = ?` - if !tx.Exists(query, sessionId, windowId) { + query := `SELECT screenid FROM screen WHERE sessionid = ? AND screenid = ?` + if !tx.Exists(query, sessionId, screenId) { return fmt.Errorf("no screen found") } return nil } } -func GetRemoteInstance(ctx context.Context, sessionId string, windowId string, remotePtr RemotePtrType) (*RemoteInstance, error) { +func GetRemoteInstance(ctx context.Context, sessionId string, screenId string, remotePtr RemotePtrType) (*RemoteInstance, error) { if remotePtr.IsSessionScope() { - windowId = "" + screenId = "" } var ri *RemoteInstance txErr := WithTx(ctx, func(tx *TxWrap) error { - query := `SELECT * FROM remote_instance WHERE sessionid = ? AND windowid = ? AND remoteownerid = ? AND remoteid = ? AND name = ?` - ri = GetMapGen[*RemoteInstance](tx, query, sessionId, windowId, remotePtr.OwnerId, remotePtr.RemoteId, remotePtr.Name) + query := `SELECT * FROM remote_instance WHERE sessionid = ? AND screenid = ? AND remoteownerid = ? AND remoteid = ? AND name = ?` + ri = GetMapGen[*RemoteInstance](tx, query, sessionId, screenId, remotePtr.OwnerId, remotePtr.RemoteId, remotePtr.Name) return nil }) if txErr != nil { @@ -1153,7 +1151,7 @@ func updateRIWithState(ctx context.Context, ri *RemoteInstance, stateBase *packe return nil } -func UpdateRemoteState(ctx context.Context, sessionId string, windowId string, remotePtr RemotePtrType, feState FeStateType, stateBase *packet.ShellState, stateDiff *packet.ShellStateDiff) (*RemoteInstance, error) { +func UpdateRemoteState(ctx context.Context, sessionId string, screenId string, remotePtr RemotePtrType, feState FeStateType, stateBase *packet.ShellState, stateDiff *packet.ShellStateDiff) (*RemoteInstance, error) { if stateBase == nil && stateDiff == nil { return nil, fmt.Errorf("UpdateRemoteState, must set state or diff") } @@ -1161,22 +1159,22 @@ func UpdateRemoteState(ctx context.Context, sessionId string, windowId string, r return nil, fmt.Errorf("UpdateRemoteState, cannot set state and diff") } if remotePtr.IsSessionScope() { - windowId = "" + screenId = "" } var ri *RemoteInstance txErr := WithTx(ctx, func(tx *TxWrap) error { - err := validateSessionWindow(tx, sessionId, windowId) + err := validateSessionScreen(tx, sessionId, screenId) if err != nil { return fmt.Errorf("cannot update remote instance state: %w", err) } - query := `SELECT * FROM remote_instance WHERE sessionid = ? AND windowid = ? AND remoteownerid = ? AND remoteid = ? AND name = ?` - ri = GetMapGen[*RemoteInstance](tx, query, sessionId, windowId, remotePtr.OwnerId, remotePtr.RemoteId, remotePtr.Name) + query := `SELECT * FROM remote_instance WHERE sessionid = ? AND screenid = ? AND remoteownerid = ? AND remoteid = ? AND name = ?` + ri = GetMapGen[*RemoteInstance](tx, query, sessionId, screenId, remotePtr.OwnerId, remotePtr.RemoteId, remotePtr.Name) if ri == nil { ri = &RemoteInstance{ RIId: scbase.GenPromptUUID(), Name: remotePtr.Name, SessionId: sessionId, - WindowId: windowId, + ScreenId: screenId, RemoteOwnerId: remotePtr.OwnerId, RemoteId: remotePtr.RemoteId, FeState: feState, @@ -1185,8 +1183,8 @@ func UpdateRemoteState(ctx context.Context, sessionId string, windowId string, r if err != nil { return err } - query = `INSERT INTO remote_instance ( riid, name, sessionid, windowid, remoteownerid, remoteid, festate, statebasehash, statediffhasharr) - VALUES (:riid,:name,:sessionid,:windowid,:remoteownerid,:remoteid,:festate,:statebasehash,:statediffhasharr)` + query = `INSERT INTO remote_instance ( riid, name, sessionid, screenid, remoteownerid, remoteid, festate, statebasehash, statediffhasharr) + VALUES (:riid,:name,:sessionid,:screenid,:remoteownerid,:remoteid,:festate,:statebasehash,:statediffhasharr)` tx.NamedExec(query, ri.ToMap()) return nil } else { @@ -1293,14 +1291,13 @@ func SetScreenName(ctx context.Context, sessionId string, screenId string, name func ArchiveScreenLines(ctx context.Context, screenId string) (*ModelUpdate, error) { txErr := WithTx(ctx, func(tx *TxWrap) error { - query := `SELECT sessionid, windowid FROM screen WHERE screenid = ?` - var swkeys SWKeys - tx.Get(&swkeys, query, screenId) - if swkeys.SessionId == "" || swkeys.WindowId == "" { - return fmt.Errorf("screen windowid does not exist") + query := `SELECT sessionid FROM screen WHERE screenid = ?` + sessionId := tx.GetString(query, screenId) + if sessionId == "" { + return fmt.Errorf("screen sessionid does not exist") } - query = `UPDATE line SET archived = 1 WHERE sessionid = ? AND windowid = ?` - tx.Exec(query, swkeys.SessionId, swkeys.WindowId) + query = `UPDATE line SET archived = 1 WHERE sessionid = ? AND screenid = ?` + tx.Exec(query, sessionId, screenId) return nil }) if txErr != nil { @@ -1315,19 +1312,19 @@ func ArchiveScreenLines(ctx context.Context, screenId string) (*ModelUpdate, err func PurgeScreenLines(ctx context.Context, screenId string) (*ModelUpdate, error) { var lineIds []string - var swkeys SWKeys + var sessionId string txErr := WithTx(ctx, func(tx *TxWrap) error { - query := `SELECT sessionid, windowid FROM screen WHERE screenid = ?` - tx.Get(&swkeys, query, screenId) - if swkeys.SessionId == "" || swkeys.WindowId == "" { - return fmt.Errorf("screen windowid does not exist") + query := `SELECT sessionid FROM screen WHERE screenid = ?` + sessionId = tx.GetString(query, screenId) + if sessionId == "" { + return fmt.Errorf("screen sessionid does not exist") } - query = `SELECT lineid FROM line WHERE sessionid = ? AND windowid = ?` - lineIds = tx.SelectStrings(query, swkeys.SessionId, swkeys.WindowId) - query = `DELETE FROM line WHERE sessionid = ? AND windowid = ?` - tx.Exec(query, swkeys.SessionId, swkeys.WindowId) - query = `DELETE FROM history WHERE sessionid = ? AND windowid = ?` - tx.Exec(query, swkeys.SessionId, swkeys.WindowId) + query = `SELECT lineid FROM line WHERE sessionid = ? AND screenid = ?` + lineIds = tx.SelectStrings(query, sessionId, screenId) + query = `DELETE FROM line WHERE sessionid = ? AND screenid = ?` + tx.Exec(query, sessionId, screenId) + query = `DELETE FROM history WHERE sessionid = ? AND screenid = ?` + tx.Exec(query, sessionId, screenId) query = `UPDATE screen SET nextlinenum = 1 WHERE screenid = ?` tx.Exec(query, screenId) return nil @@ -1335,7 +1332,7 @@ func PurgeScreenLines(ctx context.Context, screenId string) (*ModelUpdate, error if txErr != nil { return nil, txErr } - go cleanSessionCmds(context.Background(), swkeys.SessionId) + go cleanSessionCmds(context.Background(), sessionId) screen, err := GetScreenById(ctx, screenId) if err != nil { return nil, err @@ -1346,8 +1343,8 @@ func PurgeScreenLines(ctx context.Context, screenId string) (*ModelUpdate, error } for _, lineId := range lineIds { line := &LineType{ - SessionId: swkeys.SessionId, - WindowId: swkeys.WindowId, + SessionId: sessionId, + ScreenId: screenId, LineId: lineId, Remove: true, } @@ -1356,11 +1353,11 @@ func PurgeScreenLines(ctx context.Context, screenId string) (*ModelUpdate, error return &ModelUpdate{Screens: []*ScreenType{screen}, ScreenLines: screenLines}, nil } -func GetRunningWindowCmds(ctx context.Context, sessionId string, windowId string) ([]*CmdType, error) { +func GetRunningScreenCmds(ctx context.Context, sessionId string, screenId string) ([]*CmdType, error) { var rtn []*CmdType txErr := WithTx(ctx, func(tx *TxWrap) error { - query := `SELECT * from cmd WHERE cmdid IN (SELECT cmdid FROM line WHERE sessionid = ? AND windowid = ?) AND status = ?` - rtn = SelectMapsGen[*CmdType](tx, query, sessionId, windowId, CmdStatusRunning) + query := `SELECT * from cmd WHERE cmdid IN (SELECT cmdid FROM line WHERE sessionid = ? AND screenid = ?) AND status = ?` + rtn = SelectMapsGen[*CmdType](tx, query, sessionId, screenId, CmdStatusRunning) return nil }) if txErr != nil { @@ -1381,21 +1378,20 @@ func UpdateCmdTermOpts(ctx context.Context, sessionId string, cmdId string, term // returns riids of deleted RIs func ScreenReset(ctx context.Context, screenId string) ([]*RemoteInstance, error) { return WithTxRtn(ctx, func(tx *TxWrap) ([]*RemoteInstance, error) { - var swkeys SWKeys - query := `SELECT sessionid, windowid FROM screen WHERE screenid = ?` - tx.Get(&swkeys, query, screenId) - if swkeys.SessionId == "" || swkeys.WindowId == "" { + query := `SELECT sessionid FROM screen WHERE screenid = ?` + sessionId := tx.GetString(query, screenId) + if sessionId == "" { return nil, fmt.Errorf("screen does not exist") } - query = `SELECT riid FROM remote_instance WHERE sessionid = ? AND windowid = ?` - riids := tx.SelectStrings(query, swkeys.SessionId, swkeys.WindowId) + query = `SELECT riid FROM remote_instance WHERE sessionid = ? AND screenid = ?` + riids := tx.SelectStrings(query, sessionId, screenId) var delRis []*RemoteInstance for _, riid := range riids { - ri := &RemoteInstance{SessionId: swkeys.SessionId, WindowId: swkeys.WindowId, RIId: riid, Remove: true} + ri := &RemoteInstance{SessionId: sessionId, ScreenId: screenId, RIId: riid, Remove: true} delRis = append(delRis, ri) } - query = `DELETE FROM remote_instance WHERE sessionid = ? AND windowid = ?` - tx.Exec(query, swkeys.SessionId, swkeys.WindowId) + query = `DELETE FROM remote_instance WHERE sessionid = ? AND screenid = ?` + tx.Exec(query, sessionId, screenId) return delRis, nil }) } @@ -1669,11 +1665,11 @@ func UpdateScreen(ctx context.Context, screenId string, editMap map[string]inter return GetScreenById(ctx, screenId) } -func GetLineResolveItems(ctx context.Context, sessionId string, windowId string) ([]ResolveItem, error) { +func GetLineResolveItems(ctx context.Context, sessionId string, screenId string) ([]ResolveItem, error) { var rtn []ResolveItem txErr := WithTx(ctx, func(tx *TxWrap) error { - query := `SELECT lineid as id, linenum as num FROM line WHERE sessionid = ? AND windowid = ? ORDER BY linenum` - tx.Select(&rtn, query, sessionId, windowId) + query := `SELECT lineid as id, linenum as num FROM line WHERE sessionid = ? AND screenid = ? ORDER BY linenum` + tx.Select(&rtn, query, sessionId, screenId) return nil }) if txErr != nil { @@ -1693,7 +1689,7 @@ func UpdateScreensWithCmdFg(ctx context.Context, sessionId string, cmdId string) AND s.selectedline IN (SELECT linenum FROM line l WHERE l.sessionid = s.sessionid - AND l.windowid = s.windowid + AND l.screenid = s.screenid AND l.cmdid = ? )` screenIds := tx.SelectStrings(query, sessionId, cmdId) @@ -1840,12 +1836,12 @@ func UpdateLineHeight(ctx context.Context, lineId string, heightVal int) error { } // can return nil, nil if line is not found -func GetLineById(ctx context.Context, sessionId string, windowId string, lineId string) (*LineType, error) { +func GetLineById(ctx context.Context, sessionId string, screenId string, lineId string) (*LineType, error) { var rtn *LineType txErr := WithTx(ctx, func(tx *TxWrap) error { var line LineType - query := `SELECT * FROM line WHERE sessionid = ? AND windowid = ? AND lineid = ?` - found := tx.Get(&line, query, sessionId, windowId, lineId) + query := `SELECT * FROM line WHERE sessionid = ? AND screenid = ? AND lineid = ?` + found := tx.Get(&line, query, sessionId, screenId, lineId) if found { rtn = &line } @@ -1900,11 +1896,11 @@ func PurgeLinesByIds(ctx context.Context, sessionId string, lineIds []string) er return txErr } -func GetRIsForWindow(ctx context.Context, sessionId string, windowId string) ([]*RemoteInstance, error) { +func GetRIsForScreen(ctx context.Context, sessionId string, screenId string) ([]*RemoteInstance, error) { var rtn []*RemoteInstance txErr := WithTx(ctx, func(tx *TxWrap) error { - query := `SELECT * FROM remote_instance WHERE sessionid = ? AND (windowid = '' OR windowid = ?)` - rtn = SelectMapsGen[*RemoteInstance](tx, query, sessionId, windowId) + query := `SELECT * FROM remote_instance WHERE sessionid = ? AND (screenid = '' OR screenid = ?)` + rtn = SelectMapsGen[*RemoteInstance](tx, query, sessionId, screenId) return nil }) if txErr != nil { diff --git a/pkg/sstore/migrate.go b/pkg/sstore/migrate.go index c49f46bf..5e98e3e7 100644 --- a/pkg/sstore/migrate.go +++ b/pkg/sstore/migrate.go @@ -17,7 +17,7 @@ import ( "github.com/golang-migrate/migrate/v4" ) -const MaxMigration = 9 +const MaxMigration = 10 const MigratePrimaryScreenVersion = 9 func MakeMigrate() (*migrate.Migrate, error) { diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index fb5813c8..de02764c 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -34,9 +34,7 @@ const DBFileName = "prompt.db" const DBFileNameBackup = "backup.prompt.db" const DefaultSessionName = "default" -const DefaultWindowName = "default" const LocalRemoteAlias = "local" -const DefaultScreenWindowName = "w1" const DefaultCwd = "~" @@ -343,7 +341,6 @@ func (h *HistoryItemType) ToMap() map[string]interface{} { rtn["userid"] = h.UserId rtn["sessionid"] = h.SessionId rtn["screenid"] = h.ScreenId - rtn["windowid"] = h.WindowId rtn["lineid"] = h.LineId rtn["haderror"] = h.HadError rtn["cmdid"] = h.CmdId @@ -362,7 +359,6 @@ func (h *HistoryItemType) FromMap(m map[string]interface{}) bool { quickSetStr(&h.UserId, m, "userid") quickSetStr(&h.SessionId, m, "sessionid") quickSetStr(&h.ScreenId, m, "screenid") - quickSetStr(&h.WindowId, m, "windowid") quickSetStr(&h.LineId, m, "lineid") quickSetBool(&h.HadError, m, "haderror") quickSetStr(&h.CmdId, m, "cmdid") @@ -381,15 +377,9 @@ type ScreenOptsType struct { PTerm string `json:"pterm,omitempty"` } -type SWKeys struct { - SessionId string - WindowId string -} - type ScreenLinesType struct { SessionId string `json:"sessionid"` ScreenId string `json:"screenid"` - WindowId string `json:"windowid"` Lines []*LineType `json:"lines" dbmap:"-"` Cmds []*CmdType `json:"cmds" dbmap:"-"` } @@ -399,7 +389,6 @@ func (ScreenLinesType) UseDBMap() {} type ScreenType struct { SessionId string `json:"sessionid"` ScreenId string `json:"screenid"` - WindowId string `json:"windowid"` Name string `json:"name"` ScreenIdx int64 `json:"screenidx"` ScreenOpts ScreenOptsType `json:"screenopts"` @@ -422,7 +411,6 @@ func (s *ScreenType) ToMap() map[string]interface{} { rtn := make(map[string]interface{}) rtn["sessionid"] = s.SessionId rtn["screenid"] = s.ScreenId - rtn["windowid"] = s.WindowId rtn["name"] = s.Name rtn["screenidx"] = s.ScreenIdx rtn["screenopts"] = quickJson(s.ScreenOpts) @@ -443,7 +431,6 @@ func (s *ScreenType) ToMap() map[string]interface{} { func (s *ScreenType) FromMap(m map[string]interface{}) bool { quickSetStr(&s.SessionId, m, "sessionid") quickSetStr(&s.ScreenId, m, "screenid") - quickSetStr(&s.WindowId, m, "windowid") quickSetStr(&s.Name, m, "name") quickSetInt64(&s.ScreenIdx, m, "screenidx") quickSetJson(&s.ScreenOpts, m, "screenopts") @@ -497,7 +484,6 @@ type HistoryItemType struct { UserId string `json:"userid"` SessionId string `json:"sessionid"` ScreenId string `json:"screenid"` - WindowId string `json:"windowid"` LineId string `json:"lineid"` HadError bool `json:"haderror"` CmdId string `json:"cmdid"` @@ -520,7 +506,7 @@ type HistoryQueryOpts struct { SearchText string SessionId string RemoteId string - WindowId string + ScreenId string NoMeta bool RawOffset int FilterFn func(*HistoryItemType) bool @@ -568,7 +554,7 @@ type RemoteInstance struct { RIId string `json:"riid"` Name string `json:"name"` SessionId string `json:"sessionid"` - WindowId string `json:"windowid"` + ScreenId string `json:"screenid"` RemoteOwnerId string `json:"remoteownerid"` RemoteId string `json:"remoteid"` FeState FeStateType `json:"festate"` @@ -629,7 +615,7 @@ func (ri *RemoteInstance) FromMap(m map[string]interface{}) bool { quickSetStr(&ri.RIId, m, "riid") quickSetStr(&ri.Name, m, "name") quickSetStr(&ri.SessionId, m, "sessionid") - quickSetStr(&ri.WindowId, m, "windowid") + quickSetStr(&ri.ScreenId, m, "screenid") quickSetStr(&ri.RemoteOwnerId, m, "remoteownerid") quickSetStr(&ri.RemoteId, m, "remoteid") quickSetJson(&ri.FeState, m, "festate") @@ -643,7 +629,7 @@ func (ri *RemoteInstance) ToMap() map[string]interface{} { rtn["riid"] = ri.RIId rtn["name"] = ri.Name rtn["sessionid"] = ri.SessionId - rtn["windowid"] = ri.WindowId + rtn["screenid"] = ri.ScreenId rtn["remoteownerid"] = ri.RemoteOwnerId rtn["remoteid"] = ri.RemoteId rtn["festate"] = quickJson(ri.FeState) @@ -654,7 +640,7 @@ func (ri *RemoteInstance) ToMap() map[string]interface{} { type LineType struct { SessionId string `json:"sessionid"` - WindowId string `json:"windowid"` + ScreenId string `json:"screenid"` UserId string `json:"userid"` LineId string `json:"lineid"` Ts int64 `json:"ts"` @@ -948,10 +934,10 @@ func (cmd *CmdType) FromMap(m map[string]interface{}) bool { return true } -func makeNewLineCmd(sessionId string, windowId string, userId string, cmdId string, renderer string) *LineType { +func makeNewLineCmd(sessionId string, screenId string, userId string, cmdId string, renderer string) *LineType { rtn := &LineType{} rtn.SessionId = sessionId - rtn.WindowId = windowId + rtn.ScreenId = screenId rtn.UserId = userId rtn.LineId = scbase.GenPromptUUID() rtn.Ts = time.Now().UnixMilli() @@ -963,10 +949,10 @@ func makeNewLineCmd(sessionId string, windowId string, userId string, cmdId stri return rtn } -func makeNewLineText(sessionId string, windowId string, userId string, text string) *LineType { +func makeNewLineText(sessionId string, screenId string, userId string, text string) *LineType { rtn := &LineType{} rtn.SessionId = sessionId - rtn.WindowId = windowId + rtn.ScreenId = screenId rtn.UserId = userId rtn.LineId = scbase.GenPromptUUID() rtn.Ts = time.Now().UnixMilli() @@ -977,8 +963,8 @@ func makeNewLineText(sessionId string, windowId string, userId string, text stri return rtn } -func AddCommentLine(ctx context.Context, sessionId string, windowId string, userId string, commentText string) (*LineType, error) { - rtnLine := makeNewLineText(sessionId, windowId, userId, commentText) +func AddCommentLine(ctx context.Context, sessionId string, screenId string, userId string, commentText string) (*LineType, error) { + rtnLine := makeNewLineText(sessionId, screenId, userId, commentText) err := InsertLine(ctx, rtnLine, nil) if err != nil { return nil, err @@ -986,8 +972,8 @@ func AddCommentLine(ctx context.Context, sessionId string, windowId string, user return rtnLine, nil } -func AddCmdLine(ctx context.Context, sessionId string, windowId string, userId string, cmd *CmdType, renderer string) (*LineType, error) { - rtnLine := makeNewLineCmd(sessionId, windowId, userId, cmd.CmdId, renderer) +func AddCmdLine(ctx context.Context, sessionId string, screenId string, userId string, cmd *CmdType, renderer string) (*LineType, error) { + rtnLine := makeNewLineCmd(sessionId, screenId, userId, cmd.CmdId, renderer) err := InsertLine(ctx, rtnLine, cmd) if err != nil { return nil, err diff --git a/pkg/sstore/updatebus.go b/pkg/sstore/updatebus.go index 9855b82e..fa1a067e 100644 --- a/pkg/sstore/updatebus.go +++ b/pkg/sstore/updatebus.go @@ -111,7 +111,7 @@ type InfoMsgType struct { type HistoryInfoType struct { HistoryType string `json:"historytype"` SessionId string `json:"sessionid,omitempty"` - WindowId string `json:"windowid,omitempty"` + ScreenId string `json:"screenid,omitempty"` Items []*HistoryItemType `json:"items"` Show bool `json:"show"` }