You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create the Options type with all configuration fields optional
Create functions to create this Options type from different environments:
Hardcoded default
Environment variables
Files
CLI options
It's absolutely okay for different environments to fill only some parts of the config.
Implement a function to combine two Options with the second argument overriding the previous values if they both are present (otherwise, take the one that is not None)
Read options on start from all four sources and combine them in the following order of increasing priority (e.g. CLI arguments are more important than everything else)
Defaults.
Environment variables
Configuration file.
CLI flags
That's all! With this approach, it should be possible to configure different options from different sources and have a uniform config.
This pattern is common in the FP world. The description (in Haskell) can be found here:
The idea is the following:
Create the
Optionstype with all configuration fields optionalCreate functions to create this
Optionstype from different environments:It's absolutely okay for different environments to fill only some parts of the config.
Implement a function to combine two
Optionswith the second argument overriding the previous values if they both are present (otherwise, take the one that is notNone)Read options on start from all four sources and combine them in the following order of increasing priority (e.g. CLI arguments are more important than everything else)
That's all! With this approach, it should be possible to configure different options from different sources and have a uniform config.