Skip to content

Commit

Permalink
Update environment variable naming convention (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxisam committed Dec 13, 2023
1 parent 3030ba3 commit e27d75f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
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

0 comments on commit e27d75f

Please sign in to comment.