From 99381ba829058397e3d5968181fc42d13d2b8881 Mon Sep 17 00:00:00 2001 From: Kevin Lyda <kevin@lyda.ie> Date: Tue, 20 May 2025 12:03:59 +0100 Subject: [PATCH] Set the system name. --- ask/ask.go | 9 +++++++++ batch/batch.go | 14 ++++++++++++++ storage/queries/standard.sql | 9 ++++++--- storage/standard.sql.go | 33 +++++++++++++++++++++------------ this/this.go | 9 +++++++++ 5 files changed, 59 insertions(+), 15 deletions(-) diff --git a/ask/ask.go b/ask/ask.go index e58a68c..6581365 100644 --- a/ask/ask.go +++ b/ask/ask.go @@ -32,6 +32,15 @@ func GetLine(prompt string) (string, error) { return line, err } +// GetInt64 gets a line. +func GetInt64(prompt string) (int64, error) { + line, err := GetLine(prompt) + if err != nil { + return 0, err + } + return strconv.ParseInt(line, 10, 64) +} + // Choose presents a list and asks a user to choose one. func Choose(prompt string, choices []string) (int, error) { fmt.Println(prompt) diff --git a/batch/batch.go b/batch/batch.go index 0cb34b6..1a9c266 100644 --- a/batch/batch.go +++ b/batch/batch.go @@ -81,6 +81,20 @@ the following files: To complete the installation, please enter the login, name and ssh key for the first user.`) + system := storage.System{} + system.Name, err = ask.GetLine("Enter system name: ") + ask.CheckErr(err) + system.DefaultExpire, err = ask.GetInt64("Enter default expiry in days: ") + ask.CheckErr(err) + system.ExpireLimit, err = ask.GetInt64("Enter expiration limit in days: ") + ask.CheckErr(err) + err = q.SetSystem(ctx, storage.SetSystemParams{ + Name: system.Name, + DefaultExpire: system.DefaultExpire, + ExpireLimit: system.ExpireLimit, + }) + ask.CheckErr(err) + login, err := ask.GetLine("Enter login of initial user: ") login = strings.ToUpper(login) ask.CheckErr(err) diff --git a/storage/queries/standard.sql b/storage/queries/standard.sql index ca717ed..cff89bb 100644 --- a/storage/queries/standard.sql +++ b/storage/queries/standard.sql @@ -91,8 +91,11 @@ SELECT * FROM folder_configs WHERE login = ? AND folder = ?; -- name: DeleteFolderConfig :exec DELETE FROM folder_configs WHERE login = ? AND folder = ?; --- name: AddSystem :exec -INSERT INTO system (name) VALUES (?); +-- name: SetSystem :exec +INSERT INTO system (rowid, name, default_expire, expire_limit) + VALUES (1, ?1, ?2, ?3) + ON CONFLICT(rowid) DO UPDATE + SET name = ?1, default_expire = ?2, expire_limit = ?3; -- name: GetSystem :one -SELECT * FROM system WHERE name = ?; +SELECT * FROM system LIMIT 1; diff --git a/storage/standard.sql.go b/storage/standard.sql.go index edf7270..69f0f8f 100644 --- a/storage/standard.sql.go +++ b/storage/standard.sql.go @@ -104,15 +104,6 @@ func (q *Queries) AddSeen(ctx context.Context, arg AddSeenParams) error { return err } -const addSystem = `-- name: AddSystem :exec -INSERT INTO system (name) VALUES (?) -` - -func (q *Queries) AddSystem(ctx context.Context, name string) error { - _, err := q.db.ExecContext(ctx, addSystem, name) - return err -} - const deleteFolder = `-- name: DeleteFolder :exec DELETE FROM folders WHERE name = ? ` @@ -398,11 +389,11 @@ func (q *Queries) GetSeen(ctx context.Context, arg GetSeenParams) (Seen, error) } const getSystem = `-- name: GetSystem :one -SELECT name, default_expire, expire_limit FROM system WHERE name = ? +SELECT name, default_expire, expire_limit FROM system LIMIT 1 ` -func (q *Queries) GetSystem(ctx context.Context, name string) (System, error) { - row := q.db.QueryRowContext(ctx, getSystem, name) +func (q *Queries) GetSystem(ctx context.Context) (System, error) { + row := q.db.QueryRowContext(ctx, getSystem) var i System err := row.Scan(&i.Name, &i.DefaultExpire, &i.ExpireLimit) return i, err @@ -677,3 +668,21 @@ func (q *Queries) ListUsers(ctx context.Context) ([]User, error) { } return items, nil } + +const setSystem = `-- name: SetSystem :exec +INSERT INTO system (rowid, name, default_expire, expire_limit) + VALUES (1, ?1, ?2, ?3) + ON CONFLICT(rowid) DO UPDATE + SET name = ?1, default_expire = ?2, expire_limit = ?3 +` + +type SetSystemParams struct { + Name string + DefaultExpire int64 + ExpireLimit int64 +} + +func (q *Queries) SetSystem(ctx context.Context, arg SetSystemParams) error { + _, err := q.db.ExecContext(ctx, setSystem, arg.Name, arg.DefaultExpire, arg.ExpireLimit) + return err +} diff --git a/this/this.go b/this/this.go index 97be72d..20f44b2 100644 --- a/this/this.go +++ b/this/this.go @@ -39,6 +39,9 @@ var MsgID int64 // used in a folder since changing into it. var ReadFirstCall bool +// System has the information about the system. +var System storage.System + // StartThis starts a session. func StartThis(login string) error { // Validate the login name. @@ -56,6 +59,12 @@ func StartThis(login string) error { Q = storage.New(Store.DB) ctx := storage.Context() + System, err = Q.GetSystem(ctx) + if err != nil { + return err + } + fmt.Printf("Connected to the %s BULLETIN system.\n\n", System.Name) + User, err = Q.GetUser(ctx, login) if User.Login != login { -- GitLab