Giter Site home page Giter Site logo

gonfig's Introduction

gonfig

A simple tool to help with Go (web) service configs.

gonfig reads a given JSON file containing an arbitrary set of configuration settings and produces Go code that exposes these settings as variables. The name of the package where the variables are defined is configurable.

For example, given the following JSON:

{
    "api_endpoint": "https://us-3.rightscale.com",
    "port": 8000,
    "worker": {
        "enabled": true,
        "concurrency": 20
    }
}

Running:

gonfig config.json

generates a config.go file which includes the following snippet:

var (
        ApiEndpoint string
        Port int64
        Worker *WorkerCfg
)

type WorkerCfg struct {
        Concurrency int64 `json:"concurrency"`
        Enabled     bool  `json:"enabled"`
}

The file also contains a Load function:

func Load(path string) error

As you'd expect that function initializes the variables with the values read from the JSON. Client code can then consume the configuration file with something like:

package main

func main() {
        err := Load("config.json")
        if err != nil {
                panic("Invalid configuration: " + err.Error())
        }
        // Now use ApiEndpoint, Port and Worker variables
}

If you'd rather not pollute the main package with config stuff then gonfig can be told to use a different package and output file:

gonfig -o config/config.go -p cfg

The above Generates the file config.go in the config directory (creating it if necessary). The generated code defines the cfg package so the client code would look like:

func main() {
        err := cfg.Load("config.json")
        // Check err and use cfg.ApiEndpint, cfg.Port etc.
}

Usage

gonfig --help
usage: gonfig [<flags>]

Flags:
  --help               Show help.
  -c, --config=CONFIG  path to JSON configuration, defaults to "config.json"
  -o, --out=OUT        path to output file, defaults to "config.go"
  -p, --package="main"  
                       name of go package containing config code

gonfig's People

Watchers

John Gilmore avatar Doug Johnson avatar  avatar Richard Shade avatar Slava Khrenov avatar Alistair Scott avatar Victor Dramba avatar  avatar Daniel Rife avatar Priyanka Shahheydari avatar Marco Dominguez avatar Ryan Geyer avatar Alexey Shepelev avatar Antoine Joulie avatar Shane Rock avatar Ali Khajeh-Hosseini avatar Account for RightScale Internal QA testing purposes avatar James Cloos avatar Alexander Sologub avatar  avatar  avatar Jeremy Black avatar Ryan O'Leary avatar Rudel Saldivar avatar Jim McGaw avatar Amanda avatar  avatar Zach Little avatar  avatar  avatar Mitch Gerdisch avatar Enric Trepat avatar  avatar  avatar David Warren avatar Christopher Fredregill avatar alexander.deshkevich avatar  avatar Pavel Smalenski avatar Manuel Mendoza avatar  avatar  avatar Oleg Sviridovitch avatar  avatar Bart avatar Robert Lozoya avatar Zak Blake avatar  avatar Dave Witt avatar  avatar  avatar Andrew Nelson avatar  avatar Sean Pomeroy avatar  avatar Raphael Simon avatar Beth Harvey avatar Hayden Marx avatar Archie Robertson 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.