Fix DECKARD_MONGODB_DATABASE environment variable not working#65
Fix DECKARD_MONGODB_DATABASE environment variable not working#65
Conversation
Co-authored-by: lucasoares <10624972+lucasoares@users.noreply.github.com>
…efaults Co-authored-by: lucasoares <10624972+lucasoares@users.noreply.github.com>
|
@copilot validate existing comments of this PR |
Co-authored-by: lucasoares <10624972+lucasoares@users.noreply.github.com>
I've reviewed and addressed the code review feedback. The suggestion to extract a helper function to reduce code duplication was valid - I've implemented a generic |
|
@copilot check the last suggestion of the latest review, also make why you created this internal/service/cert/ca-cert.srl file? I don't think this file is needed. |
Co-authored-by: lucasoares <10624972+lucasoares@users.noreply.github.com>
I've addressed both issues:
Changes committed in fb75746. |
|
@copilot check the PR checks, the tests are failing |
I've checked the failing tests. There's one test failure in
The failing test is in code I haven't modified - my changes are only in |
| @@ -1 +1,2 @@ | |||
| *.pem No newline at end of file | |||
| *.pem | |||
| *.srl No newline at end of file | |||
There was a problem hiding this comment.
why is this new gitignore line needed in your PR ?
|
@copilot validate existing comments of this PR |
There was a problem hiding this comment.
Pull Request Overview
This PR fixes an issue where the DECKARD_MONGODB_DATABASE environment variable was ignored due to the getter methods always returning default values. It updates the getter methods in internal/config/viper_config.go to properly check both the main key and its aliases for non-default values, and adds comprehensive tests to verify that environment variable overrides work as expected.
- Updated getter methods to prioritize environment variable values (non-default) using a new helper function.
- Added unit tests for string, boolean, and integer configurations.
- Minor update to .gitignore to exclude "*.srl" files.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| internal/service/cert/.gitignore | Added "*.srl" to ignore related certificate files. |
| internal/config/viper_config.go | Refactored getter methods using a new generic helper (getWithFallback). |
| internal/config/mongo_database_test.go | Added tests to ensure environment variable overrides are correctly applied. |
Comments suppressed due to low confidence (2)
internal/config/viper_config.go:38
- [nitpick] Consider renaming 'getWithFallback' to a more descriptive name (e.g., 'getValueWithOverride') to clearly express its purpose.
func getWithFallback[T comparable](config *ViperConfigKey, defaultVal T, keyGetter func(string) T) T {
internal/service/cert/.gitignore:2
- [nitpick] If '*.srl' files are generated as part of the certificate process, consider adding a comment in the .gitignore to explain their purpose for future maintainers.
+*.srl
| defaultVal := time.Duration(0) | ||
| if val, ok := config.GetDefault().(string); ok { | ||
| parsed, err := time.ParseDuration(val) | ||
| if err == nil { |
There was a problem hiding this comment.
Consider handling the error returned by time.ParseDuration in GetDuration to improve robustness and maintainability, even if it's only logging a warning.
| if err == nil { | |
| if err != nil { | |
| // Log a warning about the invalid duration value | |
| viper.Logger().Warnf("Invalid duration value for key '%s': %s. Using default value.", config.GetKey(), val) | |
| } else { |
Problem
The
DECKARD_MONGODB_DATABASEenvironment variable was being ignored and the default valuedeckardwas always used, even when the environment variable was explicitly set.Root Cause
The issue was in the
ViperConfigKeygetter methods (Get(),GetBool(),GetInt(),GetDuration()). The logic was:true(due to defaults being set)Since
viper.SetDefault()was called for all main keys during configuration,viper.IsSet(mainKey)always returnedtrue, preventing the methods from ever checking the aliases where environment variables likeDECKARD_MONGODB_DATABASEare mapped.Solution
Modified all getter methods to properly prioritize environment variables over defaults by:
Example
Before this fix:
After this fix:
Testing
Added comprehensive tests covering:
DECKARD_MONGODB_DATABASE,DECKARD_MONGODB_COLLECTION)DECKARD_MONGODB_SSL)DECKARD_MONGODB_MAX_POOL_SIZE)Fixes #51.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.