Skip to content

Commit e27d75f

Browse files
authored
Update environment variable naming convention (#98)
1 parent 3030ba3 commit e27d75f

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

.document/BACKUP_PLAN.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,11 @@ With uri being set, host/port/username/password/database will be ignored. [Read
140140
target:
141141
uri: "mongodb://admin:secret@localhost:27017/test?authSource=admin&ssl=true"
142142
```
143+
144+
## Overriding configuration with environment variables
145+
146+
All configuration options can be overridden with environment variables. The format is `PLAN_NAME__SECTION_KEY`.
147+
148+
(all uppercase, double underscore, `__`, as separator between plan name and section key)
149+
150+
For example, to override the `scheduler.cron` option, you can set the `BACKUP_PLAN__SCHEDULER_CRON` environment variable.

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ jobs:
9999
docker run -d \
100100
--name mgob \
101101
--network "host" \
102-
-e MONGO-TEST_AZURE_CONNECTIONSTRING="$AZURE_CONNECTIONSTRING" \
102+
-e MONGO_TEST__AZURE_CONNECTIONSTRING="$AZURE_CONNECTIONSTRING" \
103103
-v ${{ github.workspace }}/test/gh-actions:/config \
104104
-v ${{ github.workspace }}/test/backups:/storage \
105105
${{ github.repository }}:${{ env.APP_VERSION }}.${{ github.run_number }}

pkg/config/plan.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ func LoadPlans(dir string) ([]Plan, error) {
206206
func setupViperEnv(planName string) {
207207
viper.SetConfigType("yaml")
208208
// set upper case plan name as env prefix
209-
viper.SetEnvPrefix(strings.ToUpper(planName))
209+
envPrefix := strings.ReplaceAll(planName, "-", "_")
210+
viper.SetEnvPrefix(envPrefix + "_") // will be uppercased automatically
210211
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
211212
viper.AutomaticEnv()
212213
}

pkg/config/plan_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestLoadPlan(t *testing.T) {
2121
// set env var for test
2222
testConnectionString := "test-connetion-string"
2323
testPlanName := "mongo-test"
24-
os.Setenv(fmt.Sprintf("%s_%s_%s", strings.ToUpper(testPlanName), "AZURE", "CONNECTIONSTRING"), testConnectionString)
24+
os.Setenv(fmt.Sprintf("%s__%s_%s", strings.ToUpper(strings.Replace(testPlanName, "-", "_", -1)), "AZURE", "CONNECTIONSTRING"), testConnectionString)
2525
dir := getDir(t)
2626
plan, err := LoadPlan(dir, testPlanName)
2727

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

4747
plans, err := LoadPlans(dir)

0 commit comments

Comments
 (0)