Documentation
¶
Index ¶
- type Config
- func (c *Config) Delete(key string)
- func (c *Config) EnableBackup()
- func (c *Config) Get(key string, defaultValue any) any
- func (c *Config) GetSecure(key string, defaultValue any) (any, error)
- func (c *Config) InitAES128(secret string) error
- func (c *Config) InitAES256(secret string) error
- func (c *Config) InitBackup(path string) error
- func (c *Config) IsBackupExist() bool
- func (c *Config) IsExist() bool
- func (c *Config) Keys() []string
- func (c *Config) Load() error
- func (c *Config) Merge(updt dictx.Dict)
- func (c *Config) Purge() error
- func (c *Config) Save() error
- func (c *Config) Set(key string, newValue any)
- func (c *Config) SetSecure(key string, val any) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { Buffer dictx.Dict // Holds the current configuration in memory // contains filtered or unexported fields }
Config represents a configuration manager that handles loading, saving, and backing up configuration data.
func New ¶
New creates a new Config instance with the provided file path and default values. Returns an error if the file path is empty.
func (*Config) Delete ¶
Delete removes a key from the configuration buffer if it exists. It supports nested keys using the separator.
func (*Config) EnableBackup ¶
func (c *Config) EnableBackup()
EnableBackup enables automatic backup by creating a backup file at the same path as the config file, with a `.backup` suffix.
func (*Config) Get ¶
Get retrieves a value from the configuration buffer by key. If the key is not found, the default_value is returned.
func (*Config) GetSecure ¶
GetSecure retrieves and decrypts a secure value by key from the configuration. If the key does not exist or decryption fails, it returns the defaultValue. Returns an error if encryption is not configured or the value format is invalid.
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/dictx" "github.com/exonlabs/go-utils/pkg/jconfig" ) func main() { cfg, _ := jconfig.New("config.json", dictx.Dict{}) cfg.InitAES128("thisis128bitkey!!") // Retrieve a non-existing key retrieved, _ := cfg.GetSecure("non_existing_key", "default") fmt.Println(retrieved) }
Output: default
func (*Config) InitAES128 ¶
InitAES128 initializes AES-128 encryption for the configuration using the provided secret key. Returns an error if the secret is invalid or encryption setup fails.
func (*Config) InitAES256 ¶
InitAES256 initializes AES-256 encryption for the configuration using the provided secret key. Returns an error if the secret is invalid or encryption setup fails.
func (*Config) InitBackup ¶
InitBackup sets the backup file path for the configuration. Returns an error if the provided path is empty.
func (*Config) IsBackupExist ¶
IsBackupExist checks whether the backup file exists.
func (*Config) Load ¶
Load reads the configuration from the main file and loads it into memory. If the main config fails to load, attempts to load from a backup file. Also saves the loaded data back to the backup if successful.
func (*Config) Merge ¶
Merge updates a configuration buffer recursively with an update dictionary. It merges keys and values, allowing nested dictionaries to be updated as well.
func (*Config) Purge ¶
Purge clears the configuration buffer and deletes the main and backup files (if they exist).
func (*Config) Save ¶
Save serializes the current buffer to a formatted JSON byte slice, then writes the configuration buffer to both the main file and the backup file (if a backup path is set).
func (*Config) Set ¶
Set adds a new value in the configuration buffer by key. If the key already exists, its value is overwritten.
func (*Config) SetSecure ¶
SetSecure encrypts and stores a secure value by key in the configuration. The key is created if it doesn't exist. Returns an error if encryption is not configured.
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/dictx" "github.com/exonlabs/go-utils/pkg/jconfig" ) func main() { cfg, _ := jconfig.New("config.json", dictx.Dict{}) cfg.InitAES128("thisis128bitkey!!") val := map[string]any{"username": "admin", "password": "secret"} cfg.SetSecure("credentials", val) // Retrieve the secure value retrieved, _ := cfg.GetSecure("credentials", nil) fmt.Println(retrieved) }
Output: map[password:secret username:admin]