Documentation
¶
Index ¶
- type FileHandler
- type JConfig
- func (c *JConfig) Delete(key string)
- func (c *JConfig) EnableBackup()
- func (c *JConfig) Get(key string, defaultValue any) any
- func (c *JConfig) GetSecure(key string, defaultValue any) (any, error)
- func (c *JConfig) InitAES128(secret string) error
- func (c *JConfig) InitAES256(secret string) error
- func (c *JConfig) InitBackup(path string) error
- func (c *JConfig) InitCipher(cipher ciphering.Handler) error
- func (c *JConfig) IsBackupExist() bool
- func (c *JConfig) IsExist() bool
- func (c *JConfig) Keys() []string
- func (c *JConfig) Load() error
- func (c *JConfig) Merge(updt dictx.Dict)
- func (c *JConfig) Purge() error
- func (c *JConfig) Save() error
- func (c *JConfig) Set(key string, newValue any)
- func (c *JConfig) SetFileHandler(handler FileHandler)
- func (c *JConfig) SetSecure(key string, val any) error
- type StdFileHandler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FileHandler ¶ added in v0.4.3
type FileHandler interface { // IsExist checks whether the named file exists. IsExist(name string) bool // Read reads the named file and returns the contents. Read(name string) ([]byte, error) // Write writes data to the named file, creating it if necessary. Write(name string, data []byte, perm os.FileMode) error // Remove delete the named file from system. Remove(name string) error }
type JConfig ¶ added in v0.4.2
type JConfig struct { Buffer dictx.Dict // Holds the current configuration in memory // contains filtered or unexported fields }
JConfig represents a configuration manager that handles loading, saving, and backing up configuration data.
func New ¶
New creates a new JConfig instance with the provided file path and default values. Returns an error if the file path is empty.
func (*JConfig) Delete ¶ added in v0.4.2
Delete removes a key from the configuration buffer if it exists. It supports nested keys using the separator.
func (*JConfig) EnableBackup ¶ added in v0.4.2
func (c *JConfig) EnableBackup()
EnableBackup enables automatic backup by creating a backup file at the same path as the config file, with a `.backup` suffix.
func (*JConfig) Get ¶ added in v0.4.2
Get retrieves a value from the configuration buffer by key. If the key is not found, the default_value is returned.
func (*JConfig) GetSecure ¶ added in v0.4.2
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 (*JConfig) InitAES128 ¶ added in v0.4.2
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 (*JConfig) InitAES256 ¶ added in v0.4.2
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 (*JConfig) InitBackup ¶ added in v0.4.2
InitBackup sets the backup file path for the configuration. Returns an error if the provided path is empty.
func (*JConfig) InitCipher ¶ added in v0.4.3
InitCipher initializes ciphering handler
func (*JConfig) IsBackupExist ¶ added in v0.4.2
IsBackupExist checks whether the backup file exists.
func (*JConfig) IsExist ¶ added in v0.4.2
IsExist checks whether the main configuration file exists.
func (*JConfig) Load ¶ added in v0.4.2
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 (*JConfig) Merge ¶ added in v0.4.2
Merge updates a configuration buffer recursively with an update dictionary. It merges keys and values, allowing nested dictionaries to be updated as well.
func (*JConfig) Purge ¶ added in v0.4.2
Purge clears the configuration buffer and deletes the main and backup files (if they exist).
func (*JConfig) Save ¶ added in v0.4.2
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 (*JConfig) Set ¶ added in v0.4.2
Set adds a new value in the configuration buffer by key. If the key already exists, its value is overwritten.
func (*JConfig) SetFileHandler ¶ added in v0.4.3
func (c *JConfig) SetFileHandler(handler FileHandler)
SetFileHandler sets a new file handler.
func (*JConfig) SetSecure ¶ added in v0.4.2
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]
type StdFileHandler ¶ added in v0.4.3
type StdFileHandler struct{}
StdFileHandler represents the standard local file access using os pkg.
func NewStdFileHandler ¶ added in v0.4.3
func NewStdFileHandler() *StdFileHandler
NewStdFileHandler creates new standard local file handler.
func (*StdFileHandler) IsExist ¶ added in v0.4.3
func (h *StdFileHandler) IsExist(name string) bool
IsExist checks whether the named file exists.
func (*StdFileHandler) Read ¶ added in v0.4.3
func (h *StdFileHandler) Read(name string) ([]byte, error)
Read reads the named file and returns the contents.
func (*StdFileHandler) Remove ¶ added in v0.4.3
func (h *StdFileHandler) Remove(name string) error
Remove delete the named file from system.