Documentation
¶
Overview ¶
Package config provides configuration file management for the gosqlx CLI.
Overview ¶
This package handles loading, parsing, validating, and saving configuration files for the gosqlx CLI. Configuration files allow users to set default values for command options, reducing the need for repetitive command-line flags.
Configuration File Format ¶
GoSQLX uses YAML format for configuration files (.gosqlx.yml):
format:
indent: 2
uppercase_keywords: true
max_line_length: 80
compact: false
validate:
dialect: postgresql
strict_mode: false
recursive: false
pattern: "*.sql"
security:
max_file_size: 10485760 # 10MB
output:
format: auto
verbose: false
analyze:
security: true
performance: true
complexity: true
all: false
Configuration Search Path ¶
Configuration files are searched in the following order with precedence:
- Current directory: .gosqlx.yml
- Home directory: ~/.gosqlx.yml
- System-wide: /etc/gosqlx.yml
- Built-in defaults (if no config file found)
CLI flags always override configuration file settings.
Usage ¶
## Loading Configuration
Load from default locations:
cfg, err := config.LoadDefault()
if err != nil {
// Handle error or use defaults
cfg = config.DefaultConfig()
}
Load from specific file:
cfg, err := config.Load("/path/to/config.yml")
if err != nil {
return err
}
## Creating Configuration
Create with defaults:
cfg := config.DefaultConfig()
Customize settings:
cfg.Format.Indent = 4 cfg.Format.UppercaseKeywords = false cfg.Validation.Dialect = "mysql"
## Saving Configuration
Save to file:
if err := cfg.Save(".gosqlx.yml"); err != nil {
return err
}
## Validation
Configuration is automatically validated during Load():
cfg, err := config.Load("config.yml")
// cfg is validated, or err contains validation errors
Explicit validation:
if err := cfg.Validate(); err != nil {
// Handle validation errors
}
## Merging Configurations
Merge CLI flags with config file (CLI flags take precedence):
cfg, _ := config.LoadDefault()
cliConfig := &config.Config{
Format: config.FormatConfig{Indent: 4}, // From CLI flag
}
cfg.Merge(cliConfig)
Configuration Sections ¶
## Format Configuration
Controls SQL formatting behavior:
format: indent: 2 # Indentation size in spaces (0-8) uppercase_keywords: true # Uppercase SQL keywords max_line_length: 80 # Maximum line length (0-500) compact: false # Compact format (minimal whitespace)
Fields:
- Indent: Number of spaces for indentation (default: 2)
- UppercaseKeywords: Convert keywords to uppercase (default: true)
- MaxLineLength: Maximum line length for wrapping (default: 80)
- Compact: Use compact format with minimal whitespace (default: false)
## Validation Configuration
Controls SQL validation behavior:
validate:
dialect: postgresql # SQL dialect for validation
strict_mode: false # Enable strict validation
recursive: false # Recursively process directories
pattern: "*.sql" # File pattern for recursive processing
security:
max_file_size: 10485760 # Maximum file size in bytes
Fields:
- Dialect: SQL dialect (postgresql, mysql, sqlserver, oracle, sqlite, generic)
- StrictMode: Enable strict validation rules (default: false)
- Recursive: Recursively process directories (default: false)
- Pattern: File pattern for recursive processing (default: "*.sql")
- Security.MaxFileSize: Maximum allowed file size in bytes (default: 10MB)
## Output Configuration
Controls output formatting and verbosity:
output: format: auto # Output format (json, yaml, table, tree, auto) verbose: false # Enable verbose output
Fields:
- Format: Output format (json, yaml, table, tree, auto) (default: auto)
- Verbose: Enable detailed output for debugging (default: false)
## Analysis Configuration
Controls SQL analysis behavior:
analyze: security: true # Perform security analysis performance: true # Perform performance analysis complexity: true # Calculate complexity metrics all: false # Comprehensive analysis (all above)
Fields:
- Security: Enable security vulnerability detection (default: true)
- Performance: Enable performance analysis (default: true)
- Complexity: Enable complexity metrics (default: true)
- All: Enable comprehensive analysis (default: false)
Configuration Validation ¶
The package validates configuration values to ensure they are within acceptable ranges:
Format validation:
- Indent: 0-8 spaces
- MaxLineLength: 0-500 characters
Validation validation:
- Dialect: Must be one of: postgresql, mysql, sqlserver, oracle, sqlite, generic
Output validation:
- Format: Must be one of: json, yaml, table, tree, auto
Invalid configurations are rejected with descriptive error messages.
CLI Flag Precedence ¶
Configuration files provide defaults, but CLI flags always take precedence:
# Config file has indent: 2 gosqlx format query.sql # Uses indent: 2 from config gosqlx format --indent 4 query.sql # Uses indent: 4 from CLI flag
The package tracks which flags were explicitly set to ensure proper precedence.
Schema Validation ¶
The package provides schema validation utilities in schema.go:
- Schema definition for configuration structure
- Type checking for configuration values
- Range validation for numeric values
- Enum validation for string values
Schema validation is used by:
- Load() function to validate loaded configurations
- Validate() method to check configuration correctness
- CLI config validate command to verify user configurations
Error Handling ¶
Configuration errors are returned with context:
cfg, err := config.Load("config.yml")
if err != nil {
// Possible errors:
// - File not found
// - YAML parsing error
// - Validation error
// - Home directory lookup failure
}
Validation errors include field names and acceptable ranges:
invalid configuration: format.indent must be between 0 and 8, got 10 invalid configuration: validate.dialect must be one of: [postgresql mysql ...], got 'custom'
Best Practices ¶
## Project Configuration
Place .gosqlx.yml in project root for team-wide defaults:
# .gosqlx.yml format: indent: 2 uppercase_keywords: true validate: dialect: postgresql recursive: true
## User Configuration
Place .gosqlx.yml in home directory for personal defaults:
# ~/.gosqlx.yml output: verbose: true format: indent: 4
## CI/CD Configuration
Use explicit flags in CI/CD for clarity and reproducibility:
gosqlx validate --dialect postgresql --strict ./sql/ gosqlx format --check --indent 2 ./sql/*.sql
Thread Safety ¶
Configuration objects are not thread-safe. Each goroutine should have its own configuration instance or use appropriate synchronization.
Loading configuration is safe for concurrent use as it creates new instances.
Performance ¶
Configuration loading is optimized:
- Files are loaded once and cached by the application
- YAML parsing uses efficient unmarshalers
- Validation is performed once at load time
Configuration file size should be kept small (< 1KB typically) for fast loading.
Examples ¶
## Complete Configuration Example
# .gosqlx.yml - Complete configuration example
format:
indent: 2
uppercase_keywords: true
max_line_length: 80
compact: false
validate:
dialect: postgresql
strict_mode: false
recursive: true
pattern: "*.sql"
security:
max_file_size: 10485760
output:
format: auto
verbose: false
analyze:
security: true
performance: true
complexity: true
all: false
## Programmatic Configuration
// Create custom configuration
cfg := &config.Config{
Format: config.FormatConfig{
Indent: 4,
UppercaseKeywords: false,
MaxLineLength: 120,
Compact: false,
},
Validation: config.ValidationConfig{
Dialect: "mysql",
StrictMode: true,
Recursive: true,
Pattern: "*.sql",
Security: config.SecurityConfig{
MaxFileSize: 5 * 1024 * 1024, // 5MB
},
},
Output: config.OutputConfig{
Format: "json",
Verbose: true,
},
Analyze: config.AnalyzeConfig{
Security: true,
Performance: true,
Complexity: true,
All: false,
},
}
// Validate
if err := cfg.Validate(); err != nil {
log.Fatal(err)
}
// Save to file
if err := cfg.Save(".gosqlx.yml"); err != nil {
log.Fatal(err)
}
See Also ¶
- cmd/gosqlx/cmd/config.go - Config management commands
- cmd/gosqlx/cmd/config_manager.go - Config manager implementation
- docs/CONFIGURATION.md - User-facing configuration documentation
Index ¶
- Variables
- func ValidateDialect(dialect string) error
- func ValidateIndent(indent int) error
- func ValidateMaxLineLength(length int) error
- func ValidateOutputFormat(format string) error
- type AnalyzeConfig
- type AnalyzeSchema
- type Config
- type ConfigSchema
- type FormatConfig
- type FormatSchema
- type OutputConfig
- type OutputSchema
- type SecurityConfig
- type ValidationConfig
- type ValidationSchema
Constants ¶
This section is empty.
Variables ¶
var ValidDialects = []string{
"postgresql",
"mysql",
"sqlserver",
"oracle",
"sqlite",
"generic",
}
ValidDialects lists all supported SQL dialects
var ValidOutputFormats = []string{
"json",
"yaml",
"table",
"tree",
"auto",
}
ValidOutputFormats lists all supported output formats
Functions ¶
func ValidateDialect ¶
ValidateDialect checks if a dialect is valid
func ValidateIndent ¶
ValidateIndent checks if indent value is within acceptable range
func ValidateMaxLineLength ¶
ValidateMaxLineLength checks if max line length is within acceptable range
func ValidateOutputFormat ¶
ValidateOutputFormat checks if an output format is valid
Types ¶
type AnalyzeConfig ¶
type AnalyzeConfig struct {
Security bool `yaml:"security"`
Performance bool `yaml:"performance"`
Complexity bool `yaml:"complexity"`
All bool `yaml:"all"`
}
AnalyzeConfig holds SQL analysis options.
Controls which analysis features are enabled by default.
Fields:
- Security: Perform security vulnerability analysis (default: true)
- Performance: Perform performance analysis (default: true)
- Complexity: Calculate complexity metrics (default: true)
- All: Enable comprehensive analysis (default: false)
type AnalyzeSchema ¶
type AnalyzeSchema struct {
Security struct {
Default bool `yaml:"default"`
Desc string `yaml:"description"`
} `yaml:"security"`
Performance struct {
Default bool `yaml:"default"`
Desc string `yaml:"description"`
} `yaml:"performance"`
Complexity struct {
Default bool `yaml:"default"`
Desc string `yaml:"description"`
} `yaml:"complexity"`
All struct {
Default bool `yaml:"default"`
Desc string `yaml:"description"`
} `yaml:"all"`
}
AnalyzeSchema defines constraints for analyze settings
type Config ¶
type Config struct {
Format FormatConfig `yaml:"format"`
Validation ValidationConfig `yaml:"validate"`
Output OutputConfig `yaml:"output"`
Analyze AnalyzeConfig `yaml:"analyze"`
}
Config represents the complete GoSQLX CLI configuration.
Configuration files use YAML format and are loaded from:
- Current directory: .gosqlx.yml
- Home directory: ~/.gosqlx.yml
- System: /etc/gosqlx.yml
CLI flags always override configuration file settings.
Example configuration file:
format:
indent: 2
uppercase_keywords: true
max_line_length: 80
compact: false
validate:
dialect: postgresql
strict_mode: false
recursive: false
pattern: "*.sql"
security:
max_file_size: 10485760
output:
format: auto
verbose: false
analyze:
security: true
performance: true
complexity: true
all: false
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a configuration with sensible defaults.
This function creates a Config instance with production-ready defaults suitable for most use cases. These defaults balance performance, code quality, and compatibility.
Default values:
- Format: 2-space indentation, uppercase keywords, 80-char line length
- Validation: PostgreSQL dialect, non-strict mode, *.sql pattern
- Security: 10MB max file size
- Output: Auto format selection, non-verbose
- Analysis: All features enabled except comprehensive mode
Returns:
- *Config with default values initialized
Usage:
cfg := config.DefaultConfig()
cfg.Format.Indent = 4 // Customize as needed
cfg.Save(".gosqlx.yml")
func Load ¶
Load reads a configuration file from the specified path.
This function loads and parses a YAML configuration file, validates the configuration values, and returns a Config instance.
The path is automatically expanded if it starts with ~ (home directory).
Parameters:
- path: File path to the configuration file (absolute or relative)
Returns:
- *Config: Loaded and validated configuration
- error: File reading, parsing, or validation error
Possible errors:
- File not found or inaccessible
- Invalid YAML syntax
- Configuration validation failure (invalid values)
- Home directory lookup failure (for ~ expansion)
Example:
cfg, err := config.Load(".gosqlx.yml")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
fmt.Printf("Using dialect: %s\n", cfg.Validation.Dialect)
func LoadDefault ¶
LoadDefault tries to load configuration from standard locations with precedence.
This function searches for configuration files in multiple standard locations and loads the first one found. If no configuration file exists, it returns a default configuration.
Search order (first found wins):
- Current directory: .gosqlx.yml
- Home directory: ~/.gosqlx.yml
- System-wide: /etc/gosqlx.yml
- Built-in defaults (if no file found)
This allows for flexible configuration strategies:
- Project-specific config in current directory
- User-specific config in home directory
- System-wide config for all users
- Automatic fallback to sensible defaults
Returns:
- *Config: Loaded configuration or defaults
- error: Always returns a valid Config (errors are logged but not fatal)
Example:
cfg, err := config.LoadDefault() // cfg is always non-nil, even if err != nil // err indicates which config was loaded or if defaults were used
func (*Config) Merge ¶
Merge applies settings from another config, with the other config taking precedence
type ConfigSchema ¶
type ConfigSchema struct {
Format FormatSchema `yaml:"format"`
Validation ValidationSchema `yaml:"validate"`
Output OutputSchema `yaml:"output"`
Analyze AnalyzeSchema `yaml:"analyze"`
}
ConfigSchema defines the structure and constraints for configuration
func GetSchema ¶
func GetSchema() *ConfigSchema
GetSchema returns the complete configuration schema with descriptions
type FormatConfig ¶
type FormatConfig struct {
Indent int `yaml:"indent"`
UppercaseKeywords bool `yaml:"uppercase_keywords"`
MaxLineLength int `yaml:"max_line_length"`
Compact bool `yaml:"compact"`
}
FormatConfig holds SQL formatting options.
Controls how SQL is formatted by the format command.
Fields:
- Indent: Number of spaces for indentation (0-8, default: 2)
- UppercaseKeywords: Convert SQL keywords to uppercase (default: true)
- MaxLineLength: Maximum line length for wrapping (0-500, default: 80)
- Compact: Use compact format with minimal whitespace (default: false)
type FormatSchema ¶
type FormatSchema struct {
Indent struct {
Min int `yaml:"min"`
Max int `yaml:"max"`
Default int `yaml:"default"`
Desc string `yaml:"description"`
} `yaml:"indent"`
UppercaseKeywords struct {
Default bool `yaml:"default"`
Desc string `yaml:"description"`
} `yaml:"uppercase_keywords"`
MaxLineLength struct {
Min int `yaml:"min"`
Max int `yaml:"max"`
Default int `yaml:"default"`
Desc string `yaml:"description"`
} `yaml:"max_line_length"`
Compact struct {
Default bool `yaml:"default"`
Desc string `yaml:"description"`
} `yaml:"compact"`
}
FormatSchema defines constraints for format settings
type OutputConfig ¶
type OutputConfig struct {
Format string `yaml:"format"` // json, yaml, table
Verbose bool `yaml:"verbose"`
}
OutputConfig holds output formatting options.
Controls output format and verbosity for all commands.
Fields:
- Format: Output format (json, yaml, table, tree, auto) (default: auto)
- Verbose: Enable detailed output for debugging (default: false)
type OutputSchema ¶
type OutputSchema struct {
Format struct {
Options []string `yaml:"options"`
Default string `yaml:"default"`
Desc string `yaml:"description"`
} `yaml:"format"`
Verbose struct {
Default bool `yaml:"default"`
Desc string `yaml:"description"`
} `yaml:"verbose"`
}
OutputSchema defines constraints for output settings
type SecurityConfig ¶
type SecurityConfig struct {
MaxFileSize int64 `yaml:"max_file_size"` // Maximum file size in bytes
}
SecurityConfig holds security-related limits for file operations.
Prevents resource exhaustion and security vulnerabilities.
Fields:
- MaxFileSize: Maximum allowed file size in bytes (default: 10MB)
Example:
security: max_file_size: 20971520 # 20MB
type ValidationConfig ¶
type ValidationConfig struct {
Dialect string `yaml:"dialect"`
StrictMode bool `yaml:"strict_mode"`
Recursive bool `yaml:"recursive"`
Pattern string `yaml:"pattern"`
Security SecurityConfig `yaml:"security"`
}
ValidationConfig holds SQL validation options.
Controls validation behavior including dialect selection and security limits.
Fields:
- Dialect: SQL dialect for validation (postgresql, mysql, sqlserver, oracle, sqlite, generic)
- StrictMode: Enable strict validation rules (default: false)
- Recursive: Recursively process directories (default: false)
- Pattern: File pattern for recursive processing (default: "*.sql")
- Security: Security-related limits (file size, etc.)
type ValidationSchema ¶
type ValidationSchema struct {
Dialect struct {
Options []string `yaml:"options"`
Default string `yaml:"default"`
Desc string `yaml:"description"`
} `yaml:"dialect"`
StrictMode struct {
Default bool `yaml:"default"`
Desc string `yaml:"description"`
} `yaml:"strict_mode"`
Recursive struct {
Default bool `yaml:"default"`
Desc string `yaml:"description"`
} `yaml:"recursive"`
Pattern struct {
Default string `yaml:"default"`
Desc string `yaml:"description"`
} `yaml:"pattern"`
}
ValidationSchema defines constraints for validation settings