Documentation
¶
Overview ¶
Package config is the configuration manager for elf.
Index ¶
- Constants
- Variables
- func GenerateDefaultConfig() string
- func MaskToken(token string) string
- func WithFile(f string) func(*Config)
- func WithFs(fs afero.Fs) func(*Config)
- type Config
- func (c Config) GetAllSettings() map[string]any
- func (c Config) GetBaseDir() string
- func (c Config) GetCacheDir() string
- func (c Config) GetConfigDir() string
- func (c Config) GetConfigFileUsed() string
- func (c Config) GetFs() afero.Fs
- func (c Config) GetInputFilename() string
- func (c Config) GetLanguage() string
- func (c Config) GetLogger() *slog.Logger
- func (c Config) GetToken() string
- func (c *Config) SafeWriteConfig(path string) error
- func (c *Config) SetToken(token string)
- func (c *Config) SetValue(key Key, value any)
- func (c Config) Validate() []error
- func (c Config) ValidateDirectories() []error
- func (c Config) ValidateToken() error
- func (c *Config) WriteConfig(path string) error
- type ConfigurationReader
- type ConfigurationValidator
- type ConfigurationWriter
- type DownloadConfiguration
- type ExerciseConfiguration
- type Key
Constants ¶
const ( ElfEnvPrefix string = "ELF" DefaultConfigFileBase string = "elf" DefaultConfigExt string = "toml" )
Variables ¶
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 ¶
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.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
func (Config) GetAllSettings ¶
GetAllSettings returns all configuration settings as a map.
func (Config) GetBaseDir ¶
func (Config) GetCacheDir ¶
func (Config) GetConfigDir ¶
func (Config) GetConfigFileUsed ¶
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) GetInputFilename ¶
func (Config) GetLanguage ¶
GetLanguage returns the configured default implementation language.
If no language is configured, an empty string is returned.
func (*Config) SafeWriteConfig ¶
SafeWriteConfig writes the configuration only if the file doesn't exist. Returns an error if the file already exists.
func (Config) Validate ¶
Validate performs full validation of the configuration. Returns all validation errors found.
func (Config) ValidateDirectories ¶
ValidateDirectories checks if configured directories exist. Returns a slice of errors for each missing directory.
func (Config) ValidateToken ¶
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 ¶
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. )