configmanager

package module
v0.0.0-...-1af93dc Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2024 License: MIT Imports: 11 Imported by: 0

README

configmanager

Go Report Card GoDoc

Table of Contents

Project Description

configmanager is a robust and flexible configuration management library for Go applications. It provides a unified interface for loading configuration data from various sources, including:

  • JSON, YAML, TOML, and INI files
  • Environment variables

The library prioritizes ease of use, flexibility, and robust error handling. It is designed to simplify the process of managing application settings, allowing developers to focus on core application logic.

Features

  • Support for multiple configuration formats: Seamlessly load configuration from JSON, YAML, TOML, and INI files.
  • Environment variable overrides: Override file-based configurations with environment variables for dynamic adjustments.
  • Dynamic format detection: Automatically determine the configuration format based on file extensions.
  • Flattened data representation: Access nested configuration values easily using dot-separated keys.
  • Robust error handling: Provides clear error messages for common configuration issues.
  • Extensible design: Easily add support for new configuration formats.
  • Thoroughly tested: Includes a comprehensive suite of unit tests to ensure reliability.

Installation

go get github.com/1broseidon/configmanager

Usage

package main

import (
	"fmt"
	"github.com/1broseidon/configmanager"
)

func main() {
	// Create a new ConfigManager instance
	cm := configmanager.New()

	// Load configuration from a file (e.g., config.toml)
	err := cm.LoadFromFile("config.toml")
	if err != nil {
		panic(err)
	}

	// Access configuration values
	databaseHost := cm.GetData()["database.host"].(string)
	serverPort := cm.GetData()["server.port"].(int)

	fmt.Println("Database Host:", databaseHost)
	fmt.Println("Server Port:", serverPort)
}

Configuration

Configuration File Example (config.toml):
[database]
host = "localhost"
port = 5432
user = "myuser"
password = "mypassword"

[server]
host = "0.0.0.0"
port = 8080
Environment Variable Overrides:

Environment variables can be used to override configuration values loaded from files. The environment variable names should follow a specific pattern:

CONFIG_SECTION_KEY=value

For example, to override the host value in the database section, you would use the following environment variable:

CONFIG_DATABASE_HOST=my-database-host

Contributing

We welcome contributions from the community! Please see our CONTRIBUTING.md file for guidelines on how to contribute code, report issues, and suggest enhancements.

Code of Conduct

This project adheres to the Contributor Covenant code of conduct. By participating, you are expected to uphold this code.

Project Structure

configmanager/
├── config/                 # Sample configuration files
│   ├── config.json
│   ├── config.toml
│   ├── config.yaml
│   ├── invalidconfig.toml
│   └── invalidconfig.txt
├── formats/                # Format-specific configuration loaders and savers
│   ├── jsonconfig.go
│   ├── tomlconfig.go
│   └── yamlconfig.go
├── internal/               # Internal utility functions
│   └── flatten.go
├── configmanager.go         # Core configuration manager implementation
├── dynamicconfig.go        # Dynamic configuration loading logic
├── iniconfig.go            # INI configuration handler
└── tests/                   # Unit tests
    ├── configmanager_test.go
    ├── iniconfig_test.go
    ├── jsonconfig_test.go
    ├── testutils/
    │   └── utils.go
    ├── tomlconfig_test.go
    └── yamlconfig_test.go

Acknowledgements

  • This project utilizes the following excellent third-party libraries:
    • github.com/BurntSushi/toml: For TOML parsing and encoding.
    • gopkg.in/ini.v1: For INI file handling.
    • gopkg.in/yaml.v2: For YAML parsing and encoding.
  • Thanks to all contributors who have helped make this project possible!

License

This project is licensed under the MIT License License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigLoader

type ConfigLoader interface {
	Load([]byte) error
	GetData() map[string]interface{}
}

ConfigLoader interface represents the ability to load configuration data.

type ConfigManager

type ConfigManager struct {
	// contains filtered or unexported fields
}

ConfigManager is the primary struct for managing configuration data.

func New

func New() *ConfigManager

New creates a new instance of ConfigManager.

func (*ConfigManager) GetData

func (cm *ConfigManager) GetData() map[string]interface{}

GetData retrieves the configuration data from ConfigManager.

func (*ConfigManager) LoadEnvVariables

func (cm *ConfigManager) LoadEnvVariables(config *DynamicConfig) error

LoadEnvVariables loads configuration data from environment variables.

func (*ConfigManager) LoadFromFile

func (cm *ConfigManager) LoadFromFile(filename string, config ...ConfigLoader) error

LoadFromFile loads configuration data from a file, using DynamicConfig by default if no config loader is provided.

func (*ConfigManager) SaveToFile

func (cm *ConfigManager) SaveToFile(filename string, config ...ConfigSaver) error

SaveToFile saves configuration data to a file, using DynamicConfig by default if no config saver is provided.

func (*ConfigManager) UpdateKey

func (cm *ConfigManager) UpdateKey(key string, value interface{}) error

UpdateKey updates a specific key in the configuration.

func (*ConfigManager) UpdateKeys

func (cm *ConfigManager) UpdateKeys(updates map[string]interface{}) error

UpdateKeys updates multiple keys in the configuration.

type ConfigSaver

type ConfigSaver interface {
	Save() ([]byte, error)
}

ConfigSaver interface represents the ability to save configuration data.

type DynamicConfig

type DynamicConfig struct {
	Data     map[string]interface{}
	Filename string
}

DynamicConfig dynamically loads and saves configuration based on file extension.

func (*DynamicConfig) GetData

func (dc *DynamicConfig) GetData() map[string]interface{}

GetData retrieves the configuration data from DynamicConfig.

func (*DynamicConfig) Load

func (dc *DynamicConfig) Load(data []byte) error

Load dynamically loads configuration based on file extension.

func (*DynamicConfig) Save

func (dc *DynamicConfig) Save() ([]byte, error)

Save dynamically saves configuration based on file extension.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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