Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support loading configuration from both YAML files and env vars #87

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

lippserd
Copy link
Member

@lippserd lippserd commented Oct 31, 2024

This PR introduces config.Load() that supports configuration loading in three scenarios:

  1. Load configuration solely from YAML files when no relevant environment variables are set.
  2. Combine YAML file and environment variable configurations, allowing environment variables to supplement or override possible incomplete YAML configurations.
  3. Load entirely from environment variables if the default YAML config file is absent and no specific config path is provided by the user.

config.FromYAMLFile() is still called first but continuation with config.FromEnv() is allowed by handling:

  • ErrInvalidConfiguration: The configuration may be incomplete and will be revalidated in config.FromEnv().
  • Non-existent file errors: If no explicit config path is set, fallback to environment variables is allowed.

config.FromEnv() is called regardless of the outcome from config.FromYAMLFile(). If no environment variables are set, configuration relies entirely on YAML. Otherwise, environment variables can supplement, override YAML settings, or serve as the sole source. config.FromEnv() also includes validation, ensuring completeness after considering both sources.

Possible alternative implementations:

  • If the config path is the default, os.Stat() could be used before calling config.FromYAMLFile(), rather than handling non-existent file errors. This approach would split the logic into two sections and add an additional if block.
  • Don't call Validate() in the config package automatically. Instead, require it to be called manually. I appreciate that library-wise, both config.FromYAMLFile() and config.FromEnv() include validation allowing them to be used without needing an additional function call on their own. When combining them, I think it's straightforward to use errors.Is() to check for ErrInvalidConfiguration, i.e. errors from Validate().

requires #83

Any error returned from `Validate()` is propagated with
`ErrInvalidConfiguration` attached, allowing `errors.Is()` checks on the
returned errors to recognize both `ErrInvalidConfiguration` and the original
errors returned from `Validate()`.
This commit introduces the `Flags` interface, which provides methods related to
access the configuration file path specified via command line flags. This
interface will be implemented by flag structs containing a switch for the
configuration file path and will serve as an argument for a future library
function that combines loading configurations from YAML files and environment
variables.
This commit adds the `config#Load` function to load configurations from both
YAML files and environment variables. It handles scenarios where configurations
are loaded exclusively from YAML, combined with environment variables, or
entirely from environment variables if the YAML file is missing and no specific
configuration file is provided via command line flags.
Else, they must be kept synchronized.
* Remove package docs, as `Load()` already provides a fairly complete example.
* Add example usage to `FromEnv()`.
* Add `TLS` to example usages.
* Harmonize doc blocks.
@cla-bot cla-bot bot added the cla/signed CLA is signed by all contributors of a PR label Oct 31, 2024
@lippserd lippserd requested a review from oxzi October 31, 2024 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla/signed CLA is signed by all contributors of a PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants