goconfig

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2025 License: ISC Imports: 8 Imported by: 0

README

GoConfig

GoConfig is a Go package that provides a flexible and easy-to-use configuration management system. It supports loading configuration from various sources such as environment variables, JSON files, and in-memory maps. The package defines a Config interface with methods for loading, setting, getting, checking existence, and casting configuration values.

Installation

To install GoConfig, use the following command:

go get github.com/mekramy/goconfig
Interface Methods

The Config interface provides the following methods:

  • Load() error: Loads configuration from the specified source.
  • Set(key string, value interface{}): Sets a configuration value for the given key. If the configuration key exists in the config files, this method overrides it.
  • Get(key string) interface{}: Retrieves the configuration value for the given key.
  • Exists(key string) bool: Checks if a configuration value exists for the given key.
  • Cast(key string) (gocast.Caster, error): Casts the configuration value to the specified type.
Constructor Methods
NewEnv

Creates a new Config instance that loads configuration from .env files to environment variables and reads config from OS environment variables.

NewJSON

Creates a new Config instance that loads configuration from JSON files. If multiple files are passed, the file name must be specified as part of the config key.

NewMemory

Creates a new Config instance that loads configuration from an in-memory map.

Example
package main

import (
    "fmt"
    "github.com/mekramy/goconfig"
)

func main() {
    // Load configuration from an env file
    env := goconfig.NewEnv(".env")
    if err != nil {
        fmt.Println("Error loading config:", err)
        return
    }

    // Override value
    env.Set("DATABASE_PASSWORD", "my-secret-password")

    // Check value
    if env.Exists("REDIS") {
        fmt.Println("REDIS config is defined")
    }

    // Cast value
    if onProd := env.Cast("PROD").BoolSafe(false); onProd {
        fmt.Println("App runs in production mode")
    }

    // Load configuration from multiple JSON files
    // If config is loaded from a single file, you must
    // enter the config path without the file name
    //
    // app.json content
    // {
    //      "meta": {
    //          "name": "test",
    //          "version": "v2.3.0"
    //      }
    // }
    //
    // http.json
    // {
    //      "port": 8080,
    //      "ssl": false,
    //      "domain": "my-app.com"
    // }

    json, err := goconfig.NewJSON("app.json", "http.json")
    if err != nil {
        fmt.Println("Error loading config:", err)
        return
    }

    // Access config with fallback
    port := json.Cast("http.port").IntSafe(8888) // use 8888 if http.port does not exist
    version := json.Cast("app.meta.version").StringSafe("v0.0.0")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config interface {
	// Load loads the configuration from a source.
	Load() error

	// Set sets/override the value for a given key in the configuration.
	Set(key string, value any)

	// Get retrieves the value associated with the given key.
	// on json driver if multiple file passed you must append
	// file name to access setting .e.g. file1.some.field
	Get(key string) any

	// Exists checks if a given key exists in the configuration.
	Exists(key string) bool

	// Cast retrieves the value associated with the given key and returns it
	// as a gocast.Caster, which allows for type-safe casting.
	Cast(key string) gocast.Caster
}

Config is an interface that defines methods for loading, setting, getting, checking existence, and casting configuration values.

func NewEnv

func NewEnv(files ...string) (Config, error)

NewEnv creates a new Config instance that loads configuration from environment variables. It accepts an optional list of file paths to load additional environment variables from. Returns a Config instance or an error if loading fails.

func NewJSON

func NewJSON(files ...string) (Config, error)

NewJSON creates a new Config instance that loads configuration from JSON files. It accepts a list of file paths to load the JSON configuration from. Returns a Config instance or an error if loading fails.

func NewMemory

func NewMemory(config map[string]any) (Config, error)

NewMemory creates a new Config instance that loads configuration from an in-memory map. It accepts a map of configuration key-value pairs. Returns a Config instance or an error if loading fails.

Jump to

Keyboard shortcuts

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