config

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2023 License: MIT Imports: 9 Imported by: 15

README

config

MIT Licensed

config 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 crg.eti.br/go/config

Example

package main

import "crg.eti.br/go/config"

/*
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 := config.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 config 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 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 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
ini_config_file
Example with configuration file.
Example with configuration file.
json_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