11// Package config provides utilities for configuration parsing and loading.
2- // It includes functionality for handling command-line flags and loading configuration from YAML files
2+ // It includes functionality for handling command-line flags and
3+ // loading configuration from YAML files and environment variables,
34// with additional support for setting default values and validation.
45// Additionally, it provides a struct that defines common settings for a TLS client.
5- //
6- // Example usage:
7- //
8- // type Config struct {
9- // ServerAddress string `yaml:"server_address" default:"localhost:8080"`
10- // TLS config.TLS `yaml:",inline"`
11- // }
12- //
13- // // Validate implements the Validator interface.
14- // func (c *Config) Validate() error {
15- // if _, _, err := net.SplitHostPort(c.ServerAddress); err != nil {
16- // return errors.Wrapf(err, "invalid server address: %s", c.ServerAddress)
17- // }
18- //
19- // return nil
20- // }
21- //
22- // type Flags struct {
23- // Config string `short:"c" long:"config" description:"Path to config file" required:"true"`
24- // }
25- //
26- // func main() {
27- // var flags Flags
28- // if err := config.ParseFlags(&flags); err != nil {
29- // log.Fatalf("error parsing flags: %v", err)
30- // }
31- //
32- // var cfg Config
33- // if err := config.FromYAMLFile(flags.Config, &cfg); err != nil {
34- // log.Fatalf("error loading config: %v", err)
35- // }
36- //
37- // tlsCfg, err := cfg.TLS.MakeConfig("icinga.com")
38- // if err != nil {
39- // log.Fatalf("error creating TLS config: %v", err)
40- // }
41- //
42- // // ...
43- // }
446package config
457
468import (
@@ -72,7 +34,9 @@ var ErrInvalidConfiguration = stderrors.New("invalid configuration")
7234// FromYAMLFile parses the given YAML file and stores the result
7335// in the value pointed to by v. If v is nil or not a struct pointer,
7436// FromYAMLFile returns an [ErrInvalidArgument] error.
37+ //
7538// It is possible to define default values via the struct tag `default`.
39+ //
7640// The function also validates the configuration using the Validate method
7741// of the provided [Validator] interface.
7842// Any error returned from Validate is propagated with [ErrInvalidConfiguration] attached,
@@ -83,14 +47,18 @@ var ErrInvalidConfiguration = stderrors.New("invalid configuration")
8347//
8448// type Config struct {
8549// ServerAddress string `yaml:"server_address" default:"localhost:8080"`
50+ // TLS config.TLS `yaml:",inline"`
8651// }
8752//
88- // // Validate implements the Validator interface.
8953// func (c *Config) Validate() error {
9054// if _, _, err := net.SplitHostPort(c.ServerAddress); err != nil {
9155// return errors.Wrapf(err, "invalid server address: %s", c.ServerAddress)
9256// }
9357//
58+ // if err := c.TLS.Validate(); err != nil {
59+ // return errors.WithStack(err)
60+ // }
61+ //
9462// return nil
9563// }
9664//
@@ -100,6 +68,11 @@ var ErrInvalidConfiguration = stderrors.New("invalid configuration")
10068// log.Fatalf("error loading config: %v", err)
10169// }
10270//
71+ // tlsCfg, err := cfg.TLS.MakeConfig("icinga.com")
72+ // if err != nil {
73+ // log.Fatalf("error creating TLS config: %v", err)
74+ // }
75+ //
10376// // ...
10477// }
10578func FromYAMLFile (name string , v Validator ) error {
@@ -137,9 +110,47 @@ type EnvOptions = env.Options
137110
138111// FromEnv parses environment variables and stores the result in the value pointed to by v.
139112// If v is nil or not a struct pointer, FromEnv returns an [ErrInvalidArgument] error.
113+ //
114+ // It is possible to define default values via the struct tag `default`.
115+ //
116+ // The function also validates the configuration using the Validate method
117+ // of the provided [Validator] interface.
140118// Any error returned from Validate is propagated with [ErrInvalidConfiguration] attached,
141119// allowing errors.Is() checks on the returned errors to recognize both ErrInvalidConfiguration and
142120// the original errors returned from Validate.
121+ //
122+ // Example usage:
123+ //
124+ // type Config struct {
125+ // ServerAddress string `env:"SERVER_ADDRESS" default:"localhost:8080"`
126+ // TLS config.TLS
127+ // }
128+ //
129+ // func (c *Config) Validate() error {
130+ // if _, _, err := net.SplitHostPort(c.ServerAddress); err != nil {
131+ // return errors.Wrapf(err, "invalid server address: %s", c.ServerAddress)
132+ // }
133+ //
134+ // if err := c.TLS.Validate(); err != nil {
135+ // return errors.WithStack(err)
136+ // }
137+ //
138+ // return nil
139+ // }
140+ //
141+ // func main() {
142+ // var cfg Config
143+ // if err := config.FromEnv(cfg, config.EnvOptions{}); err != nil {
144+ // log.Fatalf("error loading config: %v", err)
145+ // }
146+ //
147+ // tlsCfg, err := cfg.TLS.MakeConfig("icinga.com")
148+ // if err != nil {
149+ // log.Fatalf("error creating TLS config: %v", err)
150+ // }
151+ //
152+ // // ...
153+ // }
143154func FromEnv (v Validator , options EnvOptions ) error {
144155 if err := validateNonNilStructPointer (v ); err != nil {
145156 return errors .WithStack (err )
@@ -279,10 +290,12 @@ func Load(v Validator, options LoadOptions) error {
279290// ParseFlags parses CLI flags and stores the result
280291// in the value pointed to by v. If v is nil or not a struct pointer,
281292// ParseFlags returns an [ErrInvalidArgument] error.
293+ //
282294// ParseFlags adds a default Help Options group,
283295// which contains the options -h and --help.
284296// If either option is specified on the command line,
285297// ParseFlags prints the help message to [os.Stdout] and exits.
298+ //
286299// Note that errors are not printed automatically,
287300// so error handling is the sole responsibility of the caller.
288301//
0 commit comments