Unverified Commit fcc3fb91 authored by Kevin Lyda's avatar Kevin Lyda
Browse files

Initial folders module

parent f029cbfb
Loading
Loading
Loading
Loading
+5 −10
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import (
	"path"
	"strings"

	"git.lyda.ie/kevin/bulletin/folders"
	"github.com/adrg/xdg"
	_ "modernc.org/sqlite" // Loads sqlite driver.
)
@@ -19,7 +20,7 @@ type UserData struct {
	Account        string
	FullName       string
	pref           *sql.DB
	bull           *sql.DB
	Folders        *folders.Store
	CurrentFolder  string
	CurrentMessage int
}
@@ -59,16 +60,10 @@ func Open(acc string) error {
	}
	// TODO: run prefs migration - move this to a storage module.

	bulldir := path.Join(xdg.DataHome, "BULLETIN")
	err = os.MkdirAll(bulldir, 0700)
	User.Folders, err = folders.Open()
	if err != nil {
		return errors.New("bulletin directory problem")
	}
	User.bull, err = sql.Open("sqlite", path.Join(bulldir, "bboard.db"))
	if err != nil {
		return errors.New("bulletin database problem")
		return err
	}
	// TODO: run prefs migration - move this to a storage module.

	return nil
}
@@ -76,7 +71,7 @@ func Open(acc string) error {
// Close closes the resources open for the account.
func (u *UserData) Close() {
	u.pref.Close()
	u.bull.Close()
	u.Folders.Close()
}

// IsAdmin returns true if the user is an admin

folders/folders.go

0 → 100644
+63 −0
Original line number Diff line number Diff line
// Package folders are all the routines and sql for managing folders.
package folders

import (
	"database/sql"
	"embed"
	"errors"
	"log"
	"os"
	"path"

	"github.com/adrg/xdg"
	"github.com/golang-migrate/migrate/v4"
	"github.com/golang-migrate/migrate/v4/source/iofs"

	// Included to connect to sqlite.
	_ "github.com/golang-migrate/migrate/v4/database/sqlite"
	_ "modernc.org/sqlite"
)

//go:embed sql/*.sql
var fs embed.FS

// Store is the store for folders.
type Store struct {
	db *sql.DB
}

// Open opens the folders database.
func Open() (*Store, error) {
	fdir := path.Join(xdg.DataHome, "BULLETIN")
	err := os.MkdirAll(fdir, 0700)
	if err != nil {
		return nil, errors.New("bulletin directory problem")
	}
	fdb := path.Join(fdir, "bboard.db")

	sqldir, err := iofs.New(fs, "sql")
	if err != nil {
		return nil, err
	}
	m, err := migrate.NewWithSourceInstance("iofs", sqldir, "sqlite://"+fdb)
	if err != nil {
		log.Fatal(err)
	}
	err = m.Up()
	if err != nil {
		return nil, err
	}
	m.Close()

	store := &Store{}
	store.db, err = sql.Open("sqlite", fdb)
	if err != nil {
		return nil, errors.New("bulletin database problem")
	}
	return store, nil
}

// Close closes the db backing the store.
func (fstore *Store) Close() {
	fstore.db.Close()
}
+1 −0
Original line number Diff line number Diff line
DROP TABLE folders;
+18 −0
Original line number Diff line number Diff line
CREATE TABLE folders (
  name        VARCHAR(25)  NOT NULL UNIQUE,
  always      INT          DEFAULT 0,
  brief       INT          DEFAULT 0,
  description VARCHAR(53),
  co_owners   TEXT,
  notify      INT          DEFAULT 0,
  owner       TEXT,
  readnew     INT          DEFAULT 0,
  shownew     INT          DEFAULT 0,
  system      INT          DEFAULT 0,
  expire      INT          DEFAULT 14,
  visibility  TEXT         DEFAULT "public"
);

INSERT INTO folders (name, description, system, shownew, owner)
       VALUES ("GENERAL", "Default general bulletin folder.", 1, 1, "SYSTEM");
+5 −0
Original line number Diff line number Diff line
@@ -5,17 +5,22 @@ go 1.24.2
require (
	github.com/adrg/xdg v0.5.3
	github.com/chzyer/readline v1.5.1
	github.com/golang-migrate/migrate/v4 v4.18.3
	github.com/urfave/cli/v3 v3.3.2
	modernc.org/sqlite v1.37.0
)

require (
	github.com/dustin/go-humanize v1.0.1 // indirect
	github.com/golang-migrate/migrate v3.5.4+incompatible // indirect
	github.com/google/uuid v1.6.0 // indirect
	github.com/hashicorp/errwrap v1.1.0 // indirect
	github.com/hashicorp/go-multierror v1.1.1 // indirect
	github.com/mattn/go-isatty v0.0.20 // indirect
	github.com/mitchellh/go-wordwrap v1.0.1 // indirect
	github.com/ncruces/go-strftime v0.1.9 // indirect
	github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
	go.uber.org/atomic v1.11.0 // indirect
	golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 // indirect
	golang.org/x/sys v0.33.0 // indirect
	modernc.org/libc v1.65.0 // indirect
Loading