config

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2021 License: MIT Imports: 9 Imported by: 0

README

Meta-Viper is a wrapper over viper, it uses a Go tagged struct to:

  1. Initialize viper's configuration.

  2. Load application configuration on that same struct.

You can find examples at ./examples or in this article: https://carlosvin.github.io/posts/create-cmd-tool-golang.

meta viper

Usage {#_usage}

Define the struct holding your application config

Meta-Viper will try to load the configuration in that struct from configuration files, environment variables or flags.

Example.

package main

import (
    "fmt"
    "os"

    config "github.com/carlosvin/meta-viper"
)

type appConfig struct {
    Host      string `cfg_name:"host" cfg_desc:"Server host"`
    Port      int    `cfg_name:"port" cfg_desc:"Server port"`
    SearchAPI string `cfg_name:"apis.search" cfg_desc:"Search API endpoint"`
}

func main() {
    cfg := &appConfig{ 
        Host: "localhost",
        Port: 6000,
        SearchAPI: "https://google.es",
    }

    _, err := config.New(cfg, os.Args) 
    if err != nil {
        panic(err)
    }
    log.Printf("Loaded Configuration %v...", cfg)
}
  • We instantiate the declared struct. As you can see you can optionally specify default values.

  • It loads the configuration in the passed struct cfg. The os.Args are required to parse the application flags.

Let’s focus on one application configuration attribute to explain the example. Meta-Viper will allow you to load the config into Host structure attribute in 3 different ways:

Using flags.

./your-program --host=my.host

Using environment variables.

HOST=my.host ./your-program

Loading the data from a file in json, yaml or toml format.

./your-program --config=qa 
  • Following the qa configuration file content

qa.json.

{
    "host": "qa.host",
    "port": 8000,
    "apis": {
        "search": "https://search.api/"
    }
}

You can combine flags, environment variables and configuration files.

Tags {#_tags}

cfg_name

Required tag to specify the configuration parameter name.

cfg_description

Optional tag to describe how to use the configuration parameter.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cfg

type Cfg interface {
	// Reload the configured attributes in the structure from env, flags and/or configuration files
	Reload() error
}

Cfg Configuration loader

func New

func New(cfg interface{}, args []string) (Cfg, error)

New Instantiate a configuration loader. It loads the configuration into cfg argument.

Directories

Path Synopsis
examples
multi-env/cmd
This is a simple example of a command loading configuration depending on the environment e.g.
This is a simple example of a command loading configuration depending on the environment e.g.

Jump to

Keyboard shortcuts

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