config

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package config provides .gosqlx.yml configuration loading for the linter.

The configuration file controls which rules run, their severity overrides, optional per-rule parameters, and file ignore patterns. It is typically committed to a project root so a team shares a consistent lint policy.

Example .gosqlx.yml:

rules:
  L001:
    enabled: true
    severity: error
  L005:
    enabled: true
    params:
      max_length: 120
  L011:
    enabled: false
ignore:
  - "migrations/*.sql"
  - "vendor/**"
default_severity: warning

Typical use:

cfg, err := config.LoadDefault()
if err != nil && !errors.Is(err, config.ErrNotFound) {
    log.Fatal(err)
}
rules := cfg.Apply(allRules)
l := linter.NewWithConfig(cfg, rules...)

Unknown rule IDs in the config file are reported via Config.Warnings but do not cause Load to fail. This allows forward compatibility when older installations read configs that reference newer rules.

Index

Constants

View Source
const DefaultFilename = ".gosqlx.yml"

DefaultFilename is the conventional name for the linter configuration file.

Variables

View Source
var ErrNotFound = errors.New("gosqlx: no .gosqlx.yml found")

ErrNotFound is returned by LoadDefault when no .gosqlx.yml is found while walking up from the current working directory. Callers that want to treat a missing config as "use built-in defaults" should check with errors.Is.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Rules maps rule IDs (e.g. "L001") to per-rule configuration.
	Rules map[string]RuleConfig `yaml:"rules"`

	// Ignore is a list of glob patterns for file paths to skip during linting.
	// Patterns support "*" (single path segment) and "**" (any number of
	// segments). Patterns are matched against the filename passed to the
	// linter (LintFile, LintString's filename arg, or LintDirectory entries).
	Ignore []string `yaml:"ignore"`

	// DefaultSeverity, if set, is applied to rules whose RuleConfig.Severity
	// is empty. Valid values: "error", "warning", "info". Empty means the
	// rule's built-in severity is used.
	DefaultSeverity string `yaml:"default_severity"`

	// Path is the absolute filesystem path the config was loaded from.
	// Empty for configs constructed in memory.
	Path string `yaml:"-"`

	// Warnings are non-fatal diagnostics produced during Load (e.g. unknown
	// rule IDs, unknown severity values). They are forward-compatibility
	// signals, not errors.
	Warnings []string `yaml:"-"`
}

Config represents the parsed contents of a .gosqlx.yml file.

Config is immutable after Load returns. Methods on *Config do not mutate it.

func Load

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

Load reads and parses the configuration file at the given path.

Returns an error if the file cannot be read or contains invalid YAML. Unknown rule IDs and unknown severity values become Warnings rather than errors so the linter can keep running against newer configs.

func LoadDefault

func LoadDefault() (*Config, error)

LoadDefault searches for .gosqlx.yml starting at the current working directory and walking up toward the filesystem root. It returns the first match found, or ErrNotFound if none exists.

Use errors.Is(err, ErrNotFound) to distinguish "no config" from a real I/O or parse error.

func (*Config) Apply

func (c *Config) Apply(rules []linter.Rule) []linter.Rule

Apply filters and configures the given rules per this config.

Behavior:

  • A rule entry with Enabled == &false drops the rule from the result.
  • A rule entry with a valid Severity wraps the rule so Severity() returns the override, and emitted Violations carry that severity.
  • A rule with no entry uses DefaultSeverity if set and valid; otherwise the rule's built-in severity.
  • Rules in the config that don't appear in the input slice are ignored (no rule is instantiated from nothing).

The returned slice has the same order as the input, minus disabled rules. The input slice is not mutated.

func (*Config) ShouldIgnore

func (c *Config) ShouldIgnore(filename string) bool

ShouldIgnore reports whether filename matches any Ignore pattern.

Matching is performed against both the raw filename and its cleaned form. Patterns use "**" for "any number of path segments" and "*" for a single path segment. A nil or empty config returns false.

type RuleConfig

type RuleConfig struct {
	// Enabled is an optional pointer so the zero value ("not set") differs
	// from an explicit `enabled: false`.
	Enabled *bool `yaml:"enabled"`

	// Severity overrides the rule's default severity. Empty means "no override".
	Severity string `yaml:"severity"`

	// Params are rule-specific parameters (e.g. max_length for L005).
	// The types are whatever YAML produces (int, string, bool, []interface{}, map).
	Params map[string]any `yaml:"params"`
}

RuleConfig represents per-rule configuration entries.

A rule entry may set Enabled (default true if the rule appears at all), override Severity (error | warning | info), and supply rule-specific Params. Unknown params are preserved and may be consumed by rules that understand them; rules that don't read params simply ignore them.

Jump to

Keyboard shortcuts

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