Code Monkey home page Code Monkey logo

config's Introduction

Simple application configuration tool

Key features

Here is the list of features you may be interested in:

  • ✔️ Simple
  • ✔️ Supports JSON (Using encoding/json)
  • ✔️ Supports YAML (Using https://gopkg.in/yaml.v2)
  • ✔️ Supports TOML (Using https://github.com/BurntSushi/toml)
  • ✔️ Supports INI (Using https://gopkg.in/ini.v1)
  • ✔️ Aliases, subfolders and on-demand lookup in home folder (github.com/mitchellh/go-homedir) and /etc/
  • ✔️ Data validation
  • ❌ Placeholders
  • ❌ Configuration file chaining
  • ❌ Autoreload

Before you start

First of all, you need some structure, that will hold configuration for your application. Like this:

type Config struct {
    Token string `json:"token" yaml:"token" toml:"token"`
}

Simplest example

Just import and call config.ReadDefault.

package main

import "github.com/mono83/config"

func main() {
  var c Config
  if err := config.ReadDefault(&c); err != nil {
    panic(err)
  }

  fmt.Println(c)
}

Module will make attempt to find one of these files: config.json, config.yaml, config.toml in current folder and unmarshal them using corresponding unmarshaller.

More complex usecase

You can specify filename and other options using config.Source structure (event config.ReadDefault uses it intenally):

if err := (config.Source{FileName: "app.json"}).Read(&c); err != nil {
  panic(err)
}

There can be multiple aliases for configuration files (module reads only one - that was specified/found first, no joining applied):

if err := (config.Source{
    FileNames: []string{"app.json", "config.json"},
  }).Read(&c); err != nil {
  panic(err)
}

You can specify subfolder for configuration files:

if err := (config.Source{
    Subfolder: "configuration",
    FileNames: []string{"app.json", "config.json"},
  }).Read(&c); err != nil {
  panic(err)
}

Or even allow lookup in homedir and /etc/

if err := (config.Source{
    Subfolder: "configuration",
    FileNames: []string{"app.json", "config.json"},
    LookupHome: true,
    LookupEtc: true,
  }).Read(&c); err != nil {
  panic(err)
}

Validation

If structure, that is used in configuration has method Validate() error, configuration reader will automatically invoke it:

type Config struct {
    Token string `json:"token" yaml:"token" toml:"token"`
}

// Validate will be automatically invoked by mono83/config
func (c Config) Validate() error {
  if len(c.Token) == 0 {
    return errors.New("empty access token")
  }

  return nil
}

This behaviour can be disabled during config.Source initialization:

(config.Source{SkipValidation: true, ...}).Read(...)

config's People

Contributors

gotterdemarung avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.