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) fmt.Printf("[error] %v\n", err)
return 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()) err = sstore.EnsureLocalRemote(context.Background())
if err != nil { if err != nil {
fmt.Printf("[error] ensuring local remote: %v\n", err) fmt.Printf("[error] ensuring local remote: %v\n", err)
return return
} }
fmt.Printf("[db] sessions count=%d\n", numSessions) defaultSession, err := sstore.EnsureDefaultSession(context.Background())
if numSessions == 0 { if err != nil {
sstore.CreateInitialSession(context.Background()) fmt.Printf("[error] ensuring default session: %v\n", err)
return
} }
fmt.Printf("session: %#v\n", defaultSession)
return return
runnerProc, err := remote.LaunchMShell() 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) { func GetSessionByName(ctx context.Context, name string) (*SessionType, error) {
db, err := GetDB() var rtnSession *SessionType
query := `SELECT * FROM session WHERE name = ?` err := WithTx(ctx, func(tx *TxWrap) error {
var session SessionType var session SessionType
err = db.GetContext(ctx, &session, query, name) query := `SELECT * FROM session WHERE name = ?`
if err == sql.ErrNoRows { found := tx.GetWrap(&session, query, name)
return nil, nil 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 { if err != nil {
return nil, err return nil, err
} }
return &session, nil return rtnSession, nil
} }
// also creates window, and sessionremote // also creates window, and sessionremote

View File

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

View File

@ -65,24 +65,28 @@ func (tx *TxWrap) ExecWrap(query string, args ...interface{}) sql.Result {
return 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 { if tx.Err != nil {
return nil return false
} }
err := tx.Txx.Get(dest, query, args...) err := tx.Txx.Get(dest, query, args...)
if err != nil && err != sql.ErrNoRows { if err != nil && err == sql.ErrNoRows {
tx.Err = err 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 { if tx.Err != nil {
return nil return
} }
err := tx.Txx.Select(dest, query, args...) err := tx.Txx.Select(dest, query, args...)
if err != nil { if err != nil {
tx.Err = err 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
```