modulepackage
0.0.0-20221215172404-91e9092a0318
Repository: https://github.com/gravitational/configure.git
Documentation: pkg.go.dev
# README
Configure
configure
is a golang library that populates a struct from environment variables, command line arguments and YAML files.
It works by reading a struct definition with special tags.
Usage
The latest can be seen if you run:
godoc github.com/gravitational/configure
But here's a quickstart: Define a sample structure, for example:
type Config struct {
StringVar string `env:"STRING_VAR" cli:"string-var" yaml:"string_var"`
BoolVar bool `env:"BOOL_VAR" cli:"bool_var" yaml:"bool_var"`
IntVar int `env:"INT_VAR" cli:"int_var" yaml:"int_var"`
HexVar hexType `env:"HEX_VAR" cli:"hex_var" yaml:"hex_var"`
MapVar map[string]string `env:"MAP_VAR" cli:"map_var" yaml:"map_var,flow"`
SliceMapVar []map[string]string `env:"SLICE_MAP_VAR" cli:"slice_var" yaml:"slice_var,flow"`
}
Then you can query the environment and populate that structure from environment variables, YAML files or command line arguments.
import (
"os"
"github.com/gravitational/configure"
)
func main() {
var cfg Config
// parse environment variables
err := configure.ParseEnv(&cfg)
// parse YAML
err = configure.ParseYAML(&cfg)
// parse command line arguments
err = configure.ParseCommandLine(&cfg, os.Args[1:])
}
# Packages
Copyright 2015 Gravitational, Inc.
Copyright 2015 Gravitational, Inc.
No description provided by the author
Copyright 2015 Gravitational, Inc.
Copyright 2015 Gravitational, Inc.
# Functions
CIDRFlag returns CIDR range flag.
EnableTemplating allows to treat configuration file as a template for example, it will support {{env "VAR"}} - that will substitute environment variable "VAR" and pass it to YAML file parser.
No description provided by the author
KeyValParam accepts a kingpin setting parameter and returns kingpin-compatible value.
NewCommandLineApp generates a command line parsing tool based on the struct that was passed in as a parameter.
ParseCIDR parses value of the CIDR from string.
ParseCommandLine takes a pointer to a function and attempts to initialize it from environment variables.
ParseEnv takes a pointer to a struct and attempts to initialize it from environment variables.
ParseYAML parses yaml-encoded byte string into the struct passed to the function.
# Interfaces
CLISetter is an interface for setting any value from command line flag.
Setter is an interface that properties of struct can implement to initialize the value of any struct from string.
StringSetter is an interface that allows to set variable from any string.
# Type aliases
KeyVal is map that can parse itself from string, represented as a comma-separated list of keys and values "key:val,key:val".
KeyValSlice is a list of key value strings.
ParseOption is a functional argument type.