dbcontext

package
v0.35.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package dbcontext is the one resolver for "which database and path does this command act on" (decision 0008, spec/features/database-context-navigation REQ:use-sets-scoped-context and REQ:context-lookup).

The client side (CLI, TUI) knows its working directory, so it computes the project root and the walk-up candidates here and sends them; the server stores and reads contexts under <OVDB home>/contexts, keyed by a hash of the canonical directory, and applies the same ladder with Resolve.

Index

Constants

View Source
const (
	ScopeFlag        = "flag"        // --db
	ScopeEnvironment = "environment" // OVDB_DATABASE (and OVDB_PATH)
	ScopeProject     = "project"     // ovdb use, found by walk-up
	ScopeGlobal      = "global"      // ovdb use --global
	ScopeOnly        = "only"        // the only registered database
)

Rungs of the precedence ladder, as Context.Scope names them.

View Source
const (
	EnvDatabase = "OVDB_DATABASE"
	EnvPath     = "OVDB_PATH"
)

Environment variables of the environment rung.

View Source
const (
	Dir = "contexts"
)

Locations under OVDB home. Nothing is ever written into a project.

Variables

This section is empty.

Functions

func Canonical

func Canonical(dir string) string

Canonical is dir as an absolute path with symlinks resolved, so the same project reached through a link has one context. A path that cannot be resolved (it no longer exists) is only made absolute and clean.

func ChooseNext

func ChooseNext(ids []string) []envelope.Next

ChooseNext is how to pick a database when none applies.

func ClearDatabase

func ClearDatabase(home, id string) error

ClearDatabase removes every stored context naming id, so removing a database never leaves a project pointing at it.

func Key

func Key(dir string) string

Key is the file name a project directory's context is stored under.

func NoContext

func NoContext(ids []string) *envelope.Error

NoContext is not_found for a data command with no database from any rung (AC:no-context-error), listing the registered databases.

func Registered

func Registered(ids []string, id string) (string, bool)

Registered is the registered id matching id exactly, or the only one that matches ignoring case (ids are unique ignoring case).

func Source

func Source(c Context) string

Source names where c came from, for `ovdb use` and `ovdb pwd` (REQ:context-lookup): "project context from /p/a".

func UnknownDatabase

func UnknownDatabase(message, id string, ids []string) *envelope.Error

UnknownDatabase is not_found for a database name that is not registered, listing the ones that are.

func UsingMessage

func UsingMessage(c Context) string

UsingMessage is the line saying what a change set, naming its scope (REQ:use-sets-scoped-context): `Now using todo for this project (/p/a)`.

Types

type Change

type Change struct {
	Scope    string `json:"scope"` // project or global
	Dir      string `json:"dir,omitempty"`
	Database string `json:"database,omitempty"`
	Path     string `json:"path,omitempty"`
	Clear    bool   `json:"clear,omitempty"`
}

Change is the body of PUT /api/local/v1/context.

type Context

type Context struct {
	Database string `json:"database"`
	Path     string `json:"path"`
	Scope    string `json:"scope"`
	Dir      string `json:"dir,omitempty"`
}

Context is the database and path one command acts on, and where that came from: the rung, and for a project context the directory that supplied it.

type Document

type Document struct {
	Schema int `json:"schema"`
	// Context is what applies here; null when nothing does.
	Context *Context `json:"context"`
	// Global is the default for all projects, whatever applies here; the web
	// console shows and changes only this one (parity E3).
	Global *Context `json:"global"`
	// Databases are the registered ids, for choosing one.
	Databases []string `json:"databases"`
	// Message says what a change did, naming its scope.
	Message string `json:"message,omitempty"`
	// Notices are one-line warnings, such as a project context skipped
	// because its database is no longer registered.
	Notices []string        `json:"notices,omitempty"`
	Next    []envelope.Next `json:"next"`
}

Document is the body of GET and PUT /api/local/v1/context and the --json output of `ovdb use` and `ovdb pwd`.

func Apply

func Apply(home string, ids []string, change Change) (Document, error)

Apply stores or clears a context for change and returns the stored context's document. ids are the registered databases; the caller holds the registry.

func Resolve

func Resolve(home string, ids []string, request Request) Document

Resolve applies the ladder: --db or OVDB_DATABASE, the nearest project context, the global default, the only registered database. A stored context naming a database that is no longer registered is skipped.

type Lookup

type Lookup struct {
	// Dirs runs from the working directory up to the Git working-tree root
	// when inside one, otherwise to the top of the file system.
	Dirs []string
	// Root is the nearest Git working-tree root, or the working directory.
	Root string
}

Lookup is where a command runs: the directories to search for a project context, nearest first, and the root `ovdb use` writes to.

func Find

func Find(cwd string) Lookup

Find walks up from cwd (canonicalised). A directory holding `.git` — a folder in a main checkout, a file in a linked worktree or submodule — is a working-tree root, so every linked worktree is its own project. Git is detected from these markers alone; the git binary is not needed.

type Request

type Request struct {
	// Dirs are Lookup.Dirs, nearest first; empty for the web console.
	Dirs []string
	// Database and Path come from --db or OVDB_DATABASE/OVDB_PATH, with
	// Scope ScopeFlag or ScopeEnvironment; empty otherwise.
	Database, Path, Scope string
	// Root is Lookup.Root, where `ovdb use` stores; never sent.
	Root string
}

Request is what a client sends to resolve its context.

func RequestFrom

func RequestFrom(values url.Values) Request

RequestFrom reads a Request from a query string. Only absolute directories count: the browser, which has no working directory, sends none.

func (Request) Query

func (r Request) Query() url.Values

Query is r as the query string of GET /api/local/v1/context, home and status.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL