goconfig

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2018 License: MIT Imports: 7 Imported by: 0

README

goconfig

Build Status Go Report Card codecov GoDoc Go project version MIT Licensed

goconfig uses a struct as input and populates the fields of this struct with parameters from command line, environment variables and configuration file.

Install

go get github.com/crgimenes/goconfig

Example

package main

import "github.com/crgimenes/goconfig"

/*
step 1: Declare your configuration struct,
it may or may not contain substructures.
*/

type mongoDB struct {
	Host string `cfgDefault:"example.com" crgRequired:"true"`
	Port int    `cfgDefault:"999"`
}

type configTest struct {
	Domain    string
	DebugMode bool `json:"db" cfg:"db" cfgDefault:"false"`
	MongoDB   mongoDB
}

func main() {

	// step 2: Instantiate your structure.
	config := configTest{}

	// step 3: Pass the instance pointer to the parser
	err := goconfig.Parse(&config)
	if err != nil {
		println(err)
		return
	}

	/*
	   The parser populated your struct with the data
	   it took from environment variables and command
	   line and now you can use it.
	*/

	println("config.Domain......:", config.Domain)
	println("config.DebugMode...:", config.DebugMode)
	println("config.MongoDB.Host:", config.MongoDB.Host)
	println("config.MongoDB.Port:", config.MongoDB.Port)
}

With the example above try environment variables like $DOMAIN or $MONGODB_HOST and run the example again to see what happens.

You can also try using parameters on the command line, try -h to see the help.

Contributing

  • Fork the repo on GitHub
  • Clone the project to your own machine
  • Create a branch with your modifications git checkout -b fantastic-feature.
  • Then commit your changes git commit -m 'Implementation of new fantastic feature'
  • Make a push to your branch git push origin fantastic-feature.
  • Submit a Pull Request so that we can review your changes

Documentation

Overview

Package goconfig uses a struct as input and populates the fields of this struct with parameters fom command line, environment variables and configuration file.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrFileFormatNotDefined = errors.New("file format not defined")

ErrFileFormatNotDefined Is the error that is returned when there is no defined configuration file format.

View Source
var File string

File name of default config file

View Source
var FileRequired bool

FileRequired config file required

View Source
var Formats []Fileformat

Formats is the list of registered formats.

View Source
var HelpString string

HelpString temporarily saves help

View Source
var Path string

Path sets default config path

View Source
var PrefixEnv string

PrefixEnv is a string that would be placed at the beginning of the generated Event tags.

View Source
var PrefixFlag string

PrefixFlag is a string that would be placed at the beginning of the generated Flag tags.

View Source
var Tag = "cfg"

Tag to set main name of field

View Source
var TagDefault = "cfgDefault"

TagDefault to set default value

View Source
var TagHelper = "cfgHelper"

TagHelper to set usage help line

View Source
var Usage func()

Usage is a function to show the help, can be replaced by your own version.

Functions

func DefaultUsage

func DefaultUsage()

DefaultUsage is assigned for Usage function by default

func Parse

func Parse(config interface{}) (err error)

Parse configuration

Example
type config struct {
	Name  string `cfg:"Name" cfgDefault:"root"`
	Value int    `cfg:"Value" cfgDefault:"123"`
}

cfg := config{}

err := Parse(&cfg)
if err != nil {
	println(err)
}

println("Name:", cfg.Name, "Value:", cfg.Value)
Output:

func PrintDefaults

func PrintDefaults()

PrintDefaults print the default help

Types

type Fileformat

type Fileformat struct {
	Extension   string
	Load        func(config interface{}) (err error)
	PrepareHelp func(config interface{}) (help string, err error)
}

Fileformat struct holds the functions to Load the file containing the settings

Directories

Path Synopsis
examples
hcl_config_file
Example with configuration file.
Example with configuration file.
ini_config_file
Example with configuration file.
Example with configuration file.
json_config_file
Example with configuration file.
Example with configuration file.
toml_config_file
Example with configuration file.
Example with configuration file.
yaml_config_file
Example with configuration file.
Example with configuration file.

Jump to

Keyboard shortcuts

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