cli

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 8 Imported by: 0

README

cli

Shared configuration and error handling for the melange CLI.

Responsibility

This package provides infrastructure shared across CLI commands:

  • Configuration loading - Discovers and parses melange.yaml with proper precedence (flags > env > config > defaults)
  • Exit codes - Standardized exit codes for different failure modes
  • Error wrapping - Typed errors that carry exit codes for proper CLI behavior

Architecture Role

cmd/melange/main.go
       │
       ├── internal/cli (config, errors)
       │
       └── individual command handlers

The CLI commands use this package to:

  1. Load configuration from melange.yaml or environment
  2. Build database connection strings
  3. Return structured errors with appropriate exit codes

Key Components

  • Config - Top-level configuration struct with database, generate, migrate, and doctor settings
  • LoadConfig() - Discovers config file by walking up to .git boundary
  • ExitError - Error type carrying exit code for os.Exit()
  • Exit code constants (ExitConfig, ExitSchemaParse, ExitDBConnect)

Configuration Precedence

  1. CLI flags (highest)
  2. Environment variables (MELANGE_*)
  3. Config file (melange.yaml)
  4. Defaults (lowest)

Documentation

Overview

Package cli provides shared configuration and utilities for the melange CLI.

Index

Constants

View Source
const (
	ExitSuccess     = 0
	ExitGeneral     = 1
	ExitConfig      = 2
	ExitSchemaParse = 3
	ExitDBConnect   = 4
)

Exit codes per spec.

Variables

This section is empty.

Functions

func ExitWithError

func ExitWithError(err error)

ExitWithError prints the error and exits with the appropriate code.

Types

type ClientConfig

type ClientConfig struct {
	Runtime string `mapstructure:"runtime"`
	Output  string `mapstructure:"output"`
	Package string `mapstructure:"package"`
	Filter  string `mapstructure:"filter"`
	IDType  string `mapstructure:"id_type"`
}

ClientConfig holds client code generation settings.

type Config

type Config struct {
	// Schema is the path to the OpenFGA schema file (e.g., "schemas/schema.fga")
	Schema string `mapstructure:"schema"`

	// Database configuration
	Database DatabaseConfig `mapstructure:"database"`

	// Per-command configuration
	Generate GenerateConfig `mapstructure:"generate"`
	Migrate  MigrateConfig  `mapstructure:"migrate"`
	Doctor   DoctorConfig   `mapstructure:"doctor"`

	// DefaultEnvironment names the environment applied when neither the --env
	// flag nor MELANGE_ENV selects one. Empty means "use the base config".
	DefaultEnvironment string `mapstructure:"default_environment"`

	// Environments are named connection profiles. Each overlays the base
	// configuration (see ForEnvironment); a field an environment leaves unset
	// falls through to the base. Absent map ⇒ behavior identical to before
	// environments existed.
	Environments map[string]EnvironmentConfig `mapstructure:"environments"`
}

Config represents the melange configuration from melange.yaml.

func LoadConfig

func LoadConfig(explicitConfigPath string) (*Config, string, error)

LoadConfig discovers and loads configuration with proper precedence: flags > env > config file > defaults.

Returns the loaded config, the path to the config file (empty if none found), and any error encountered.

func (*Config) DSN

func (c *Config) DSN() (string, error)

DSN returns the database connection string. If database.url is set, it's returned directly. Otherwise, builds a DSN from discrete fields.

func (*Config) EnvironmentSummary added in v0.9.3

func (c *Config) EnvironmentSummary(name string) (string, error)

EnvironmentSummary returns a redacted, human-readable connection target for the named environment (or the base config when name == ""). Unlike ForEnvironment it does not expand ${VAR} references — a listing should show what is configured, not whatever happens to be in the current shell — and it redacts any URL password. Returns an error only when name is a non-empty environment that is not defined.

func (*Config) ForEnvironment added in v0.9.3

func (c *Config) ForEnvironment(envName string) (*Config, error)

ForEnvironment returns a copy of the config with the named environment overlaid and ${VAR} references expanded. An empty envName returns the base config (still with ${VAR} expansion applied, so the base database block may reference secrets too).

A non-empty envName that is not defined is an error (returned with a nil config). An unset ${VAR} reference is also an error — an unset secret must fail loud rather than silently resolve and retarget the command — but there the returned config is still populated best-effort (unset references expand to empty), so callers may use its non-connection fields (e.g. Schema) and honor an explicit --db while any DSN attempt fails loud.

func (*Config) HasEnvironment added in v0.9.3

func (c *Config) HasEnvironment(name string) bool

HasEnvironment reports whether the named environment profile is defined.

func (*Config) Redacted added in v0.9.3

func (c *Config) Redacted() *Config

Redacted returns a copy of the config safe to print: every database password (discrete field and URL userinfo) across the base block and all environment profiles is masked. Used by `config show` so resolved ${VAR} secrets are not echoed in cleartext.

type DatabaseConfig

type DatabaseConfig struct {
	URL      string `mapstructure:"url"`
	Host     string `mapstructure:"host"`
	Port     int    `mapstructure:"port"`
	Name     string `mapstructure:"name"`
	User     string `mapstructure:"user"`
	Password string `mapstructure:"password"`
	SSLMode  string `mapstructure:"sslmode"`
	Schema   string `mapstructure:"schema"`
}

DatabaseConfig holds database connection settings.

type DoctorConfig

type DoctorConfig struct {
	Verbose         bool `mapstructure:"verbose"`
	SkipPerformance bool `mapstructure:"skip_performance"`
}

DoctorConfig holds doctor command settings.

type EnvironmentConfig added in v0.9.3

type EnvironmentConfig struct {
	Database DatabaseConfig `mapstructure:"database"`
	Schema   string         `mapstructure:"schema"`
}

EnvironmentConfig is a named connection profile overlaid onto the base Config by ForEnvironment. Typically only Database is set; Schema is available for the uncommon case of an environment that reads a different schema file.

type ExitError

type ExitError struct {
	Code    int
	Message string
	Err     error
}

ExitError wraps an error with an exit code.

func ConfigError

func ConfigError(msg string, err error) *ExitError

ConfigError creates an ExitError with ExitConfig code.

func DBConnectError

func DBConnectError(msg string, err error) *ExitError

DBConnectError creates an ExitError with ExitDBConnect code.

func GeneralError

func GeneralError(msg string, err error) *ExitError

GeneralError creates an ExitError with ExitGeneral code.

func SchemaParseError

func SchemaParseError(msg string, err error) *ExitError

SchemaParseError creates an ExitError with ExitSchemaParse code.

func (*ExitError) Error

func (e *ExitError) Error() string

func (*ExitError) Unwrap

func (e *ExitError) Unwrap() error

type GenerateConfig

type GenerateConfig struct {
	Client    ClientConfig       `mapstructure:"client"`
	Migration MigrationGenConfig `mapstructure:"migration"`
}

GenerateConfig holds code generation settings.

type MigrateConfig

type MigrateConfig struct {
	DryRun bool `mapstructure:"dry_run"`
	Force  bool `mapstructure:"force"`
}

MigrateConfig holds settings for `melange migrate` (builtin migration).

type MigrationGenConfig added in v0.9.0

type MigrationGenConfig struct {
	Output string `mapstructure:"output"`
	Name   string `mapstructure:"name"`
	Format string `mapstructure:"format"`
}

MigrationGenConfig holds settings for `melange generate migration`, which produces versioned SQL files for use with external migration frameworks. This is distinct from MigrateConfig, which controls `melange migrate` (the built-in apply-to-database workflow). Configuring both for the same database is discouraged; the migrate command warns when generate.migration.output is set.

Directories

Path Synopsis
Package render formats melange.Trace values for terminal display.
Package render formats melange.Trace values for terminal display.

Jump to

Keyboard shortcuts

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