config

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package config is the configuration manager for elf.

Index

Constants

View Source
const (
	ElfEnvPrefix          string = "ELF"
	DefaultConfigFileBase string = "elf"
	DefaultConfigExt      string = "toml"
)

Variables

View Source
var (
	ErrTokenNotSet      = errors.New("advent token not configured")
	ErrTokenPlaceholder = errors.New("advent token is placeholder value")
	ErrDirectoryMissing = errors.New("directory does not exist")
)

Validation errors.

Functions

func GenerateDefaultConfig

func GenerateDefaultConfig() string

GenerateDefaultConfig returns a default TOML configuration template string.

func MaskToken

func MaskToken(token string) string

MaskToken returns a masked version of the token for display. Shows first 4 and last 4 characters with "..." in between. Returns empty string if token is empty. Returns "****" if token is too short to mask properly.

func WithFile

func WithFile(f string) func(*Config)

WithFile sets the configuration file and type.

If the file is empty, the default file name and type are used.

func WithFs

func WithFs(fs afero.Fs) func(*Config)

WithFs sets the file system.

If the file system is nil, a new OS file system is used.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

func NewConfig

func NewConfig(options ...func(*Config)) (Config, error)

func (Config) GetAllSettings

func (c Config) GetAllSettings() map[string]any

GetAllSettings returns all configuration settings as a map.

func (Config) GetBaseDir

func (c Config) GetBaseDir() string

func (Config) GetCacheDir

func (c Config) GetCacheDir() string

func (Config) GetConfigDir

func (c Config) GetConfigDir() string

func (Config) GetConfigFileUsed

func (c Config) GetConfigFileUsed() string

GetConfigFileUsed returns the configuration file used.

If no configuration file is loaded, an empty string is returned. Failure to read a configuration file does not cause an error and will still result in an empty string.

func (Config) GetFs

func (c Config) GetFs() afero.Fs

func (Config) GetInputFilename

func (c Config) GetInputFilename() string

func (Config) GetLanguage

func (c Config) GetLanguage() string

GetLanguage returns the configured default implementation language.

If no language is configured, an empty string is returned.

func (Config) GetLogger

func (c Config) GetLogger() *slog.Logger

func (Config) GetToken

func (c Config) GetToken() string

GetToken returns the authentication token for downloading exercises.

func (*Config) SafeWriteConfig

func (c *Config) SafeWriteConfig(path string) error

SafeWriteConfig writes the configuration only if the file doesn't exist. Returns an error if the file already exists.

func (*Config) SetToken

func (c *Config) SetToken(token string)

SetToken updates the advent token value in the configuration.

func (*Config) SetValue

func (c *Config) SetValue(key Key, value any)

SetValue sets a configuration value by key.

func (Config) Validate

func (c Config) Validate() []error

Validate performs full validation of the configuration. Returns all validation errors found.

func (Config) ValidateDirectories

func (c Config) ValidateDirectories() []error

ValidateDirectories checks if configured directories exist. Returns a slice of errors for each missing directory.

func (Config) ValidateToken

func (c Config) ValidateToken() error

ValidateToken checks if the advent token is properly configured. Returns an error if the token is empty or is the default placeholder.

func (*Config) WriteConfig

func (c *Config) WriteConfig(path string) error

WriteConfig writes the current configuration to the specified path. If path is empty, it writes to the default config directory. Creates parent directories as needed.

type ConfigurationReader

type ConfigurationReader interface {
	// GetConfigFileUsed returns the configuration file used.
	GetConfigFileUsed() string
}

ConfigurationReader is an interface that defines methods for reading configuration.

type ConfigurationValidator

type ConfigurationValidator interface {
	// ValidateToken checks if the advent token is properly configured.
	ValidateToken() error

	// ValidateDirectories checks if configured directories exist.
	ValidateDirectories() []error

	// Validate performs full validation of the configuration.
	Validate() []error
}

ConfigurationValidator is an interface for validating configuration.

type ConfigurationWriter

type ConfigurationWriter interface {
	// WriteConfig writes the configuration to the specified path.
	WriteConfig(path string) error

	// SafeWriteConfig writes the configuration only if the file doesn't exist.
	SafeWriteConfig(path string) error

	// SetToken updates the advent token value.
	SetToken(token string)

	// SetValue sets a configuration value by key.
	SetValue(key Key, value any)

	// GetAllSettings returns all configuration settings as a map.
	GetAllSettings() map[string]any
}

ConfigurationWriter is an interface for writing configuration files.

type DownloadConfiguration

type DownloadConfiguration interface {
	ExerciseConfiguration

	// GetCacheDir returns the directory where downloaded exercises are cached.
	GetCacheDir() string

	// GetConfigDir returns the configuration directory.
	GetConfigDir() string

	// GetInputFilename returns the input filename.
	GetInputFilename() string

	// GetToken returns the authentication token for downloading exercises.
	GetToken() string
}

DownloadConfiguration is an interface that extends the ExerciseConfiguration interface. It represents the configuration for downloading exercises.

type ExerciseConfiguration

type ExerciseConfiguration interface {
	// GetBaseDir returns the base directory.
	GetBaseDir() string

	// GetFs returns the file system.
	GetFs() afero.Fs

	// GetLanguage returns the language.
	GetLanguage() string

	// GetLogger returns the logger.
	GetLogger() *slog.Logger
}

ExerciseConfiguration represents the interface for exercise configuration.

type Key

type Key string

Key is a type for configuration keys.

const (
	LanguageKey  Key = "language"   // Configuration key for the the default implementation language.
	ConfigDirKey Key = "config-dir" // Configuration key for application configuration files.
	CacheDirKey  Key = "cache-dir"  // Configuration key for cached application data.
	InputFileKey Key = "input-file" // InputFileKey is the configuration key for the default input file name.

	AdventTokenKey Key = "advent.token" // Configuration key for the Advent of Code auth token.
	AdventUserKey  Key = "advent.user"  // Configuration key for the Advent of Code user name.
	AdventDirKey   Key = "advent.dir"   // Configuration key for the Advent of Code exercise directory.
)

func (Key) String

func (k Key) String() string

Jump to

Keyboard shortcuts

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