|
| 1 | +// Package config provides utilities for configuration parsing and loading. |
| 2 | +// It provides functionality for parsing command-line flags and loading configuration from YAML files, |
| 3 | +// with additional support for setting default values and validation. |
| 4 | +// |
| 5 | +// The package includes the following main components: |
| 6 | +// |
| 7 | +// - [ParseFlags] function for parsing command-line flags. |
| 8 | +// - [FromYAMLFile] function for loading configuration settings from a YAML file, |
| 9 | +// with additional support for setting default values and validation. |
| 10 | +// - [TLS] struct representing configuration for a TLS client which |
| 11 | +// can be assembled to [crypto/tls.Config]. |
| 12 | +// |
| 13 | +// Example usage: |
| 14 | +// |
| 15 | +// type Config struct { |
| 16 | +// ServerAddress string `yaml:"server_address" default:"localhost:8080"` |
| 17 | +// TLS config.TLS `yaml:",inline"` |
| 18 | +// } |
| 19 | +// |
| 20 | +// // Validate implements the Validator interface. |
| 21 | +// func (c *Config) Validate() error { |
| 22 | +// if _, _, err := net.SplitHostPort(c.ServerAddress); err != nil { |
| 23 | +// return errors.Wrapf(err, "invalid server address: %s", c.ServerAddress) |
| 24 | +// } |
| 25 | +// |
| 26 | +// return nil |
| 27 | +// } |
| 28 | +// |
| 29 | +// type Flags struct { |
| 30 | +// Config string `short:"c" long:"config" description:"Path to config file" required:"true"` |
| 31 | +// } |
| 32 | +// |
| 33 | +// func main() { |
| 34 | +// var flags Flags |
| 35 | +// if err := config.ParseFlags(&flags); err != nil { |
| 36 | +// log.Fatalf("error parsing flags: %v", err) |
| 37 | +// } |
| 38 | +// |
| 39 | +// var cfg Config |
| 40 | +// if err := config.FromYAMLFile(flags.Config, &cfg); err != nil { |
| 41 | +// log.Fatalf("error loading config: %v", err) |
| 42 | +// } |
| 43 | +// |
| 44 | +// tlsCfg, err := cfg.TLS.MakeConfig("icinga.com") |
| 45 | +// if err != nil { |
| 46 | +// log.Fatalf("error creating TLS config: %v", err) |
| 47 | +// } |
| 48 | +// |
| 49 | +// // ... |
| 50 | +// } |
1 | 51 | package config
|
2 | 52 |
|
3 | 53 | import (
|
|
0 commit comments