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
washingt0 and felipeweb Implementation of #33 watch config file changes (#47)
* Implementation of #33 watch config file changes

* Implementation of #33 create new func to watch file changes
Latest commit 95fb7fa Oct 13, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci configure circle ci May 14, 2019
examples Implementation of #33 watch config file changes (#47) Oct 13, 2019
goenv fix issue-43 Jul 12, 2019
goflags Removes unnecessary *else* statements and decreases cyclomatic comple… Nov 25, 2018
hcl hcl support (#34) Nov 22, 2018
helper Implement ini file, remove unused save routines Apr 25, 2018
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
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 Implementation of #33 watch config file changes (#47) Oct 13, 2019
config_test.go Implementatio of #40 allow the path and file to be set with environme… Oct 3, 2019
cover.sh update packages name Aug 1, 2017
doc.go update packages name Aug 1, 2017
wercker.yml update packages name Aug 1, 2017

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.