config

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package config loads the optional crdb-sql.yaml project file that maps schema-file globs to query-file globs. When a config is present, schema-aware subcommands (currently validate) can run with no flags and operate over every matching query file in the project.

The file lives at the project root and is auto-discovered from the current working directory by the root command's PersistentPreRunE. Absence of a config file is not an error — Discover returns (nil, nil) and the subcommand falls back to its existing flag-driven behavior.

The schema is intentionally small (one version, repeating schema/queries pairs); richer fields (per-pair name, default tier, connection profile) can be added in future versions without breaking readers that already understand version 1.

Index

Constants

View Source
const MaxConfigFileSize = 1 << 20 // 1 MiB

MaxConfigFileSize caps the size of a config file Load will read. Configs are tiny by nature; the limit exists only to prevent a pathological 1GB YAML file from being slurped into memory.

View Source
const SupportedVersion = 1

SupportedVersion is the only schema version this build understands. Bumping it is a breaking change for older binaries; introduce a new version only when a field's meaning changes incompatibly.

Variables

View Source
var DefaultFilenames = []string{"crdb-sql.yaml", "crdb-sql.yml"}

DefaultFilenames are the names Discover looks for in the working directory, in priority order. Both spellings are accepted because .yaml and .yml are equally common in Go projects.

Functions

This section is empty.

Types

type File

type File struct {
	Version int       `yaml:"version"`
	SQL     []SQLPair `yaml:"sql"`

	// BaseDir is the directory the config file was loaded from. Not
	// present in the YAML; populated by Load. Empty for File values
	// constructed in tests without going through Load — those tests
	// must use absolute glob patterns.
	BaseDir string `yaml:"-"`
}

File is the parsed crdb-sql.yaml. It is populated by Load (and Discover) and then attached to the root command's per-invocation state so subcommands can read it.

BaseDir is set by the loader to the directory containing the YAML file. Globs in SQL[*].Schema and SQL[*].Queries are resolved relative to BaseDir, which makes configs portable across machines (no absolute paths required) and makes tests easy to write (point the loader at t.TempDir()).

func Discover

func Discover(cwd string) (*File, error)

Discover looks for a config file in cwd. If none of DefaultFilenames exists, it returns (nil, nil) — absence is not an error, callers just fall through to their existing flag-driven behavior.

Only cwd is checked; there is no walk-up to parent directories. This matches the issue spec ("from CWD") and avoids surprises where a config two levels up silently changes a command's behavior.

func Load

func Load(path string) (*File, error)

Load reads and parses the config file at path. The returned *File has BaseDir set to filepath.Dir(path) so subsequent glob expansion resolves patterns relative to the config's location.

Load is strict: unknown YAML fields produce an error rather than being silently dropped. This catches typos in the config (e.g. `querys:` instead of `queries:`) at load time instead of producing a confusing empty match later.

type SQLPair

type SQLPair struct {
	Schema  []string `yaml:"schema"`
	Queries []string `yaml:"queries"`
}

SQLPair maps a set of CREATE TABLE schema files to the queries that should be validated against that schema. Both sides are lists of glob patterns (doublestar syntax: `**` matches any depth).

Multiple pairs in one config let a project keep, e.g., production schema with its queries separate from a test fixture schema with its own queries.

func (SQLPair) ExpandQueries

func (p SQLPair) ExpandQueries(baseDir string) ([]string, error)

ExpandQueries is the queries-side counterpart to ExpandSchema; see that method for the semantics of pattern matching, deduplication, and zero matches.

func (SQLPair) ExpandSchema

func (p SQLPair) ExpandSchema(baseDir string) ([]string, error)

ExpandSchema returns the concrete file paths matched by the pair's Schema globs, resolved against baseDir. Patterns may use doublestar syntax (`**` for recursive matches). Results are deduplicated and sorted for stable downstream behavior.

An empty pattern list returns an empty slice (no error). A pattern that matches nothing also produces no error here — the caller decides whether zero matches is meaningful (validate, for instance, treats a pair with no query matches as a no-op rather than a failure).

Jump to

Keyboard shortcuts

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