jconfig

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 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 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

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

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

func (c *JConfig) Delete(key string)

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

func (c *JConfig) 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 (*JConfig) GetSecure added in v0.4.2

func (c *JConfig) 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 (*JConfig) InitAES128 added in v0.4.2

func (c *JConfig) 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 (*JConfig) InitAES256 added in v0.4.2

func (c *JConfig) 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 (*JConfig) InitBackup added in v0.4.2

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

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

func (c *JConfig) InitCipher(cipher ciphering.Handler) error

InitCipher initializes ciphering handler

func (*JConfig) IsBackupExist added in v0.4.2

func (c *JConfig) IsBackupExist() bool

IsBackupExist checks whether the backup file exists.

func (*JConfig) IsExist added in v0.4.2

func (c *JConfig) IsExist() bool

IsExist checks whether the main configuration file exists.

func (*JConfig) Keys added in v0.4.2

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

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

func (*JConfig) Load added in v0.4.2

func (c *JConfig) 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 (*JConfig) Merge added in v0.4.2

func (c *JConfig) 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 (*JConfig) Purge added in v0.4.2

func (c *JConfig) Purge() error

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

func (*JConfig) Save added in v0.4.2

func (c *JConfig) 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 (*JConfig) Set added in v0.4.2

func (c *JConfig) 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 (*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

func (c *JConfig) 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]

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.

func (*StdFileHandler) Write added in v0.4.3

func (h *StdFileHandler) Write(name string, data []byte, perm os.FileMode) error

Write writes data to the named file, creating it if necessary.

Jump to

Keyboard shortcuts

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