ffyaml
This is a YAML configuration parser for the excellent peterbourgon/ff module. It is a fork of the ffyaml
parser included that module. This fork provides the WithKeyPath
and WithAllowMissingKeyPath
options to specify the location within the YAML document where the configuration resides. It allows one to use a subset of a larger configuration file.
Usage
Given the following configuration file:
name: Super Configuration
plugins:
cli:
verbose: true
workers: 20
The following program will parse the options from it:
import (
"flag"
"fmt"
"os"
"github.com/peterbourgon/ff/v3"
"github.com/pkazmier/ffyaml"
)
func main() {
fs := flag.NewFlagSet("example", flag.ContinueOnError)
verbose := fs.Bool("verbose", false, "enable verbose logging")
workers := fs.Int("workers", 10, "number of workers to create")
parser := ffyaml.New(
ffyaml.WithKeyPath("plugins", "cli"),
ffyaml.WithAllowMissingKeyPath(true),
)
err := ff.Parse(fs, os.Args[1:],
ff.WithConfigFile("config.yaml"),
ff.WithConfigFileParser(parser.Parse),
)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("verbose = %v\n", *verbose)
fmt.Printf("workers = %v\n", *workers)
}