refactor: consolidate packages - move output to cmd, config/session to store, rename Store to GraphStore

This commit is contained in:
2026-04-02 00:18:33 +02:00
parent 2bcc310c6d
commit 03a896d23f
25 changed files with 190 additions and 239 deletions

193
src/store/config.go Normal file
View File

@@ -0,0 +1,193 @@
package store
import (
"encoding/json"
"errors"
"os"
"os/user"
"path/filepath"
"slices"
)
type Alias struct {
Name string `json:"name"`
Command string `json:"command"`
Description string `json:"description,omitempty"`
}
type ServerConfig struct {
Host string `json:"host"`
Port int `json:"port"`
}
type OIDCConfig struct {
Issuer string `json:"issuer"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
PublicURL string `json:"public_url"`
UserClaim string `json:"user_claim"`
}
type Config struct {
path string
User string `json:"user"`
Editor string `json:"editor"`
UserAliases []*Alias `json:"aliases"`
Serve ServerConfig `json:"serve"`
Remote ServerConfig `json:"remote"`
OIDC OIDCConfig `json:"oidc"`
}
func FindDataRoot(std ...string) (string, error) {
dir, err := filepath.Abs(".")
if err != nil {
return "", err
}
for {
p := filepath.Join(dir, ".ax")
if stat, err := os.Stat(p); err == nil {
if stat.IsDir() {
return p, nil
}
}
if parent := filepath.Dir(dir); parent == dir {
break
} else {
dir = parent
}
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
stdpath := filepath.Join(std...)
return filepath.Join(home, stdpath, "ax"), nil
}
func LoadConfigFile() (*Config, error) {
configRoot, err := FindDataRoot(".config")
if err != nil {
return nil, err
}
path := filepath.Join(configRoot, "config.json")
fc := &Config{path: path, UserAliases: []*Alias{}}
data, err := os.ReadFile(path)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
} else {
if err := json.Unmarshal(data, fc); err != nil {
return nil, err
}
}
return fc, nil
}
func (c *Config) GetUser() string {
if c.User != "" {
return c.User
}
if u := os.Getenv("AX_USER"); u != "" {
return u
}
if u, err := user.Current(); err == nil {
return u.Username
}
return "unknown"
}
func (c *Config) GetEditor() string {
if c.Editor != "" {
return c.User
}
if u := os.Getenv("EDITOR"); u != "" {
return u
}
return "vi"
}
func (c *Config) GetAlias(name string) (*Alias, error) {
for _, a := range c.UserAliases {
if a.Name == name {
return a, nil
}
}
return nil, errors.New("alias not found")
}
func (c *Config) SetAlias(alias *Alias) error {
for i, a := range c.UserAliases {
if a.Name == alias.Name {
c.UserAliases[i] = alias
return c.Save()
}
}
c.UserAliases = append(c.UserAliases, alias)
return c.Save()
}
func (c *Config) DeleteAlias(name string) error {
for i, a := range c.UserAliases {
if a.Name == name {
c.UserAliases = slices.Delete(c.UserAliases, i, i+1)
return c.Save()
}
}
return errors.New("alias not found")
}
func (c *Config) ListAliases() ([]*Alias, error) {
seen := make(map[string]bool)
var result []*Alias
for _, a := range c.UserAliases {
result = append(result, a)
seen[a.Name] = true
}
return result, nil
}
func (c *Config) GetOIDCConfig() (*OIDCConfig, bool) {
if c.OIDC.Issuer == "" {
return nil, false
}
cfg := c.OIDC
if cfg.UserClaim == "" {
cfg.UserClaim = "preferred_username"
}
return &cfg, true
}
func (c *Config) GetRemoteConfig() (*ServerConfig, bool) {
if c.Remote.Host == "" {
return nil, false
}
port := c.Remote.Port
if port == 0 {
port = 7000
}
return &ServerConfig{Host: c.Remote.Host, Port: port}, true
}
func (c *Config) GetServerConfig() *ServerConfig {
host := c.Serve.Host
if host == "" {
host = "localhost"
}
port := c.Serve.Port
if port == 0 {
port = 7000
}
return &ServerConfig{Host: host, Port: port}
}
func (c *Config) Save() error {
if err := os.MkdirAll(filepath.Dir(c.path), 0755); err != nil {
return err
}
data, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
}
return os.WriteFile(c.path, data, 0644)
}

View File

@@ -2,10 +2,10 @@ package store
import "axolotl/models"
// Store is a primitive graph persistence interface. It provides basic
// GraphStore is a primitive graph persistence interface. It provides basic
// operations for nodes and directed rels. No business logic lives here.
// "Tag" rels are rels with an empty toID (e.g. "_type::issue" or "backend").
type Store interface {
type GraphStore interface {
// Nodes
AddNode(id, title, content, dueDate, createdAt, updatedAt string) error
GetNode(id string) (*models.Node, error) // returns node with tags and rels populated
@@ -26,5 +26,5 @@ type Store interface {
// Transaction runs fn inside an atomic transaction. If fn returns an error
// the transaction is rolled back; otherwise it is committed.
// Calls to Transaction inside fn reuse the same transaction (no nesting).
Transaction(fn func(Store) error) error
Transaction(fn func(GraphStore) error) error
}

View File

@@ -3,7 +3,6 @@ package store
import (
"axolotl/models"
"database/sql"
"errors"
"fmt"
"math/rand"
"os"
@@ -27,8 +26,8 @@ type querier interface {
QueryRow(query string, args ...any) *sql.Row
}
// SQLiteStore is the top-level Store backed by a SQLite database file.
type SQLiteStore struct {
// GraphStoreSqlite is the top-level Store backed by a SQLite database file.
type GraphStoreSqlite struct {
db *sql.DB
}
@@ -62,31 +61,28 @@ func InitSQLiteStore(path string) error {
// FindAndOpenSQLiteStore opens the SQLite database. If the AX_DB_PATH environment
// variable is set, it uses that path directly. Otherwise, it walks up from the
// current working directory to find an .ax.db file.
func FindAndOpenSQLiteStore() (Store, error) {
func FindAndOpenSQLiteStore() (GraphStore, error) {
if dbpath := os.Getenv("AX_DB_PATH"); dbpath != "" {
return NewSQLiteStore(dbpath)
}
dir, err := filepath.Abs(".")
dataRoot, err := FindDataRoot(".local", "share")
if err != nil {
return nil, err
fmt.Fprintln(os.Stderr, "failed to find data dir:", err)
os.Exit(1)
}
for {
dbpath := filepath.Join(dir, ".ax.db")
if _, err := os.Stat(dbpath); err == nil {
return NewSQLiteStore(dbpath)
}
if parent := filepath.Dir(dir); parent == dir {
return nil, errors.New("no .ax.db found (run 'ax init' first)")
} else {
dir = parent
}
dbPath := filepath.Join(dataRoot, "ax.db")
if _, err := os.Stat(dbPath); err == nil {
fmt.Fprintln(os.Stderr, "database already exists:", dbPath)
os.Exit(1)
}
return NewSQLiteStore(dbPath)
}
// FindOrInitSQLiteStore is like FindAndOpenSQLiteStore but intended for server
// mode: if no .ax.db is found it creates and initialises one in the current
// working directory instead of returning an error.
func FindOrInitSQLiteStore() (Store, error) {
func FindOrInitSQLiteStore() (GraphStore, error) {
if dbpath := os.Getenv("AX_DB_PATH"); dbpath != "" {
if err := InitSQLiteStore(dbpath); err != nil {
return nil, err
@@ -119,7 +115,7 @@ func FindOrInitSQLiteStore() (Store, error) {
// NewSQLiteStore opens a SQLite database at the given path, runs a one-time
// schema migration if needed, then applies per-connection PRAGMAs.
func NewSQLiteStore(path string) (Store, error) {
func NewSQLiteStore(path string) (GraphStore, error) {
db, err := sql.Open("sqlite", path)
if err != nil {
return nil, fmt.Errorf("failed to open database: %w", err)
@@ -139,7 +135,7 @@ func NewSQLiteStore(path string) (Store, error) {
db.Close()
return nil, err
}
return &SQLiteStore{db: db}, nil
return &GraphStoreSqlite{db: db}, nil
}
// migrateSchema migrates from the legacy two-table (tags + rels) schema to the
@@ -171,7 +167,7 @@ func migrateSchema(db *sql.DB) error {
// --- Transaction ---
func (s *SQLiteStore) Transaction(fn func(Store) error) error {
func (s *GraphStoreSqlite) Transaction(fn func(GraphStore) error) error {
tx, err := s.db.Begin()
if err != nil {
return err
@@ -183,14 +179,14 @@ func (s *SQLiteStore) Transaction(fn func(Store) error) error {
return tx.Commit()
}
func (s *txStore) Transaction(fn func(Store) error) error {
func (s *txStore) Transaction(fn func(GraphStore) error) error {
return fn(s) // already in a transaction — reuse it
}
// --- Node operations ---
func addNode(q querier, id, title, content, dueDate, createdAt, updatedAt string) error {
var dd interface{}
var dd any
if dueDate != "" {
dd = dueDate
}
@@ -227,7 +223,7 @@ func getNode(q querier, id string) (*models.Node, error) {
}
func updateNode(q querier, id, title, content, dueDate, updatedAt string) error {
var dd interface{}
var dd any
if dueDate != "" {
dd = dueDate
}
@@ -249,15 +245,15 @@ func nodeExists(q querier, id string) (bool, error) {
return e, err
}
func (s *SQLiteStore) AddNode(id, title, content, dueDate, createdAt, updatedAt string) error {
func (s *GraphStoreSqlite) AddNode(id, title, content, dueDate, createdAt, updatedAt string) error {
return addNode(s.db, id, title, content, dueDate, createdAt, updatedAt)
}
func (s *SQLiteStore) GetNode(id string) (*models.Node, error) { return getNode(s.db, id) }
func (s *SQLiteStore) UpdateNode(id, title, content, dueDate, updatedAt string) error {
func (s *GraphStoreSqlite) GetNode(id string) (*models.Node, error) { return getNode(s.db, id) }
func (s *GraphStoreSqlite) UpdateNode(id, title, content, dueDate, updatedAt string) error {
return updateNode(s.db, id, title, content, dueDate, updatedAt)
}
func (s *SQLiteStore) DeleteNode(id string) error { return deleteNode(s.db, id) }
func (s *SQLiteStore) NodeExists(id string) (bool, error) { return nodeExists(s.db, id) }
func (s *GraphStoreSqlite) DeleteNode(id string) error { return deleteNode(s.db, id) }
func (s *GraphStoreSqlite) NodeExists(id string) (bool, error) { return nodeExists(s.db, id) }
func (s *txStore) AddNode(id, title, content, dueDate, createdAt, updatedAt string) error {
return addNode(s.tx, id, title, content, dueDate, createdAt, updatedAt)
@@ -292,8 +288,8 @@ func generateID(q querier) (string, error) {
}
}
func (s *SQLiteStore) GenerateID() (string, error) { return generateID(s.db) }
func (s *txStore) GenerateID() (string, error) { return generateID(s.db) }
func (s *GraphStoreSqlite) GenerateID() (string, error) { return generateID(s.db) }
func (s *txStore) GenerateID() (string, error) { return generateID(s.db) }
// --- Rel operations ---
@@ -307,10 +303,10 @@ func removeRel(q querier, nodeID, relName, toID string) error {
return err
}
func (s *SQLiteStore) AddRel(nodeID, relName, toID string) error {
func (s *GraphStoreSqlite) AddRel(nodeID, relName, toID string) error {
return addRel(s.db, nodeID, relName, toID)
}
func (s *SQLiteStore) RemoveRel(nodeID, relName, toID string) error {
func (s *GraphStoreSqlite) RemoveRel(nodeID, relName, toID string) error {
return removeRel(s.db, nodeID, relName, toID)
}
func (s *txStore) AddRel(nodeID, relName, toID string) error {
@@ -377,7 +373,7 @@ func findNodes(q querier, filters []*models.Rel) ([]*models.Node, error) {
return nodes, nil
}
func (s *SQLiteStore) FindNodes(filters []*models.Rel) ([]*models.Node, error) {
func (s *GraphStoreSqlite) FindNodes(filters []*models.Rel) ([]*models.Node, error) {
return findNodes(s.db, filters)
}

54
src/store/session.go Normal file
View File

@@ -0,0 +1,54 @@
package store
import (
"encoding/json"
"os"
"path/filepath"
)
// Session holds the server-issued token returned by POST /auth/poll.
// The ax server owns the full OIDC flow; the client only needs this token.
type Session struct {
path string
Token string `json:"token"`
}
func LoadSession() (*Session, error) {
sessionRoot, err := FindDataRoot(".local", "share")
if err != nil {
return nil, err
}
path := filepath.Join(sessionRoot, "session.json")
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
var s Session
if err := json.Unmarshal(data, &s); err != nil {
return nil, err
}
s.path = path
return &s, nil
}
func (s *Session) Save() error {
if err := os.MkdirAll(filepath.Dir(s.path), 0700); err != nil {
return err
}
data, err := json.MarshalIndent(s, "", " ")
if err != nil {
return err
}
return os.WriteFile(s.path, data, 0600)
}
func (s *Session) ClearSession() error {
err := os.Remove(s.path)
if os.IsNotExist(err) {
return nil
}
return err
}