config

package
v1.3.121 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package config provides configuration management for KTN linter rules. It allows enabling/disabling rules, setting thresholds, and excluding files.

Package config provides configuration management for KTN linter rules.

Index

Constants

View Source
const (
	// DefaultConfigFileName is the default configuration file name.
	DefaultConfigFileName string = ".ktn-linter.yaml"
	// AlternateConfigFileName is an alternate configuration file name.
	AlternateConfigFileName string = ".ktn-linter.yml"
)

Configuration constants.

Variables

View Source
var (

	// ErrUnsupportedVersion indicates an unsupported config version.
	ErrUnsupportedVersion error = errors.New("unsupported config version")
	// ErrNegativeThreshold indicates a negative threshold value.
	ErrNegativeThreshold error = errors.New("threshold must be non-negative")
	// ErrEmptyExclusionPattern indicates an empty exclusion pattern.
	ErrEmptyExclusionPattern error = errors.New("empty exclusion pattern")
)

Package-level variables.

Functions

func LoadAndSet

func LoadAndSet(path string) error

LoadAndSet loads configuration and sets it as the global config.

Params:

  • path: File path to load configuration from

Returns:

  • error: Error if loading fails

func Reset

func Reset()

Reset resets the global configuration to default.

Returns: none

func SaveToFile

func SaveToFile(cfg *Config, path string) error

SaveToFile saves configuration to a file.

Params:

  • cfg: Configuration to save
  • path: File path to save configuration to

Returns:

  • error: Error if saving fails

func Set

func Set(cfg *Config)

Set sets the global configuration instance.

Params:

  • cfg: configuration to set

Returns: none

Types

type Config

type Config struct {
	// Version of the configuration format
	Version int `yaml:"version"`

	// Exclude contains global file exclusion patterns (apply to all rules)
	Exclude []string `yaml:"exclude,omitempty"`

	// Rules contains per-rule configuration
	Rules map[string]*RuleConfig `yaml:"rules,omitempty"`

	// ForceAllRulesOnTests runs all rules on test files (default: false)
	// By default, only KTN-TEST-* rules analyze *_test.go files.
	// Set to true to run all rules on test files (useful for debugging).
	ForceAllRulesOnTests bool `yaml:"force_all_rules_on_tests,omitempty"`

	// StrictMode disables all built-in exclusions (test files, testdata, generated).
	// When true, ALL files are analyzed by ALL rules without exception.
	StrictMode bool `yaml:"strict_mode,omitempty"`

	// AnalyzerTestFixturePath is the path pattern for analyzer test fixtures.
	// Files matching this pattern are never skipped by ShouldSkipTestdata.
	// Default: "testdata/src/"
	AnalyzerTestFixturePath string `yaml:"analyzer_test_fixture_path,omitempty"`

	// SkipGenerated skips files with generation markers (DO NOT EDIT, etc.)
	// Default: true
	SkipGenerated *bool `yaml:"skip_generated,omitempty"`

	// ConfigSerializationTags defines recognized serialization tags for KTN-STRUCT-JSONTAG.
	// Default: json, xml, yaml, toml, tlv, protobuf, flatbuffer
	ConfigSerializationTags []string `yaml:"serialization_tags,omitempty"`

	// Verbose enables verbose message output with examples
	Verbose bool `yaml:"-"`

	// OnlyRule is set when --only-rule is used; bypasses global exclusions for this rule
	OnlyRule string `yaml:"-"`
	// contains filtered or unexported fields
}

Config represents the complete linter configuration. It contains global settings and per-rule configurations.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns the default configuration.

Returns:

  • *Config: default configuration instance with version 1

func Get

func Get() *Config

Get returns the global configuration instance.

Returns:

  • *Config: global configuration instance, never nil

func Load

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

Load loads configuration from a file path. If path is empty, it searches for default config files in the current directory and parent directories.

Params:

  • path: File path to load configuration from (empty for default locations)

Returns:

  • *Config: Loaded configuration
  • error: Error if loading fails

func MustLoad

func MustLoad(path string) *Config

MustLoad loads configuration and panics on error.

Params:

  • path: File path to load configuration from

Returns:

  • *Config: Loaded configuration (panics on error)

func (*Config) GetThreshold

func (c *Config) GetThreshold(ruleCode string, defaultValue int) int

GetThreshold returns the threshold for a rule, or the default if not set.

Params:

  • ruleCode: the rule code to check
  • defaultValue: default value if not configured

Returns:

  • int: the threshold value

func (*Config) IsFileExcluded

func (c *Config) IsFileExcluded(ruleCode, filename string) bool

IsFileExcluded checks if a file should be excluded for a specific rule. KTN-TEST rules ignore ALL global exclusions (they only respect rule-specific exclusions). Rules explicitly targeted with --only-rule also ignore global exclusions.

Params:

  • ruleCode: the rule code to check
  • filename: the file path to check

Returns:

  • bool: true if the file should be excluded

func (*Config) IsFileExcludedGlobally

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

IsFileExcludedGlobally checks if a file is excluded globally (all rules).

Params:

  • filename: the file path to check

Returns:

  • bool: true if the file is globally excluded

func (*Config) IsFunctionExcluded added in v0.104.0

func (c *Config) IsFunctionExcluded(ruleCode, filename, funcName string) bool

IsFunctionExcluded checks if a specific function is excluded for a rule. Uses the exclude_functions patterns in rule configuration. Format: "file_pattern:function_name"

Params:

  • ruleCode: the rule code to check
  • filename: the file path
  • funcName: the function name (for methods: "ReceiverType.MethodName")

Returns:

  • bool: true if the function should be excluded

func (*Config) IsRuleEnabled

func (c *Config) IsRuleEnabled(ruleCode string) bool

IsRuleEnabled checks if a rule is enabled.

Params:

  • ruleCode: the rule code to check

Returns:

  • bool: true if the rule is enabled

func (*Config) Merge

func (c *Config) Merge(other *Config)

Merge merges another config into this one (other takes precedence).

Params:

  • other: configuration to merge

Returns: none

func (*Config) SerializationTags added in v0.91.0

func (c *Config) SerializationTags() []string

SerializationTags returns the configured serialization tags or defaults.

Returns:

  • []string: list of serialization tag prefixes (e.g., "json:", "xml:")

func (*Config) ShouldSkipGenerated added in v0.99.0

func (c *Config) ShouldSkipGenerated() bool

ShouldSkipGenerated checks if generated files should be skipped. Default is true unless explicitly set to false in config.

Returns:

  • bool: true if generated files should be skipped

func (*Config) ShouldSkipTestFile added in v0.95.0

func (c *Config) ShouldSkipTestFile(ruleCode, filename string) bool

ShouldSkipTestFile checks if a rule should skip a test file. Returns true if the file is a test file AND the rule has built-in test exclusion AND the user hasn't disabled it via config.

Params:

  • ruleCode: the rule code to check
  • filename: the file path to check

Returns:

  • bool: true if the rule should skip this test file

func (*Config) ShouldSkipTestdata added in v0.99.0

func (c *Config) ShouldSkipTestdata(ruleCode, filename string) bool

ShouldSkipTestdata checks if a rule should skip a testdata file. Returns true if the file is in testdata AND the rule has built-in testdata exclusion AND the user hasn't disabled it via config.

Params:

  • ruleCode: the rule code to check
  • filename: the file path to check

Returns:

  • bool: true if the rule should skip this testdata file

type RuleConfig

type RuleConfig struct {
	// Enabled indicates whether the rule is active (default: true)
	Enabled *bool `yaml:"enabled,omitempty"`

	// Threshold is a numeric threshold for rules that support it
	Threshold *int `yaml:"threshold,omitempty"`

	// Exclude contains rule-specific file exclusion patterns
	Exclude []string `yaml:"exclude,omitempty"`

	// ExcludeFunctions contains function-level exclusion patterns.
	// Format: "file_pattern:function_name" where:
	//   - file_pattern: glob pattern for file matching (e.g., "**/logging/*.go")
	//   - function_name: exact function name or pattern with * wildcard
	// Examples:
	//   - "event.go:WithMeta" - exact file and function
	//   - "**/formatter.go:formatValue" - glob pattern for file
	//   - "**/logging/**:*" - all functions in matching files
	ExcludeFunctions []string `yaml:"exclude_functions,omitempty"`

	// SkipTestFiles indicates if the rule should skip *_test.go files.
	// Some rules have built-in test file exclusions (default: true for those rules).
	// Set to false to force the rule to analyze test files.
	SkipTestFiles *bool `yaml:"skip_test_files,omitempty"`

	// SkipTestdata indicates if the rule should skip testdata directories.
	// Some rules have built-in testdata exclusions.
	// Set to false to force the rule to analyze testdata files.
	SkipTestdata *bool `yaml:"skip_testdata,omitempty"`
}

RuleConfig represents configuration for a single rule. It allows customizing rule behavior per-rule basis.

Jump to

Keyboard shortcuts

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