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

Update environment variable naming convention #98

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .document/BACKUP_PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,11 @@ With uri being set, host/port/username/password/database will be ignored. [Read
target:
uri: "mongodb://admin:secret@localhost:27017/test?authSource=admin&ssl=true"
```

## Overriding configuration with environment variables

All configuration options can be overridden with environment variables. The format is `PLAN_NAME__SECTION_KEY`.

(all uppercase, double underscore, `__`, as separator between plan name and section key)

For example, to override the `scheduler.cron` option, you can set the `BACKUP_PLAN__SCHEDULER_CRON` environment variable.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ jobs:
docker run -d \
--name mgob \
--network "host" \
-e MONGO-TEST_AZURE_CONNECTIONSTRING="$AZURE_CONNECTIONSTRING" \
-e MONGO_TEST__AZURE_CONNECTIONSTRING="$AZURE_CONNECTIONSTRING" \
-v ${{ github.workspace }}/test/gh-actions:/config \
-v ${{ github.workspace }}/test/backups:/storage \
${{ github.repository }}:${{ env.APP_VERSION }}.${{ github.run_number }}
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ func LoadPlans(dir string) ([]Plan, error) {
func setupViperEnv(planName string) {
viper.SetConfigType("yaml")
// set upper case plan name as env prefix
viper.SetEnvPrefix(strings.ToUpper(planName))
envPrefix := strings.ReplaceAll(planName, "-", "_")
viper.SetEnvPrefix(envPrefix + "_") // will be uppercased automatically
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
}
4 changes: 2 additions & 2 deletions pkg/config/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestLoadPlan(t *testing.T) {
// set env var for test
testConnectionString := "test-connetion-string"
testPlanName := "mongo-test"
os.Setenv(fmt.Sprintf("%s_%s_%s", strings.ToUpper(testPlanName), "AZURE", "CONNECTIONSTRING"), testConnectionString)
os.Setenv(fmt.Sprintf("%s__%s_%s", strings.ToUpper(strings.Replace(testPlanName, "-", "_", -1)), "AZURE", "CONNECTIONSTRING"), testConnectionString)
dir := getDir(t)
plan, err := LoadPlan(dir, testPlanName)

Expand All @@ -41,7 +41,7 @@ func TestLoadPlans(t *testing.T) {
// set env var for test
testConnectionString := "test-connetion-string"
testPlanName := "mongo-test"
os.Setenv(fmt.Sprintf("%s_%s_%s", strings.ToUpper(testPlanName), "AZURE", "CONNECTIONSTRING"), testConnectionString)
os.Setenv(fmt.Sprintf("%s__%s_%s", strings.ToUpper(strings.Replace(testPlanName, "-", "_", -1)), "AZURE", "CONNECTIONSTRING"), testConnectionString)
dir := getDir(t)

plans, err := LoadPlans(dir)
Expand Down
Loading