cli

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: MIT Imports: 7 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"`
	Schema  string `mapstructure:"schema"`
	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"`
}

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) ResolvedSchema

func (c *Config) ResolvedSchema() string

ResolvedSchema returns the effective schema path, with generate.client.schema taking precedence over top-level schema (for generate command).

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"`
}

DatabaseConfig holds database connection settings.

type DoctorConfig

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

DoctorConfig holds doctor command settings.

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"`
}

GenerateConfig holds code generation settings.

type MigrateConfig

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

MigrateConfig holds migration settings.

Jump to

Keyboard shortcuts

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