goconfig

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2021 License: MIT Imports: 11 Imported by: 9

README

goconfig

Build Status Go Report Card Test Coverage Maintainability GoDoc Go project version MIT Licensed Gitpod Ready-to-Code

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/gosidekick/goconfig

Example

package main

import "github.com/gosidekick/goconfig"

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

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

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

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 (

	// Tag to set main name of field
	Tag = "cfg"

	// TagDefault to set default value
	TagDefault = "cfgDefault"

	// TagHelper to set usage help line
	TagHelper = "cfgHelper"

	// Path sets default config path
	Path string

	// File name of default config file
	File string

	// FileRequired config file required
	FileRequired bool

	// HelpString temporarily saves help
	HelpString string

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

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

	// ErrFileFormatNotDefined Is the error that is returned when there is no defined configuration file format.
	ErrFileFormatNotDefined = errors.New("file format not defined")

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

	// Formats is the list of registered formats.
	Formats []Fileformat

	// FileEnv is the enviroment variable that define the config file
	FileEnv string

	// PathEnv is the enviroment variable that define the config file path
	PathEnv string

	// WatchConfigFile is the flag to update the config when the config file changes
	WatchConfigFile bool

	// DisableFlags on the command line
	DisableFlags bool
)

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 ParseAndWatch added in v1.3.0

func ParseAndWatch(config interface{}) (chChanges chan int64, chErr chan error, err error)

ParseAndWatch configuration returns a channel for errors while watching files and anorther when each update has been detected

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