1
+ // Package config provides utilities for configuration parsing and loading.
2
+ // It includes functionality for handling command-line flags and loading configuration from YAML files,
3
+ // with additional support for setting default values and validation.
4
+ // 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
+ // }
1
44
package config
2
45
3
46
import (
@@ -20,6 +63,33 @@ var ErrInvalidArgument = stderrors.New("invalid argument")
20
63
// FromYAMLFile parses the given YAML file and stores the result
21
64
// in the value pointed to by v. If v is nil or not a pointer,
22
65
// FromYAMLFile returns an [ErrInvalidArgument] error.
66
+ // It is possible to define default values via the struct tag `default`.
67
+ // The function also validates the configuration using the Validate method
68
+ // of the provided [Validator] interface.
69
+ //
70
+ // Example usage:
71
+ //
72
+ // type Config struct {
73
+ // ServerAddress string `yaml:"server_address" default:"localhost:8080"`
74
+ // }
75
+ //
76
+ // // Validate implements the Validator interface.
77
+ // func (c *Config) Validate() error {
78
+ // if _, _, err := net.SplitHostPort(c.ServerAddress); err != nil {
79
+ // return errors.Wrapf(err, "invalid server address: %s", c.ServerAddress)
80
+ // }
81
+ //
82
+ // return nil
83
+ // }
84
+ //
85
+ // func main() {
86
+ // var cfg Config
87
+ // if err := config.FromYAMLFile("config.yml", &cfg); err != nil {
88
+ // log.Fatalf("error loading config: %v", err)
89
+ // }
90
+ //
91
+ // // ...
92
+ // }
23
93
func FromYAMLFile (name string , v Validator ) error {
24
94
rv := reflect .ValueOf (v )
25
95
if rv .Kind () != reflect .Pointer || rv .IsNil () {
@@ -86,6 +156,21 @@ func FromEnv(v Validator, options EnvOptions) error {
86
156
// ParseFlags prints the help message to [os.Stdout] and exits.
87
157
// Note that errors are not printed automatically,
88
158
// so error handling is the sole responsibility of the caller.
159
+ //
160
+ // Example usage:
161
+ //
162
+ // type Flags struct {
163
+ // Config string `short:"c" long:"config" description:"Path to config file" required:"true"`
164
+ // }
165
+ //
166
+ // func main() {
167
+ // var flags Flags
168
+ // if err := config.ParseFlags(&flags); err != nil {
169
+ // log.Fatalf("error parsing flags: %v", err)
170
+ // }
171
+ //
172
+ // // ...
173
+ // }
89
174
func ParseFlags (v any ) error {
90
175
rv := reflect .ValueOf (v )
91
176
if rv .Kind () != reflect .Pointer || rv .IsNil () {
0 commit comments