All Projects → jmartin82 → Mconfig

jmartin82 / Mconfig

Licence: mit
MCONFIG is a lightweight Golang library for integrating configs files like (json, yml, toml) and environment variables into one config struct.

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Mconfig

climatecontrol
Python library for loading settings and config data from files and environment variables
Stars: ✭ 20 (-28.57%)
Mutual labels:  config, yaml, toml, environment-variables
Dasel
Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.
Stars: ✭ 759 (+2610.71%)
Mutual labels:  config, json, yaml, toml
Konf
A type-safe cascading configuration library for Kotlin/Java/Android, supporting most configuration formats
Stars: ✭ 225 (+703.57%)
Mutual labels:  config, json, yaml, toml
Config
📝 Go config manage(load,get,set). support JSON, YAML, TOML, INI, HCL, ENV and Flags. Multi file load, data override merge, parse ENV var. Go应用配置加载管理,支持多种格式,多文件加载,远程文件加载,支持数据合并,解析环境变量名
Stars: ✭ 225 (+703.57%)
Mutual labels:  config, json, yaml, toml
parse it
A python library for parsing multiple types of config files, envvars & command line arguments that takes the headache out of setting app configurations.
Stars: ✭ 86 (+207.14%)
Mutual labels:  config, yaml, toml, environment-variables
Simple Settings
A simple way to manage your project settings.
Stars: ✭ 165 (+489.29%)
Mutual labels:  config, json, yaml, toml
Fig
A minimalist Go configuration library
Stars: ✭ 142 (+407.14%)
Mutual labels:  environment-variables, json, yaml, toml
Koanf
Light weight, extensible configuration management library for Go. Built in support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.
Stars: ✭ 450 (+1507.14%)
Mutual labels:  config, yaml, toml
Vscode Data Preview
Data Preview 🈸 extension for importing 📤 viewing 🔎 slicing 🔪 dicing 🎲 charting 📊 & exporting 📥 large JSON array/config, YAML, Apache Arrow, Avro, Parquet & Excel data files
Stars: ✭ 245 (+775%)
Mutual labels:  config, json, yaml
config-cpp
C++ Configuration management library inspired by the Viper package for golang.
Stars: ✭ 21 (-25%)
Mutual labels:  yaml, toml, environment-variables
paerser
No description or website provided.
Stars: ✭ 38 (+35.71%)
Mutual labels:  yaml, toml, environment-variables
cfg-rs
A Configuration Library for Rust Applications
Stars: ✭ 18 (-35.71%)
Mutual labels:  config, yaml, toml
Structured Text Tools
A list of command line tools for manipulating structured text data
Stars: ✭ 6,180 (+21971.43%)
Mutual labels:  json, yaml, toml
Remarshal
Convert between CBOR, JSON, MessagePack, TOML, and YAML
Stars: ✭ 421 (+1403.57%)
Mutual labels:  json, yaml, toml
Config
🛠 A configuration library for Go that parses environment variables, JSON files, and reloads automatically on SIGHUP
Stars: ✭ 203 (+625%)
Mutual labels:  config, environment-variables, json
transfer
Converts from one encoding to another. Supported formats HCL ⇄ JSON ⇄ YAML⇄TOML⇄XML⇄plist⇄pickle⇄properties ...
Stars: ✭ 70 (+150%)
Mutual labels:  config, yaml, toml
Config Rs
⚙️ Layered configuration system for Rust applications (with strong support for 12-factor applications).
Stars: ✭ 915 (+3167.86%)
Mutual labels:  config, json, toml
Node Convict
Featureful configuration management library for Node.js
Stars: ✭ 1,855 (+6525%)
Mutual labels:  config, environment-variables, json
Cfgdiff
diff(1) all your configs
Stars: ✭ 138 (+392.86%)
Mutual labels:  config, json, yaml
exenv
Exenv makes loading environment variables from external sources easy.
Stars: ✭ 35 (+25%)
Mutual labels:  config, yaml, environment-variables

Build Status codecov

Mconfig

Mconfig is a lightweight Golang library for integrating configs files like (json, yml, toml) and environment variables into one config struct.

Features

  • Load multiple types of files (yaml, json, toml).
  • Autofill environment variables with .env files.
  • Works with nested structs.
  • Combine config files and environment variables.
  • Map environment variables to struct via tag.
  • Auto casting.
  • Easy to use (only one function exposed).
  • Extendable (allows add more providers).
  • Customizable (via facade).

Usage

Mconfig is designed to be very simple and straightforward to use. All you can do with it is load configurations to a predifined struct.

First define a configuration structure:

type MysqlConfiguration struct {
    Host     string `env:"MYSQL_HOST"`
    Username string `env:"MYSQL_USERNAME"`
    Password string `env:"MYSQL_PASSWORD"`
    Database string `env:"MYSQL_DATABASE"`
    Port     int    `env:"MYSQL_PORT"`
}

type RedisConfiguration struct {
    Host string `env:"REDIS_HOST"`
    Port int    `env:"REDIS_PORT"`
}

type Configuration struct {
    Port         int `env:"APP_PORT"`
    Mysql        MysqlConfiguration
    Redis        RedisConfiguration
}

Then fill your YAML (config.yaml) file:

---
Port: 3001
Mysql:
  Host: 192.168.0.1
  Username: root
  Password: test
  Database: mconfig
  Port: 3306
Redis:
  Host: localhost
  Port: 6379

From your code:

import 	"github.com/jmartin82/mconfig/pkg/mconfig"

configuration := Configuration{}
err := mconfig.Parse("config.yaml", &configuration)
if err != nil {
	panic(err)
}

Finally in prod:

MYSQL_PASSWORD=SuperSecret MYSQL_HOST=mysql.prod.service REDIS_HOST=mysql.prod.service your_app

Supported types

All environment variables are string but you can specify the correct type in your configuration struct.

The valid types are:

type TestTypes struct {
	IsInt        int      `env:"FOO_VAR"`
	IsUint       uint     `env:"FOO_VAR"`
	IsString     string   `env:"FOO_VAR"`
	IsFloat      float64  `env:"FOO_VAR"`
	IsBool       bool     `env:"FOO_VAR"`
	PtrIsInt     *int     `env:"FOO_VAR"`
	PtrIsInt8    *int8    `env:"FOO_VAR"`
	PtrIsInt16   *int16   `env:"FOO_VAR"`
	PtrIsInt32   *int32   `env:"FOO_VAR"`
	PtrIsInt64   *int64   `env:"FOO_VAR"`
	PtrIsUint    *uint    `env:"FOO_VAR"`
	PtrIsUint8   *uint8   `env:"FOO_VAR"`
	PtrIsUint16  *uint16  `env:"FOO_VAR"`
	PtrIsUint32  *uint32  `env:"FOO_VAR"`
	PtrIsUint64  *uint64  `env:"FOO_VAR"`
	PtrIsString  *string  `env:"FOO_VAR"`
	PtrIsFloat32 *float32 `env:"FOO_VAR"`
	PtrIsFloat64 *float64 `env:"FOO_VAR"`
        PtrIsBool    *bool    `env:"FOO_VAR"`
        Struct       struct
        PtrStruct    *struct
}

In case of an casting error, the field will keep the original value.

Customization

By default only the Parse function it's exposed. With that, you can read json, yaml, toml. Load .env files and combine the config read from the file with your system environment variables.

But if you don't want the full feature call, you always can use the facade to adjust the functionality to your necessities.

Read-only env vars from your system and load into your config struct.

import 	"github.com/jmartin82/mconfig/pkg/mconfig"

configuration := Configuration{}
err := mconfig.ConfigManager.ReadFromEnvironment(&configuration);
if err != nil {
	panic(err)
}

Read only the config from a toml.

import 	"github.com/jmartin82/mconfig/pkg/mconfig"

configuration := Configuration{}
err := mconfig.ConfigManager.ReadFromFile("config.toml", &configuration)
if err != nil {
	panic(err)
}

Use differnt files to load the enviroment variables.

import 	"github.com/jmartin82/mconfig/pkg/mconfig"

configuration := Configuration{}
mconfig.ConfigManager.SetEnvFiles("enviroment.txt","env.txt")
err := mconfig.Parse("config.yaml", &configuration)
if err != nil {
	panic(err)
}

Extensibility

You can add more file provider in order to read different kind of config files.

Interface:

type Unmarshaler interface {
	IsCapable(filename string) bool
	Unmarshal(source []byte, configuration interface{}) error
}

Code:

import 	"github.com/jmartin82/mconfig/pkg/mconfig"

configuration := Configuration{}
mconfig.ConfigManager.AddFileUnmashaler(NewXMLFormatUnmarshaler())
err := mconfig.Parse("config.yaml", &configuration)
if err != nil {
	panic(err)
}

Current limitations

  • Only the exposed fields can be setted via file config or enviroment variable.
  • The config struct should be a pointer.

Licence

Copyright © 2018 - 2018, Jordi Martín (http://jordi.io)

Released under MIT license, see LICENSE for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].