Skip to content
goconfig uses a struct as input and populates the fields of this struct with parameters from command line, environment variables and configuration file.
Go Shell
Branch: master
Clone or download
crgimenes Merge pull request #44 from alexk-esna/issue_43_ak
Fix bug in the bool values parsing
Latest commit 26b33cc Jul 15, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci configure circle ci May 14, 2019
examples replace cfgUsage to cfgHelper and TagUsage to TagHelper Nov 23, 2018
goenv fix issue-43 Jul 12, 2019
goflags Removes unnecessary *else* statements and decreases cyclomatic comple… Nov 25, 2018
hcl
helper
ini Implement ini file, remove unused save routines Apr 25, 2018
issues refactoring Jul 12, 2019
json Implement ini file, remove unused save routines Apr 25, 2018
structtag fix issue-43 Jul 12, 2019
toml Implement ini file, remove unused save routines Apr 25, 2018
validate int64 support (#31) Oct 24, 2018
yaml Implement ini file, remove unused save routines Apr 25, 2018
.codeclimate.yml Update .codeclimate.yml Jun 21, 2017
.gitignore Initial commit Dec 18, 2016
.travis.yml update travis Oct 10, 2017
LICENSE update packages name Aug 1, 2017
README.md document skipped field syntax Mar 7, 2019
config.go fix issue-43 Jul 12, 2019
config_test.go Add validate tests Jul 17, 2018
cover.sh update packages name Aug 1, 2017
doc.go update packages name Aug 1, 2017
wercker.yml

README.md

goconfig

Build Status Go Report Card Test Coverage Maintainability 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" 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
You can’t perform that action at this time.