From cb83223ab80b91f8282e630fd13d7dac0c3a63b5 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 21 Feb 2023 03:56:19 +0000 Subject: [PATCH] working --- cmd/signup/main.go | 15 ++++++++------- signup/signup.go | 12 ++++++++++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/cmd/signup/main.go b/cmd/signup/main.go index c57f966..f39daf2 100644 --- a/cmd/signup/main.go +++ b/cmd/signup/main.go @@ -20,7 +20,7 @@ import ( const ( maxInputLength = 10000 - logDir = "/town/var/signup" + logDir = "/town/var/signups/log" ) type scene struct { @@ -260,8 +260,7 @@ func _main(l *log.Logger, db *signup.DB) error { sceneIx := 0 currentScene := scenes[sceneIx] - inputBuff := []byte{} - inputWriter := bytes.NewBuffer(inputBuff) + inputWriter := bytes.NewBuffer([]byte{}) advanceScene := func(fromScene, sorryMsg string) { if currentScene.Name != fromScene { @@ -271,9 +270,10 @@ func _main(l *log.Logger, db *signup.DB) error { fmt.Fprintln(msgScroll, currentScene.Host.Say(sorryMsg)) return } - currentScene.Write(inputBuff) - inputBuff = []byte{} - inputWriter = bytes.NewBuffer(inputBuff) + l.Println("advancing scene") + + currentScene.Write(inputWriter.Bytes()) + inputWriter = bytes.NewBuffer([]byte{}) sceneIx++ currentScene = scenes[sceneIx] fmt.Fprintln(msgScroll, heredoc.Doc(` @@ -325,9 +325,10 @@ func _main(l *log.Logger, db *signup.DB) error { defer func() { if currentScene.Name == "done" { - currentScene.Write(inputBuff) + currentScene.Write(inputWriter.Bytes()) db.UpdateSignup(su) } + db.Close() }() app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { diff --git a/signup/signup.go b/signup/signup.go index 9b0c1f8..69bc7e1 100644 --- a/signup/signup.go +++ b/signup/signup.go @@ -2,8 +2,9 @@ package signup import ( "database/sql" - _ "github.com/mattn/go-sqlite3" "time" + + _ "github.com/mattn/go-sqlite3" ) type AdminNote struct { @@ -51,7 +52,7 @@ type DB struct { } func NewDB() (*DB, error) { - db, err := sql.Open("sqlite3", "/town/var/signups.db") + db, err := sql.Open("sqlite3", "/town/var/signups/signups.db?mode=rw") if err != nil { return nil, err } @@ -76,6 +77,8 @@ func (d *DB) InsertSignup(su *TownSignup) error { return err } + defer stmt.Close() + liid, err := result.LastInsertId() if err != nil { return err @@ -103,6 +106,11 @@ func (d *DB) UpdateSignup(su *TownSignup) error { if err != nil { return err } + defer stmt.Close() return nil } + +func (d *DB) Close() error { + return d.db.Close() +}