jconfig

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2025 License: BSD-3-Clause Imports: 8 Imported by: 0

README


This package provides a lightweight configuration manager for Go applications, offering features such as loading, saving, merging, and backing up configuration data. It also supports secure storage of sensitive data using AES-128 and AES-256 encryption.

Features:

  • Load and save configuration data from JSON files.
  • Merge default and custom configurations.
  • Backup and restore configuration data automatically.
  • Securely store and retrieve sensitive data using AES encryption.
  • Flexible dictionary-based configuration storage.

Documentation

Index

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

func New(path string, defaults dictx.Dict) (*Config, error)

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

func (c *Config) Delete(key string)

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

func (c *Config) Get(key string, defaultValue any) any

Get retrieves a value from the configuration buffer by key. If the key is not found, the default_value is returned.

func (*Config) GetSecure

func (c *Config) GetSecure(key string, defaultValue any) (any, error)

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

func (c *Config) InitAES128(secret string) error

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

func (c *Config) InitAES256(secret string) error

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

func (c *Config) InitBackup(path string) error

InitBackup sets the backup file path for the configuration. Returns an error if the provided path is empty.

func (*Config) IsBackupExist

func (c *Config) IsBackupExist() bool

IsBackupExist checks whether the backup file exists.

func (*Config) IsExist

func (c *Config) IsExist() bool

IsExist checks whether the main configuration file exists.

func (*Config) Keys

func (c *Config) Keys() []string

Keys returns a list of all keys in the configuration buffer.

func (*Config) Load

func (c *Config) Load() error

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

func (c *Config) Merge(updt dictx.Dict)

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

func (c *Config) Purge() error

Purge clears the configuration buffer and deletes the main and backup files (if they exist).

func (*Config) Save

func (c *Config) Save() error

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

func (c *Config) Set(key string, newValue any)

Set adds a new value in the configuration buffer by key. If the key already exists, its value is overwritten.

func (*Config) SetSecure

func (c *Config) SetSecure(key string, val any) error

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]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL