Support loading configuration from both YAML files and env vars #831
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I will keep the original PR description as a reference, but the implementation now uses
config.Load()
from our Go libs.With this PR Icinga DB supports configuration loading in three scenarios:
config.FromYAMLFile()
is still called first but continuation withconfig.FromEnv()
is allowed by handling:config.FromEnv()
.config.FromEnv()
is called regardless of the outcome fromconfig.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:
os.Stat()
could be used before callingconfig.FromYAMLFile()
, rather than handling non-existent file errors. This approach would split the logic into two sections and add an additional if block.Validate()
in the config package automatically. Instead, require it to be called manually. I appreciate that library-wise, bothconfig.FromYAMLFile()
andconfig.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 useerrors.Is()
to check forErrInvalidConfiguration
, i.e. errors fromValidate()
.requires Icinga/icinga-go-library#87
closes #756