Documentation
¶
Overview ¶
Package envconf allows for loading configuration from environment variables.
While multiple packages offer similar functionalities (e.g., github.com/kelseyhightower/envconfig, github.com/caarlos0/env), envconf distinguishes itself by its configuration method. Unlike tag-based approaches, envconf employs a method-based API, similar to flag.FlagSet.
A basic usage example is provided below:
package main
type configuration struct {
Port int
Debug bool
}
func main() {
env := envconf.LoadEnv()
conf := configuration{
Port: env.Int("PORT", 8000, "The port number used by the server."),
Debug: env.Bool("DEBUG", false, "Enable debug functionality.")
}
if envconf.HasHelp(os.Args[1:]) {
env.WriteHelp(os.Stdout)
os.Exit(0)
}
// ...
}
Index ¶
- func HasHelp(args []string) bool
- type Configuration
- func (c *Configuration) Bool(name string, fallback bool, description string) bool
- func (c *Configuration) Dur(name string, fallback time.Duration, description string) time.Duration
- func (c *Configuration) Int(name string, fallback int, description string) int
- func (c *Configuration) Ints(name string, separator string, fallback []int, description string) []int
- func (c *Configuration) Str(name string, fallback string, description string) string
- func (c *Configuration) WriteHelp(out io.Writer)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Configuration ¶
type Configuration struct {
// OnFali callback function, if set, is called when a configuration
// cannot be parsed.
// If not set, default behaviour is to write the error to stderr and
// exit the current process with code 2.
OnFail func(err error)
// contains filtered or unexported fields
}
func Load ¶
func Load(environment []string) *Configuration
Load returns a new Configuration instance, populated from the given list of "NAME=VALUE" serialized pairs.
func LoadEnv ¶
func LoadEnv() *Configuration
LoadEnv returns a new Configuration instance, populated from the environment variables.
func (*Configuration) Bool ¶
func (c *Configuration) Bool(name string, fallback bool, description string) bool
Bool returns the boolean value represented by the variable with given name. If the variable is not specified, fallback value is returned.
func (*Configuration) Dur ¶
Dur returns a time duration represented by the variable with given name. If the variable is not specified, fallback value is returned.
func (*Configuration) Int ¶
func (c *Configuration) Int(name string, fallback int, description string) int
Int returns an integer value of a variable with given name. If the variable is not specified, fallback value is returned.
func (*Configuration) Ints ¶
func (c *Configuration) Ints(name string, separator string, fallback []int, description string) []int
Ints returns a list of integer values represented by a variable with given name. If the variable is not specified, fallback value is returned.
func (*Configuration) Str ¶
func (c *Configuration) Str(name string, fallback string, description string) string
Str returns the string representation of a variable.
func (*Configuration) WriteHelp ¶
func (c *Configuration) WriteHelp(out io.Writer)
WriteHelp writers a tabular representation of all requested environment variable names, their corresponding default values and description.