Documentation
¶
Index ¶
- func DefaultDataDir(appName string) (string, error)
- type ConfigFile
- func (c *ConfigFile[T]) AppName() string
- func (c *ConfigFile[T]) Content() ([]byte, error)
- func (c *ConfigFile[T]) Create(data T) error
- func (c *ConfigFile[T]) Load() (T, error)
- func (c *ConfigFile[T]) LoadOrCreate(defaultData T) (T, error)
- func (c *ConfigFile[T]) Path() string
- func (c *ConfigFile[T]) Save(data T) error
- func (c *ConfigFile[T]) SetPath(path string) error
- type Validatable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultDataDir ¶ added in v0.2.0
DefaultDataDir returns the application's conventional data directory. XDG_DATA_HOME takes precedence when set and must contain an absolute path.
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. It may be used concurrently after its path has been configured.
func NewJSONConfigFile ¶
func NewJSONConfigFile[T Validatable](appName, fileName string) (*ConfigFile[T], error)
NewJSONConfigFile creates a typed JSON configuration file inside the user's configuration directory.
func NewYAMLConfigFile ¶
func NewYAMLConfigFile[T Validatable](appName, fileName string) (*ConfigFile[T], error)
NewYAMLConfigFile creates a typed YAML configuration file inside the user's configuration directory.
func (*ConfigFile[T]) AppName ¶ added in v0.3.0
func (c *ConfigFile[T]) AppName() string
AppName returns the application name associated with the configuration.
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]) Create ¶ added in v0.2.0
func (c *ConfigFile[T]) Create(data T) error
Create validates and writes a new configuration file. It returns an error wrapping os.ErrExist when the file already exists.
func (*ConfigFile[T]) Load ¶ added in v0.2.0
func (c *ConfigFile[T]) Load() (T, error)
Load reads and validates the configuration from disk.
func (*ConfigFile[T]) LoadOrCreate ¶ added in v0.2.0
func (c *ConfigFile[T]) LoadOrCreate(defaultData T) (T, error)
LoadOrCreate loads an existing configuration or saves and returns defaultData when the file does not exist.
func (*ConfigFile[T]) Path ¶
func (c *ConfigFile[T]) Path() string
Path constructs and returns the full path to the configuration file. It combines the base directory, application name, and file name.
func (*ConfigFile[T]) Save ¶ added in v0.2.0
func (c *ConfigFile[T]) Save(data T) error
Save validates and writes the configuration to disk, replacing an existing file atomically.
func (*ConfigFile[T]) SetPath ¶ added in v0.3.0
func (c *ConfigFile[T]) SetPath(path string) error
SetPath overrides the conventional configuration file path. It must not be called concurrently with any other ConfigFile method.
type Validatable ¶
type Validatable interface {
Validate() error
}
Validatable is implemented by configuration types that can perform their own validation after being loaded from disk.