diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index e8925ee9..f459e35b 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -89,6 +89,19 @@ func resolveSessionScreen(ctx context.Context, sessionId string, screenArg strin return "", fmt.Errorf("could not resolve screen '%s' (name/id not found)", screenArg) } +func resolveSession(ctx context.Context, sessionArg string) (string, error) { + sessions, err := sstore.GetBareSessions(ctx) + if err != nil { + return "", fmt.Errorf("could not retrive bare sessions") + } + for _, session := range sessions { + if session.SessionId == sessionArg || session.Name == sessionArg { + return session.SessionId, nil + } + } + return "", fmt.Errorf("could not resolve sesssion '%s' (name/id not found)", sessionArg) +} + func resolveSessionId(pk *scpacket.FeCommandPacketType) (string, error) { sessionId := pk.Kwargs["session"] if sessionId == "" { @@ -373,5 +386,24 @@ func CommentCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ssto } func SessionCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { - return nil, nil + if pk.MetaSubCmd == "open" || pk.MetaSubCmd == "new" { + activate := resolveBool(pk.Kwargs["activate"], true) + update, err := sstore.InsertSessionWithName(ctx, pk.Kwargs["name"], activate) + if err != nil { + return nil, err + } + return update, nil + } + if pk.MetaSubCmd != "" { + return nil, fmt.Errorf("invalid /session subcommand '%s'", pk.MetaSubCmd) + } + firstArg := firstArg(pk) + if firstArg == "" { + return nil, fmt.Errorf("usage /session [session-name|session-id], no param specified") + } + sessionId, err := resolveSession(ctx, firstArg) + if err != nil { + return nil, err + } + return sstore.SessionUpdate{ActiveSessionId: sessionId}, nil } diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index a00e93ba..6bb87f83 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -85,11 +85,27 @@ func InsertRemote(ctx context.Context, remote *RemoteType) error { return nil } +func GetBareSessions(ctx context.Context) ([]*SessionType, error) { + var rtn []*SessionType + err := WithTx(ctx, func(tx *TxWrap) error { + query := `SELECT * FROM session` + tx.SelectWrap(&rtn, query) + return nil + }) + if err != nil { + return nil, err + } + return rtn, nil +} + func GetAllSessions(ctx context.Context) ([]*SessionType, error) { var rtn []*SessionType err := WithTx(ctx, func(tx *TxWrap) error { query := `SELECT * FROM session` tx.SelectWrap(&rtn, query) + for _, session := range rtn { + session.Full = true + } var screens []*ScreenType query = `SELECT * FROM screen ORDER BY screenidx` tx.SelectWrap(&screens, query) @@ -154,26 +170,16 @@ func GetSessionScreens(ctx context.Context, sessionId string) ([]*ScreenType, er } func GetSessionById(ctx context.Context, id string) (*SessionType, error) { - var rtnSession *SessionType - err := WithTx(ctx, func(tx *TxWrap) error { - var session SessionType - query := `SELECT * FROM session WHERE sessionid = ?` - found := tx.GetWrap(&session, query, id) - if !found { - return nil - } - rtnSession = &session - query = `SELECT * FROM screen WHERE sessionid = ? ORDER BY screenidx` - tx.SelectWrap(&session.Screens, query, session.SessionId) - query = `SELECT * FROM remote_instance WHERE sessionid = ?` - tx.SelectWrap(&session.Remotes, query, session.SessionId) - session.Full = true - return nil - }) + allSessions, err := GetAllSessions(ctx) if err != nil { return nil, err } - return rtnSession, nil + for _, session := range allSessions { + if session.SessionId == id { + return session, nil + } + } + return nil, nil } func GetSessionByName(ctx context.Context, name string) (*SessionType, error) { @@ -195,7 +201,7 @@ func GetSessionByName(ctx context.Context, name string) (*SessionType, error) { // also creates default window, returns sessionId // if sessionName == "", it will be generated -func InsertSessionWithName(ctx context.Context, sessionName string) (string, error) { +func InsertSessionWithName(ctx context.Context, sessionName string, activate bool) (*SessionUpdate, error) { newSessionId := uuid.New().String() txErr := WithTx(ctx, func(tx *TxWrap) error { names := tx.SelectStrings(`SELECT name FROM session`) @@ -210,9 +216,19 @@ func InsertSessionWithName(ctx context.Context, sessionName string) (string, err return nil }) if txErr != nil { - return "", txErr + return nil, txErr } - return newSessionId, nil + session, err := GetSessionById(ctx, newSessionId) + if err != nil { + return nil, err + } + update := &SessionUpdate{ + Sessions: []*SessionType{session}, + } + if activate { + update.ActiveSessionId = newSessionId + } + return update, nil } func containsStr(strs []string, testStr string) bool { diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index d3514864..b8997fdb 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -178,6 +178,7 @@ type TermOpts struct { Rows int64 `json:"rows"` Cols int64 `json:"cols"` FlexRows bool `json:"flexrows,omitempty"` + CmdSize int64 `json:"cmdsize,omitempty"` } func (opts *TermOpts) Scan(val interface{}) error { @@ -393,7 +394,7 @@ func EnsureDefaultSession(ctx context.Context) (*SessionType, error) { if session != nil { return session, nil } - _, err = InsertSessionWithName(ctx, DefaultSessionName) + _, err = InsertSessionWithName(ctx, DefaultSessionName, true) if err != nil { return nil, err } diff --git a/pkg/sstore/updatebus.go b/pkg/sstore/updatebus.go index f36b7626..70909e4c 100644 --- a/pkg/sstore/updatebus.go +++ b/pkg/sstore/updatebus.go @@ -41,7 +41,8 @@ func (WindowUpdate) WindowUpdate() string { } type SessionUpdate struct { - Sessions []*SessionType `json:"sessions"` + Sessions []*SessionType `json:"sessions"` + ActiveSessionId string `json:"activesessionid,omitempty"` } func (SessionUpdate) UpdateType() string {