diff --git a/ask/ask.go b/ask/ask.go index e58a68c9a7fb7becff422273f7b19e3481ce66f3..6581365d12a184d5d7b06159b12f46a0b373da8c 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 0cb34b641797008e25d82f7f898334ef1b38208f..1a9c266a4ef3fee4fd53cdc5404c0748e856b867 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 ca717ed23a539d737ecfbed729874b7b355d331a..cff89bb0f1d82c98ae4510513db2397f47a73b04 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 edf7270080cc965d00f6d403f128dc5c322cfabf..69f0f8f40daa617c9ac334bc6348fb51d197d827 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 97be72de0e14ef2ba3b5db14925c708e2e3f262e..20f44b22b58685f3a26157b2b382cc75852e83a2 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 {