Flags-first package for configuration
Switch branches/tags
Nothing to show
Clone or download
carlmjohnson and peterbourgon Add JSONParser (#11)
* Return parsed errors

* Add JSONParser

* Fix nitpicks
Latest commit d8a146b Dec 11, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Initial commit Aug 31, 2017
.travis.yml New API (#2) Nov 29, 2018
LICENSE Initial commit Aug 31, 2017
README.md Update README with latest release Dec 6, 2018
go.mod Add Go modules (#4) Dec 6, 2018
go.sum Add Go modules (#4) Dec 6, 2018
json.go Add JSONParser (#11) Dec 11, 2018
json_test.go Add JSONParser (#11) Dec 11, 2018
parse.go Add JSONParser (#11) Dec 11, 2018
parse_test.go Fix test Nov 29, 2018

README.md

ff Latest Release GoDoc Travis CI

ff stands for flags-first, and provides an opinionated way to populate a flag.FlagSet with configuration data from the environment. Specifically, it allows data to be parsed from commandline args, a configuration file, and environment variables, in that priority order.

Usage

Define a flag.FlagSet in your func main.

func main() {
	fs := flag.NewFlagSet("my-program", flag.ExitOnError)
	var (
		listenAddr = fs.String("listen-addr", "localhost:8080", "listen address")
		refresh    = fs.Duration("refresh", 15*time.Second, "refresh interval")
		debug      = fs.Bool("debug", false, "log debug information")
		_          = fs.String("config", "", "config file (optional)")
	)

Then, call ff.Parse instead of fs.Parse.

	ff.Parse(fs, os.Args[1:],
		ff.WithConfigFileFlag("config"),
		ff.WithConfigFileParser(ff.PlainParser),
		ff.WithEnvVarPrefix("MY_PROGRAM"),
	)

This example will parse flags from the commandline args, just like regular package flag, with the highest priority. If a -config file is specified, it will try to parse it using the PlainParser, which expects files in this format:

listen-addr localhost:8080
refresh 30s
debug true

It's simple to write your own config file parser.

// ConfigFileParser interprets the config file represented by the reader
// and calls the set function for each parsed flag pair.
type ConfigFileParser func(r io.Reader, set func(name, value string) error) error

Finally, it will look in the environment for variables with a MY_PROGRAM prefix. Flag names are capitalized, and separator characters are converted to underscores. In this case, for example, MY_PROGRAM_LISTEN_ADDR would match to listen-addr.