config

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package config provides configuration loading and discovery for tally.

Configuration is loaded from multiple sources with the following priority (highest to lowest):

  1. CLI flags
  2. Environment variables (TALLY_* prefix)
  3. Config file (closest .tally.toml or tally.toml)
  4. Built-in defaults

Config file discovery follows a cascading pattern similar to Ruff: starting from the target file's directory, walk up the filesystem until a config file is found. The closest config wins (no merging).

Index

Constants

View Source
const EnvPrefix = "TALLY_"

EnvPrefix is the prefix for environment variables.

Variables

View Source
var ConfigFileNames = []string{".tally.toml", "tally.toml"}

ConfigFileNames defines the config file names to search for, in priority order.

Functions

func CIName

func CIName() string

CIName returns the detected CI provider name, or empty string if not in CI.

func DecodeRuleOptions

func DecodeRuleOptions[T any](rc *RulesConfig, ruleCode string, defaults T) T

GetOptionsTyped returns typed rule options merged over defaults. Returns defaults if the rule has no options or decoding fails.

func Discover

func Discover(targetPath string) string

Discover finds the closest config file for a target file path. It walks up the directory tree from the target's directory, checking for config files at each level. Returns empty string if no config file is found.

func SlowChecksEnabled

func SlowChecksEnabled(mode string) bool

SlowChecksEnabled returns whether slow checks should run based on the mode. "on" → true, "off" → false, "auto" → enabled when not running in CI.

Types

type AIConfig

type AIConfig struct {
	// Enabled toggles all AI features in tally. Disabled by default.
	Enabled bool `json:"enabled,omitempty" koanf:"enabled"`

	// Command is the ACP-capable agent program argv (stdio).
	// Example: ["acp-agent", "--model", "foo"].
	Command []string `json:"command,omitempty" koanf:"command"`

	// Timeout is the per-fix timeout (e.g. "90s"). Parsed with time.ParseDuration at runtime.
	Timeout string `json:"timeout,omitempty" koanf:"timeout"`

	// MaxInputBytes limits how much content tally will send to the agent (guards cost/latency).
	MaxInputBytes int `json:"max-input-bytes,omitempty" koanf:"max-input-bytes"`

	// RedactSecrets redacts obvious secrets before sending content to the agent.
	RedactSecrets bool `json:"redact-secrets,omitempty" koanf:"redact-secrets"`
}

AIConfig configures opt-in AI features.

This is intentionally minimal for MVP. Expand cautiously: AI behavior must remain opt-in.

type Config

type Config struct {
	// Rules contains configuration for individual linting rules.
	Rules RulesConfig `json:"rules" koanf:"rules"`

	// Output configures output format and destination.
	Output OutputConfig `json:"output" koanf:"output"`

	// InlineDirectives controls inline suppression directives.
	InlineDirectives InlineDirectivesConfig `json:"inline-directives" koanf:"inline-directives"`

	// AI configures opt-in AI features (e.g., AI AutoFix).
	AI AIConfig `json:"ai" koanf:"ai"`

	// FileValidation configures pre-parse file validation checks.
	FileValidation FileValidationConfig `json:"file-validation" koanf:"file-validation"`

	// SlowChecks configures async checks that require network or other slow I/O.
	SlowChecks SlowChecksConfig `json:"slow-checks" koanf:"slow-checks"`

	// ConfigFile is the path to the config file that was loaded (if any).
	// This is metadata, not loaded from config.
	ConfigFile string `json:"-" koanf:"-"`
}

Config represents the complete tally configuration.

func Default

func Default() *Config

Default returns the default configuration. Rule-specific defaults are owned by each rule via ConfigurableRule.DefaultConfig().

func Load

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

Load loads configuration for a target file path. It discovers the closest config file, loads it, and applies environment variable overrides.

func LoadFromFile

func LoadFromFile(configPath string) (*Config, error)

LoadFromFile loads configuration from a specific config file path. Unlike Load, it does not perform config discovery.

func LoadWithOverrides

func LoadWithOverrides(targetPath string, overrides map[string]any, preference ConfigurationPreference) (*Config, error)

LoadWithOverrides loads configuration for a target file path with an optional overrides map applied according to preference.

Overrides are expected to use the same (nested) shape as the TOML config file, for example:

overrides := map[string]any{
  "output": map[string]any{"format": "json"},
  "rules": map[string]any{"include": []any{"tally/*"}},
}

Precedence:

- editorFirst: defaults → filesystem config → env → overrides - filesystemFirst: defaults → overrides → filesystem config → env - editorOnly: defaults → env → overrides (filesystem discovery skipped)

type ConfigurationPreference

type ConfigurationPreference string

ConfigurationPreference controls how editor-provided overrides interact with filesystem config discovery.

This is primarily used by editor integrations (LSP) to decide whether VS Code settings or `.tally.toml` / `tally.toml` should take precedence.

const (
	ConfigurationPreferenceEditorFirst     ConfigurationPreference = "editorFirst"
	ConfigurationPreferenceFilesystemFirst ConfigurationPreference = "filesystemFirst"
	ConfigurationPreferenceEditorOnly      ConfigurationPreference = "editorOnly"
)

type ExcludeConfig

type ExcludeConfig struct {
	// Paths contains glob patterns for files to exclude.
	Paths []string `json:"paths,omitempty" koanf:"paths"`
}

ExcludeConfig defines file exclusion patterns for a rule.

type FileValidationConfig added in v0.13.0

type FileValidationConfig struct {
	// MaxFileSize is the maximum file size in bytes (0 = unlimited).
	MaxFileSize int64 `json:"max-file-size,omitempty" koanf:"max-file-size"`
}

FileValidationConfig configures pre-parse file validation checks.

Example TOML configuration:

[file-validation]
max-file-size = 102400

type FixMode

type FixMode string

FixMode controls when auto-fixes are applied for a rule.

const (
	// FixModeNever disables fixes even with --fix.
	FixModeNever FixMode = "never"

	// FixModeExplicit requires --fix-rule to apply.
	FixModeExplicit FixMode = "explicit"

	// FixModeAlways applies with --fix when safety threshold is met (default).
	FixModeAlways FixMode = "always"

	// FixModeUnsafeOnly requires --fix-unsafe to apply.
	FixModeUnsafeOnly FixMode = "unsafe-only"
)

type InlineDirectivesConfig

type InlineDirectivesConfig struct {
	// Enabled controls whether inline directives are processed.
	Enabled bool `json:"enabled,omitempty" koanf:"enabled"`

	// WarnUnused reports warnings for directives that don't suppress any violations.
	WarnUnused bool `json:"warn-unused,omitempty" koanf:"warn-unused"`

	// ValidateRules reports warnings for unknown rule codes in directives.
	ValidateRules bool `json:"validate-rules,omitempty" koanf:"validate-rules"`

	// RequireReason reports warnings for directives without a reason= explanation.
	RequireReason bool `json:"require-reason,omitempty" koanf:"require-reason"`
}

InlineDirectivesConfig controls inline suppression directives. Supports # tally ignore=..., # hadolint ignore=..., and # check=skip=...

Example TOML configuration:

[inline-directives]
enabled = true
warn-unused = false
validate-rules = true
require-reason = false

type OutputConfig

type OutputConfig struct {
	// Format specifies the output format.
	Format string `json:"format,omitempty" koanf:"format"`

	// Path specifies where to write output.
	Path string `json:"path,omitempty" koanf:"path"`

	// ShowSource enables source code snippets in text output.
	ShowSource bool `json:"show-source,omitempty" koanf:"show-source"`

	// FailLevel sets the minimum severity level that causes a non-zero exit code.
	FailLevel string `json:"fail-level,omitempty" koanf:"fail-level"`
}

OutputConfig configures output formatting and behavior.

type RuleConfig

type RuleConfig struct {
	// Severity overrides the rule's default severity.
	// Use "off" to disable the rule.
	Severity string `json:"severity,omitempty" koanf:"severity"`

	// Fix controls when auto-fixes are applied for this rule.
	// Values: never, explicit, always (default), unsafe-only.
	Fix FixMode `json:"fix,omitempty" koanf:"fix"`

	// Exclude contains path patterns where this rule should not run.
	Exclude ExcludeConfig `json:"exclude" koanf:"exclude"`

	// Options contains rule-specific configuration options.
	Options map[string]any `json:"-" koanf:",remain"`
}

RuleConfig represents per-rule configuration. Can be specified in TOML as:

[rules.tally.max-lines]
severity = "warning"
fix = "always"
# Rule-specific options are flattened at this level
max = 100
skip-blank-lines = true

type RulesConfig

type RulesConfig struct {
	// Include explicitly enables rules.
	Include []string `json:"include,omitempty" koanf:"include"`

	// Exclude explicitly disables rules.
	Exclude []string `json:"exclude,omitempty" koanf:"exclude"`

	// Tally contains configuration for tally/* rules.
	Tally map[string]RuleConfig `json:"tally,omitempty" koanf:"tally"`

	// Buildkit contains configuration for buildkit/* rules.
	Buildkit map[string]RuleConfig `json:"buildkit,omitempty" koanf:"buildkit"`

	// Hadolint contains configuration for hadolint/* rules.
	Hadolint map[string]RuleConfig `json:"hadolint,omitempty" koanf:"hadolint"`

	// Shellcheck contains configuration for shellcheck/* rules.
	Shellcheck map[string]RuleConfig `json:"shellcheck,omitempty" koanf:"shellcheck"`
}

RulesConfig contains rule selection and per-rule configuration.

Example TOML (Ruff-style selection):

[rules]
include = ["buildkit/*"]                    # Enable all buildkit rules
exclude = ["buildkit/MaintainerDeprecated"] # Disable specific rules

[rules.tally.max-lines]
severity = "warning"
max = 100

[rules.hadolint.DL3026]
severity = "warning"
trusted-registries = ["docker.io", "gcr.io"]

func (*RulesConfig) Get

func (rc *RulesConfig) Get(ruleCode string) *RuleConfig

Get returns the configuration for a specific rule. Returns nil if no configuration exists for the rule. ruleCode should be namespaced (e.g., "buildkit/StageNameCasing").

func (*RulesConfig) GetExcludePaths

func (rc *RulesConfig) GetExcludePaths(ruleCode string) []string

GetExcludePaths returns the exclusion patterns for a rule.

func (*RulesConfig) GetFixMode

func (rc *RulesConfig) GetFixMode(ruleCode string) FixMode

GetFixMode returns the fix mode for a rule. Returns FixModeAlways (default) if no override is configured.

func (*RulesConfig) GetOptions

func (rc *RulesConfig) GetOptions(ruleCode string) map[string]any

GetOptions returns rule-specific options. Returns nil if no options are configured. Returns a shallow copy to prevent mutation of internal state.

func (*RulesConfig) GetSeverity

func (rc *RulesConfig) GetSeverity(ruleCode string) string

GetSeverity returns the severity override for a rule. Returns empty string if no override is configured.

func (*RulesConfig) IsEnabled

func (rc *RulesConfig) IsEnabled(ruleCode string) *bool

IsEnabled checks if a rule is enabled based on Include/Exclude patterns. Returns nil if no configuration specifies enabled/disabled (use rule default). Include takes precedence over Exclude (Ruff-style semantics).

func (*RulesConfig) Set

func (rc *RulesConfig) Set(ruleCode string, cfg RuleConfig) bool

Set stores configuration for a rule. Creates the namespace map if nil. Returns false if the namespace is unknown.

type SlowChecksConfig

type SlowChecksConfig struct {
	// Mode controls when slow checks run: auto (CI detection), on, off.
	Mode string `json:"mode,omitempty" koanf:"mode"`

	// FailFast skips async checks when fast checks already produce SeverityError violations.
	FailFast bool `json:"fail-fast,omitempty" koanf:"fail-fast"`

	// Timeout is the wall-clock budget for all async checks per invocation.
	Timeout string `json:"timeout,omitempty" koanf:"timeout"`
}

SlowChecksConfig configures async checks that require potentially slow I/O (registry access, network, filesystem).

Example TOML configuration:

[slow-checks]
mode = "auto"
fail-fast = true
timeout = "20s"

Jump to

Keyboard shortcuts

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