45 lines
1.7 KiB
Go
45 lines
1.7 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
// openCodeEndpointSetting is the single server_settings key that remembers the
|
|
// operator's chosen OpenCode endpoint across restarts.
|
|
const openCodeEndpointSetting = "opencode_endpoint"
|
|
|
|
// initServerSettings creates the additive single-row settings table that records
|
|
// operator choices that must survive a restart (currently the OpenCode
|
|
// endpoint). It is its own table rather than a column elsewhere so no existing
|
|
// schema or ownership table is touched.
|
|
func (s *Store) initServerSettings() error {
|
|
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS server_settings (
|
|
key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
CREATE TABLE IF NOT EXISTS storage_workspaces(path TEXT PRIMARY KEY);`)
|
|
return err
|
|
}
|
|
|
|
// RecordOpenCodeEndpoint records the operator's chosen OpenCode endpoint in one
|
|
// bounded upsert, so a restart with no flag or environment keeps brokering from
|
|
// the same place. It is only called after OpenEndpoint validated the endpoint.
|
|
func (s *Store) RecordOpenCodeEndpoint(ctx context.Context, url string) error {
|
|
_, err := s.db.ExecContext(ctx, `INSERT INTO server_settings(key,value) VALUES(?,?)
|
|
ON CONFLICT(key) DO UPDATE SET value=excluded.value`, openCodeEndpointSetting, url)
|
|
return err
|
|
}
|
|
|
|
// OpenCodeEndpoint returns the recorded OpenCode endpoint, or an empty string
|
|
// when none was ever recorded.
|
|
func (s *Store) OpenCodeEndpoint(ctx context.Context) (string, error) {
|
|
var value string
|
|
err := s.db.QueryRowContext(ctx, `SELECT value FROM server_settings WHERE key=?`, openCodeEndpointSetting).Scan(&value)
|
|
if err == sql.ErrNoRows {
|
|
return "", nil
|
|
}
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return value, nil
|
|
}
|