checkpoint, ensuredefaultsession

This commit is contained in:
sawka 2022-07-01 14:45:33 -07:00
parent b85be3457c
commit 60199713e8
5 changed files with 55 additions and 37 deletions

View File

@ -496,20 +496,17 @@ func main() {
fmt.Printf("[error] %v\n", err)
return
}
numSessions, err := sstore.NumSessions(context.Background())
if err != nil {
fmt.Printf("[error] getting num sessions: %v\n", err)
return
}
err = sstore.EnsureLocalRemote(context.Background())
if err != nil {
fmt.Printf("[error] ensuring local remote: %v\n", err)
return
}
fmt.Printf("[db] sessions count=%d\n", numSessions)
if numSessions == 0 {
sstore.CreateInitialSession(context.Background())
defaultSession, err := sstore.EnsureDefaultSession(context.Background())
if err != nil {
fmt.Printf("[error] ensuring default session: %v\n", err)
return
}
fmt.Printf("session: %#v\n", defaultSession)
return
runnerProc, err := remote.LaunchMShell()

View File

@ -94,17 +94,23 @@ func GetSessionById(ctx context.Context, id string) (*SessionType, error) {
}
func GetSessionByName(ctx context.Context, name string) (*SessionType, error) {
db, err := GetDB()
query := `SELECT * FROM session WHERE name = ?`
var rtnSession *SessionType
err := WithTx(ctx, func(tx *TxWrap) error {
var session SessionType
err = db.GetContext(ctx, &session, query, name)
if err == sql.ErrNoRows {
return nil, nil
query := `SELECT * FROM session WHERE name = ?`
found := tx.GetWrap(&session, query, name)
if !found {
return nil
}
rtnSession = &session
query = `SELECT sessionid, windowid, name, curremote, version FROM window WHERE sessionid = ?`
tx.SelectWrap(&session.Windows, query, session.SessionId)
return nil
})
if err != nil {
return nil, err
}
return &session, nil
return rtnSession, nil
}
// also creates window, and sessionremote

View File

@ -51,7 +51,6 @@ func GetDB() (*sqlx.DB, error) {
type SessionType struct {
SessionId string `json:"sessionid"`
Remote string `json:"remote"`
Name string `json:"name"`
Windows []*WindowType `json:"windows"`
Cmds []*CmdType `json:"cmds"`
@ -62,7 +61,7 @@ type WindowType struct {
WindowId string `json:"windowid"`
Name string `json:"name"`
CurRemote string `json:"curremote"`
Remotes []*SessionRemote `json:"remotes"`
Remotes []*RemoteType `json:"remotes"`
Lines []*LineType `json:"lines"`
Version int `json:"version"`
}
@ -178,19 +177,19 @@ func EnsureLocalRemote(ctx context.Context) error {
return nil
}
func EnsureDefaultSession(ctx context.Context) error {
func EnsureDefaultSession(ctx context.Context) (*SessionType, error) {
session, err := GetSessionByName(ctx, DefaultSessionName)
if err != nil {
return err
return nil, err
}
if session != nil {
return nil
return session, nil
}
err = InsertSessionWithName(ctx, DefaultSessionName)
if err != nil {
return err
return nil, err
}
return nil
return GetSessionByName(ctx, DefaultSessionName)
}
func CreateInitialSession(ctx context.Context) error {

View File

@ -65,24 +65,28 @@ func (tx *TxWrap) ExecWrap(query string, args ...interface{}) sql.Result {
return result
}
func (tx *TxWrap) GetWrap(dest interface{}, query string, args ...interface{}) error {
func (tx *TxWrap) GetWrap(dest interface{}, query string, args ...interface{}) bool {
if tx.Err != nil {
return nil
return false
}
err := tx.Txx.Get(dest, query, args...)
if err != nil && err != sql.ErrNoRows {
tx.Err = err
if err != nil && err == sql.ErrNoRows {
return false
}
return err
if err != nil {
tx.Err = err
return false
}
return true
}
func (tx *TxWrap) SelectWrap(dest interface{}, query string, args ...interface{}) error {
func (tx *TxWrap) SelectWrap(dest interface{}, query string, args ...interface{}) {
if tx.Err != nil {
return nil
return
}
err := tx.Txx.Select(dest, query, args...)
if err != nil {
tx.Err = err
}
return err
return
}

12
scripthaus.md Normal file
View File

@ -0,0 +1,12 @@
# SH2 Server Commands
```bash
# @scripthaus command dump-schema
sqlite3 /Users/mike/scripthaus/sh2.db .schema > db/schema.sql
```
```bash
# @scripthaus command opendb
sqlite3 /Users/mike/scripthaus/sh2.db
```