config

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package config provides configuration management for the PI scanner. It supports loading configuration from YAML files, environment variables, and command-line flags, with proper validation and default values.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultFileTypes

func DefaultFileTypes() []string

DefaultFileTypes returns the default file types to scan

func GenerateExampleConfig

func GenerateExampleConfig(path string) error

GenerateExampleConfig writes an example configuration file

Example
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/MacAttak/pi-scanner/pkg/config"
)

func main() {
	// Generate an example configuration file
	if err := config.GenerateExampleConfig("example-config.yaml"); err != nil {
		log.Fatal(err)
	}

	// Load and display the generated config
	cfg, err := config.LoadConfig("example-config.yaml")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Generated config version: %s\n", cfg.Version)
	fmt.Printf("Scanner workers: %d\n", cfg.Scanner.Workers)

	// Clean up
	os.Remove("example-config.yaml")

}
Output:

Generated config version: 1.0
Scanner workers: 8

func SaveConfig

func SaveConfig(config *Config, path string) error

SaveConfig saves configuration to a YAML file

Types

type CoOccurrenceConfig

type CoOccurrenceConfig struct {
	Enabled         bool    `yaml:"enabled"`
	ProximityWindow int     `yaml:"proximity_window"`
	MinOccurrences  int     `yaml:"min_occurrences"`
	ScoreBoost      float64 `yaml:"score_boost"`
}

CoOccurrenceConfig defines co-occurrence settings

type Config

type Config struct {
	Version string        `yaml:"version"`
	Scanner ScannerConfig `yaml:"scanner"`
	Risk    RiskConfig    `yaml:"risk"`
	Report  ReportConfig  `yaml:"report"`
	Github  GithubConfig  `yaml:"github"`
	Logging LoggingConfig `yaml:"logging"`
}

Config represents the complete scanner configuration

func ConfigFromEnvironment

func ConfigFromEnvironment() *Config

ConfigFromEnvironment loads configuration overrides from environment variables

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns the default configuration

func ExampleConfig

func ExampleConfig() (*Config, error)

ExampleConfig generates an example configuration file

func LoadConfig

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

LoadConfig loads configuration from a YAML file

Example
package main

import (
	"fmt"

	"github.com/MacAttak/pi-scanner/pkg/config"
)

func main() {
	// Load configuration from file
	cfg, err := config.LoadConfig("config.yaml")
	if err != nil {
		// Fall back to defaults if config file not found
		cfg = config.DefaultConfig()
	}

	fmt.Printf("Workers: %d\n", cfg.Scanner.Workers)
	fmt.Printf("Risk Threshold (Critical): %.1f\n", cfg.Risk.Thresholds.Critical)

}
Output:

Workers: 4
Risk Threshold (Critical): 0.8

func LoadConfigWithDefaults

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

LoadConfigWithDefaults loads config or returns default if file doesn't exist

func MergeConfig

func MergeConfig(base, override *Config) *Config

MergeConfig merges two configurations, with the second taking precedence

Example
package main

import (
	"fmt"

	"github.com/MacAttak/pi-scanner/pkg/config"
)

func main() {
	// Start with default configuration
	base := config.DefaultConfig()

	// Create override configuration
	override := &config.Config{
		Scanner: config.ScannerConfig{
			Workers: 8,
		},
	}

	// Merge configurations
	merged := config.MergeConfig(base, override)

	fmt.Printf("Workers: %d\n", merged.Scanner.Workers)
	fmt.Printf("File Types Count: %d\n", len(merged.Scanner.FileTypes))

}
Output:

Workers: 8
File Types Count: 34

func (*Config) Validate

func (c *Config) Validate() error

Validate checks if the configuration is valid

Example
package main

import (
	"fmt"

	"github.com/MacAttak/pi-scanner/pkg/config"
)

func main() {
	cfg := config.DefaultConfig()

	// Modify some values
	cfg.Scanner.Workers = 16
	cfg.Risk.Thresholds.Critical = 0.9

	// Validate the configuration
	if err := cfg.Validate(); err != nil {
		fmt.Printf("Configuration error: %v\n", err)
	} else {
		fmt.Println("Configuration is valid")
	}

}
Output:

Configuration is valid

type GithubConfig

type GithubConfig struct {
	Token         string        `yaml:"token,omitempty"`
	RateLimit     int           `yaml:"rate_limit"`
	CloneTimeout  time.Duration `yaml:"clone_timeout"`
	CloneDepth    int           `yaml:"clone_depth"`
	TempDirectory string        `yaml:"temp_directory"`
}

GithubConfig contains GitHub integration settings

type LoggingConfig

type LoggingConfig struct {
	Level      string `yaml:"level"`
	Format     string `yaml:"format"`
	OutputFile string `yaml:"output_file,omitempty"`
	MaxSize    int    `yaml:"max_size"`
	MaxBackups int    `yaml:"max_backups"`
	MaxAge     int    `yaml:"max_age"`
}

LoggingConfig contains logging settings

type ReportConfig

type ReportConfig struct {
	Formats           []string    `yaml:"formats"`
	OutputDirectory   string      `yaml:"output_directory"`
	IncludeMasked     bool        `yaml:"include_masked"`
	IncludeContext    bool        `yaml:"include_context"`
	TemplateDirectory string      `yaml:"template_directory,omitempty"`
	SARIF             SARIFConfig `yaml:"sarif"`
}

ReportConfig contains report generation settings

type RiskConfig

type RiskConfig struct {
	Thresholds   RiskThresholds     `yaml:"thresholds"`
	Multipliers  RiskMultipliers    `yaml:"multipliers"`
	CoOccurrence CoOccurrenceConfig `yaml:"co_occurrence"`
}

RiskConfig contains risk scoring settings

type RiskMultipliers

type RiskMultipliers struct {
	Production  float64 `yaml:"production"`
	Staging     float64 `yaml:"staging"`
	Development float64 `yaml:"development"`
	Test        float64 `yaml:"test"`
}

RiskMultipliers defines environment multipliers

type RiskThresholds

type RiskThresholds struct {
	Critical float64 `yaml:"critical"`
	High     float64 `yaml:"high"`
	Medium   float64 `yaml:"medium"`
	Low      float64 `yaml:"low"`
}

RiskThresholds defines risk level thresholds

type SARIFConfig

type SARIFConfig struct {
	ToolName    string `yaml:"tool_name"`
	ToolVersion string `yaml:"tool_version"`
	InfoURI     string `yaml:"info_uri"`
}

SARIFConfig contains SARIF-specific settings

type ScannerConfig

type ScannerConfig struct {
	Workers           int             `yaml:"workers"`
	FileTypes         []string        `yaml:"file_types"`
	ExcludePaths      []string        `yaml:"exclude_paths"`
	MaxFileSize       int64           `yaml:"max_file_size"`
	Timeout           time.Duration   `yaml:"timeout"`
	Validators        ValidatorConfig `yaml:"validators"`
	ProximityDistance int             `yaml:"proximity_distance"`
}

ScannerConfig contains scanner-specific settings

type ValidatorConfig

type ValidatorConfig struct {
	TFN        ValidatorSettings `yaml:"tfn"`
	Medicare   ValidatorSettings `yaml:"medicare"`
	ABN        ValidatorSettings `yaml:"abn"`
	BSB        ValidatorSettings `yaml:"bsb"`
	CreditCard ValidatorSettings `yaml:"credit_card"`
	Email      ValidatorSettings `yaml:"email"`
	Phone      ValidatorSettings `yaml:"phone"`
}

ValidatorConfig contains validator settings

type ValidatorSettings

type ValidatorSettings struct {
	Enabled       bool    `yaml:"enabled"`
	StrictMode    bool    `yaml:"strict_mode"`
	MinConfidence float64 `yaml:"min_confidence"`
	CustomPattern string  `yaml:"custom_pattern,omitempty"`
}

ValidatorSettings contains individual validator settings

Jump to

Keyboard shortcuts

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