Home

Awesome

<p align="center"> <br/> <br/> <img width="637px" src="https://user-images.githubusercontent.com/16992394/78989442-e7c1ae80-7b33-11ea-98c6-1d37ed276a3b.png"> </p> <h3 align="center">Opinionated configuration loading framework for Containerized and <a href="https://12factor.net/config">12-Factor</a> compliant applications.</h3> <h6 align="center">Read configurations from Environment Variables, and/or Configuration Files. With support to Environment Variables Expanding and Validation Methods.</h4> <p align="center"> <a href="https://github.com/avelino/awesome-go#configuration"> <img src="https://awesome.re/mentioned-badge.svg" alt="Mentioned in Awesome Go"> </a> <a href="http://godoc.org/github.com/sherifabdlnaby/configuro"> <img src="https://godoc.org/github.com/sherifabdlnaby/configuro?status.svg" alt="Go Doc"> </a> <a> <img src="https://img.shields.io/github/v/tag/sherifabdlnaby/configuro?label=release&amp;sort=semver"> </a> <a> <img src="https://img.shields.io/badge/Go-%3E=v1.11-blue?style=flat&logo=go" alt="Go Version"> </a> <a> <img src="https://github.com/sherifabdlnaby/configuro/workflows/Build/badge.svg"> </a> <a href='https://coveralls.io/github/sherifabdlnaby/configuro'><img src='https://img.shields.io/coveralls/github/sherifabdlnaby/configuro?logo=codecov&logoColor=white' alt='Coverage Status' /></a> <a href="https://goreportcard.com/report/github.com/sherifabdlnaby/configuro"> <img src="https://goreportcard.com/badge/github.com/sherifabdlnaby/configuro" alt="Go Report"> </a> <a href="https://raw.githubusercontent.com/sherifabdlnaby/configuro/blob/master/LICENSE"> <img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="GitHub license"> </a> </p>

Introduction

Configuro is an opinionated configuration loading and validation framework with not very much to configure. It defines a method for loading configurations without so many options for a straight-forward and simple code.

The method defined by Configuro allow you to implement 12-Factor's Config and it mimics how many mature applications do configurations (e.g Elastic, Neo4J, etc); and is also fit for containerized applications.


With Only with two lines of code, and zero setting up you get loading configuration from Config File, Overwrite values /or rely exclusively on Environment Variables, Expanding values using ${ENV} expressions, and validation tags.

Loading Configuration

<p align="center"> <img width="770" src="https://user-images.githubusercontent.com/16992394/85208005-4b830780-b32d-11ea-9b41-83a3c87704e3.png"> </p>

1. Define Application configurations in a struct

2. Setting Configuration by Environment Variables.

3. Setting Configuration by Configuration File.

4. Support Environment Variables Expanding.

5. Validate Loaded Values

Notes


Install

go get github.com/sherifabdlnaby/configuro
import "github.com/sherifabdlnaby/configuro"

Usage

1. Define You Config Struct.

This is the struct you're going to use to retrieve your config values in-code.

type Config struct {
    Database struct {
        Host     string
        Port     int
    }
    Logging struct {
        Level  string
        LineFormat string `config:"line_format"`
    }
}

2. Create and Configure the Configuro.Config object.

    // Create Configuro Object with supplied options (explained below)
    config, err := configuro.NewConfig( opts ...configuro.ConfigOption )

    // Create our Config Struct
    configStruct := &Config{ /*put defaults config here*/ }

    // Load values in our struct
    err = config.Load(configStruct)

3. Loading from Environment Variables

The above settings can be changed upon constructing the configuro object via passing these options.

    configuro.WithLoadFromEnvVars(EnvPrefix string)  // Enable Env loading and set Prefix.
    configuro.WithoutLoadFromEnvVars()               // Disable Env Loading Entirely
    configuro.WithLoadDotEnv(envDotFilePath string)  // Enable loading .env into Environment Variables
    configuro.WithoutLoadDotEnv()                    // Disable loading .env

4. Loading from Configuration Files

The above settings can be changed upon constructing the configuro object via passing these options.

    configuro.WithLoadFromConfigFile(Filepath string, ErrIfFileNotFound bool)    // Enable Config File Load
    configuro.WithoutLoadFromConfigFile()                                        // Disable Config File Load
    configuro.WithEnvConfigPathOverload(configFilepathENV string)                // Enable Overloading Path with ENV var.
    configuro.WithoutEnvConfigPathOverload()                                     // Disable Overloading Path with ENV var.

5. Expanding Environment Variables in Config

config:
    database:
        host: xyz:${PORT:3306}
        username: admin
        password: ${PASSWORD}

The above settings can be changed upon constructing the configuro object via passing these options.

    configuro.WithExpandEnvVars()       // Enable Expanding
    configuro.WithoutExpandEnvVars()    // Disable Expanding

6. Validate Struct

    err := config.Validate(configStruct)
    if err != nil {
        return err
    }
    configuro.WithValidateByTags()
    configuro.WithoutValidateByTags()
    configuro.WithValidateByFunc(stopOnFirstErr bool, recursive bool)
    configuro.WithoutValidateByFunc()

7. Miscellaneous

Built on top of

License

MIT License Copyright (c) 2020 Sherif Abdel-Naby

Contribution

PR(s) are Open and Welcomed.