Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConfigFile ¶
type ConfigFile[T Validatable] struct { // contains filtered or unexported fields }
ConfigFile wraps the metadata and helpers required to manage one application-specific configuration file.
func NewDefaultConfigFile ¶
func NewDefaultConfigFile[T Validatable](options ...ConfigFileOption[T]) (*ConfigFile[T], error)
NewDefaultConfigFile builds a YAML configuration backed by the user's configuration directory (falling back to the OS temp dir when unavailable).
func NewJSONConfigFile ¶
func NewJSONConfigFile[T Validatable](options ...ConfigFileOption[T]) (*ConfigFile[T], error)
NewJSONConfigFile constructs a ConfigFile that persists data as JSON inside the default configuration directory. The base path can be overridden with WithPath.
func NewYAMLConfigFile ¶
func NewYAMLConfigFile[T Validatable](options ...ConfigFileOption[T]) (*ConfigFile[T], error)
NewYAMLConfigFile constructs a ConfigFile that persists data as YAML inside the default configuration directory. The base path can be overridden with WithPath.
func (*ConfigFile[T]) Content ¶
func (c *ConfigFile[T]) Content() ([]byte, error)
Content reads and returns the content of the configuration file. It returns an error if the file cannot be read.
func (*ConfigFile[T]) Data ¶
func (c *ConfigFile[T]) Data() T
Data returns the in-memory copy of the configuration that was last loaded from disk or passed to Init/SoftInit.
func (*ConfigFile[T]) DirPath ¶
func (c *ConfigFile[T]) DirPath() string
DirPath returns the full directory path where the application's configuration files are stored. It combines the configuration directory and the application's name.
func (*ConfigFile[T]) Init ¶
func (c *ConfigFile[T]) Init(data T) error
Init initializes the configuration by ensuring that the directory and file exist, and by writing the initial configuration data to the file.
func (*ConfigFile[T]) Path ¶
func (c *ConfigFile[T]) Path() string
Path constructs and returns the full path to the configuration file. It combines the directory path and the file name.
func (*ConfigFile[T]) Reload ¶
func (c *ConfigFile[T]) Reload() error
Reload refreshes the cached configuration by pulling the latest content from disk using the configured file manager.
func (*ConfigFile[T]) SoftInit ¶
func (c *ConfigFile[T]) SoftInit() error
SoftInit attempts to initialize the configuration by loading existing data or creating new configuration. It reads the configuration if the file exists or initializes it if it does not.
type ConfigFileOption ¶
type ConfigFileOption[T Validatable] func(*ConfigFile[T])
ConfigFileOption customizes the behavior of a ConfigFile during construction.
func WithAppName ¶
func WithAppName[T Validatable](appName string) ConfigFileOption[T]
WithAppName overrides the application identifier used to locate the configuration directory and derive environment-specific overrides.
func WithDefault ¶
func WithDefault[T Validatable](defaultData T) ConfigFileOption[T]
WithDefault seeds the ConfigFile with a default value that will be written to disk during initialization when no configuration file exists yet.
func WithFilename ¶
func WithFilename[T Validatable](fileName string) ConfigFileOption[T]
WithFilename overrides the default file name while ensuring it uses the extension that matches the underlying file manager.
func WithPath ¶
func WithPath[T Validatable](path string) ConfigFileOption[T]
WithPath overrides the directory where configuration files are stored. When the provided path is empty the default user configuration directory (or the system temp directory as fallback) is used.
type FileManager ¶
type FileManager[T Validatable] interface { // LoadDataFromFile hydrates the provided struct pointer with the contents // read from filePath. LoadDataFromFile(filePath string, data *T) error // WriteDataToFile serializes the given value and stores it at filePath. WriteDataToFile(filePath string, data T) error // Extension returns the preferred file extension (including the leading // dot) used for files handled by this manager. Extension() string }
FileManager abstracts how configuration data is loaded from and persisted to disk for a given encoding format.
type JSONFileManager ¶
type JSONFileManager[T any] struct{}
JSONFileManager implements FileManager for configurations encoded as JSON.
func NewJSONFileManager ¶
func NewJSONFileManager[T any]() *JSONFileManager[T]
NewJSONFileManager builds a FileManager that marshals and unmarshals JSON payloads using the standard library.
func (*JSONFileManager[T]) Extension ¶
func (b *JSONFileManager[T]) Extension() string
Extension returns the canonical JSON file extension.
func (*JSONFileManager[T]) LoadDataFromFile ¶
func (b *JSONFileManager[T]) LoadDataFromFile(filePath string, data *T) error
LoadDataFromFile reads the JSON file, unmarshals it into the provided value, and returns an error if the file cannot be read or parsed.
func (*JSONFileManager[T]) WriteDataToFile ¶
func (b *JSONFileManager[T]) WriteDataToFile(filePath string, data T) error
WriteDataToFile serializes the value as JSON and persists it to disk.
type Validatable ¶
type Validatable interface {
Validate() error
}
Validatable is implemented by configuration types that can perform their own validation after being loaded from disk.
type YAMLFileManager ¶
type YAMLFileManager[T any] struct{}
YAMLFileManager implements FileManager for configurations encoded as YAML.
func NewYAMLFileManager ¶
func NewYAMLFileManager[T any]() *YAMLFileManager[T]
NewYAMLFileManager builds a FileManager that marshals and unmarshals YAML payloads via gopkg.in/yaml.v3.
func (*YAMLFileManager[T]) Extension ¶
func (b *YAMLFileManager[T]) Extension() string
Extension returns the canonical YAML file extension.
func (*YAMLFileManager[T]) LoadDataFromFile ¶
func (b *YAMLFileManager[T]) LoadDataFromFile(filePath string, data *T) error
LoadDataFromFile reads the YAML file, unmarshals it into the provided value, and returns an error if the file cannot be read or parsed.
func (*YAMLFileManager[T]) WriteDataToFile ¶
func (b *YAMLFileManager[T]) WriteDataToFile(filePath string, data T) error
WriteDataToFile serializes the value as YAML and persists it to disk.