github.com/blend/go-sdk@v1.20220411.3/configutil/doc.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 /* 9 Package configutil contains helpers for configuration. 10 11 The intent of this package is to allow you to compose configuration types found throughout `sdk/` and elsewhere into a single configuration type for a given program. 12 13 An example configuration type might be: 14 15 type Config struct { 16 DB db.Config `yaml:"db"` 17 Logger logger.Config `yaml:"logger"` 18 Web web.Config `yaml:"web"` 19 } 20 21 On start, you may want to read from a yaml configuration file into this type, deserializing the configuration. 22 23 var cfg Config 24 paths, err := configutil.Read(&cfg) 25 // `err` is the first error returned from reading files 26 // `paths` are the paths read from to populate the config 27 28 If we want to also read from the environment, we can do so by adding a resolver, and using the `configutil.Resolve`, `configutil.SetXYZ` helpers. 29 30 func (c *Config) Resolve(ctx context.Context) error { 31 // NOTE: the pointer receiver is critical to modify the fields of the config 32 return configutil.Resolve(ctx, 33 (&c.DB).Resolve, // we use the (&foo) form here because we want to call the pointer receiver for `Resolve` 34 (&c.Logger).Resolve, 35 (&c.Web).Resolve, 36 37 configutil.SetString(&c.Web.BindAddr, configutil.Env("BIND_ADDR"), configutil.String(c.Web.BindAddr)), 38 ) 39 } 40 41 In the above, the environment variable `BIND_ADDR` takes precedence over the string value found in any configuration file(s). 42 43 Note, we also "resolve" each of the attached configs first, in case they also have environment variables they read from etc. 44 */ 45 package configutil // import "github.com/blend/go-sdk/configutil"