From 7ba912c20394c94bce4fed3ec5be18f8e072a8e0 Mon Sep 17 00:00:00 2001 From: Sam Lin Date: Tue, 12 Dec 2023 15:23:20 -0600 Subject: [PATCH] Update environment variable naming convention --- .document/BACKUP_PLAN.md | 8 ++++++++ .github/workflows/build.yml | 2 +- pkg/config/plan.go | 3 ++- pkg/config/plan_test.go | 4 ++-- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/.document/BACKUP_PLAN.md b/.document/BACKUP_PLAN.md index cd05e04..39c64fd 100644 --- a/.document/BACKUP_PLAN.md +++ b/.document/BACKUP_PLAN.md @@ -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. diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cd2cae8..6f5077e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 }} diff --git a/pkg/config/plan.go b/pkg/config/plan.go index 264294f..a7fa00d 100644 --- a/pkg/config/plan.go +++ b/pkg/config/plan.go @@ -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() } diff --git a/pkg/config/plan_test.go b/pkg/config/plan_test.go index 45bbbea..fe44580 100644 --- a/pkg/config/plan_test.go +++ b/pkg/config/plan_test.go @@ -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) @@ -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)