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

Merged
merged 7 commits into from
Jan 7, 2025

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

@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
@lippserd lippserd force-pushed the load-config-from-yaml-and-env branch from 79829da to b5ad3ae Compare January 7, 2025 12:31
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.
@lippserd lippserd force-pushed the load-config-from-yaml-and-env branch from b5ad3ae to 37ce192 Compare January 7, 2025 12:41
@lippserd lippserd merged commit cf9d730 into main Jan 7, 2025
15 checks passed
@lippserd lippserd deleted the load-config-from-yaml-and-env branch January 7, 2025 13:07
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