github.com/lyft/flytestdlib@v0.3.12-0.20210213045714-8cdd111ecda1/config/accessor.go (about) 1 // A strongly-typed config library to parse configs from PFlags, Env Vars and Config files. 2 // Config package enables consumers to access (readonly for now) strongly typed configs without worrying about mismatching 3 // keys or casting to the wrong type. It supports basic types (e.g. int, string) as well as more complex structures through 4 // json encoding/decoding. 5 // 6 // Config package introduces the concept of Sections. Each section should be given a unique section key. The binary will 7 // not load if there is a conflict. Each section should be represented as a Go struct and registered at startup before 8 // config is loaded/parsed. 9 // 10 // Sections can be nested too. A new config section can be registered as a sub-section of an existing one. This allows 11 // dynamic grouping of sections while continuing to enforce strong-typed parsing of configs. 12 // 13 // Config data can be parsed from supported config file(s) (yaml, prop, toml), env vars, PFlags or a combination of these 14 // Precedence is (flags, env vars, config file, defaults). When data is read from config files, a file watcher is started 15 // to monitor for changes in those files. If the registrant of a section subscribes to changes then a handler is called 16 // when the relevant section has been updated. Sections within a single config file will be invoked after all sections 17 // from that particular config file are parsed. It follows that if there are inter-dependent sections (e.g. changing one 18 // MUST be followed by a change in another), then make sure those sections are placed in the same config file. 19 // 20 // A convenience tool is also provided in cli package (pflags) that generates an implementation for PFlagProvider interface 21 // based on json names of the fields. 22 package config 23 24 import ( 25 "context" 26 "flag" 27 28 "github.com/spf13/pflag" 29 ) 30 31 // Provides a simple config parser interface. 32 type Accessor interface { 33 // Gets a friendly identifier for the accessor. 34 ID() string 35 36 // Initializes the config parser with golang's default flagset. 37 InitializeFlags(cmdFlags *flag.FlagSet) 38 39 // Initializes the config parser with pflag's flagset. 40 InitializePflags(cmdFlags *pflag.FlagSet) 41 42 // Parses and validates config file(s) discovered then updates the underlying config section with the results. 43 // Exercise caution when calling this because multiple invocations will overwrite each other's results. 44 UpdateConfig(ctx context.Context) error 45 46 // Gets path(s) to the config file(s) used. 47 ConfigFilesUsed() []string 48 } 49 50 // Options used to initialize a Config Accessor 51 type Options struct { 52 // Instructs parser to fail if any section/key in the config file read do not have a corresponding registered section. 53 StrictMode bool 54 55 // Search paths to look for config file(s). If not specified, it searches for config.yaml under current directory as well 56 // as /etc/flyte/config directories. 57 SearchPaths []string 58 59 // Defines the root section to use with the accessor. 60 RootSection Section 61 }