Skip to content

feat: accept snake_case environment variable names - #298

Open
Abhinesh Jha (Abhineshhh) wants to merge 2 commits into
Azure:mainfrom
Abhineshhh:feat/248-snake-case-env-vars
Open

feat: accept snake_case environment variable names#298
Abhinesh Jha (Abhineshhh) wants to merge 2 commits into
Azure:mainfrom
Abhineshhh:feat/248-snake-case-env-vars

Conversation

@Abhineshhh

Copy link
Copy Markdown

Summary

Implements support for snake_case environment variable names while fully preserving the existing concatenated names for backward compatibility.

Closes / fixes: #248

Problem

Environment variables used a hard-to-read concatenated form (e.g. MPF_SUBSCRIPTIONID). Users requested conventional snake_case (e.g. MPF_SUBSCRIPTION_ID) without breaking existing scripts and CI.

Solution

  • Derive both env names from each Cobra flag:
    • Legacy: MPF_ + upper(flagName) → MPF_SUBSCRIPTIONID (unchanged behavior)
    • Snake_case: MPF_ + upper(camelToSnake(flagName)) → MPF_SUBSCRIPTION_ID
  • Bind both via Viper BindEnv for every flag (global, ARM, Bicep, Terraform)
  • Precedence: CLI flags > legacy env > snake_case env
  • Document preferred + legacy forms in the flag reference
  • E2E tests accept either form via a small mpfEnv() helper (legacy first)

Examples

Flag Preferred env Legacy env (still works)
subscriptionID MPF_SUBSCRIPTION_ID MPF_SUBSCRIPTIONID
spClientID MPF_SP_CLIENT_ID MPF_SPCLIENTID
templateFilePath MPF_TEMPLATE_FILE_PATH MPF_TEMPLATEFILEPATH
tfPath MPF_TF_PATH MPF_TFPATH

Changes

Area Files
CLI binding cmd/envbinding.go, cmd/rootCmd.go
Unit tests cmd/envbinding_test.go
Docs docs/commandline-flags-and-env-variables.md, README.md, AGENTS.md
E2E alignment e2eTests/env_helpers.go + e2e test files

Backward compatibility

  • Existing MPF_SUBSCRIPTIONID, MPF_TFPATH, etc. continue to work
  • If both forms are set, legacy wins so current deployments are not surprised
  • No flag renames, no dependency changes, no changes to deploy/permission logic under pkg/

Test plan

  • go test ./cmd/ -count=1
  • go test ./pkg/... -count=1
  • go test ./e2eTests/ -count=1 (compile / unit path; live Azure e2e not run in this environment)
  • go vet ./cmd/ ./e2eTests/
  • gofmt clean on touched Go files
  • Unit coverage for: snake_case only, legacy only, both set (legacy wins), CLI overrides env, terraform flags, initializeConfig with root+arm flags

References

Support preferred snake_case env vars (e.g. MPF_SUBSCRIPTION_ID) while
keeping legacy concatenated names (e.g. MPF_SUBSCRIPTIONID) for backward
compatibility. Bind both forms via Viper for all CLI flags, prefer legacy
when both are set, and document the dual naming. E2E helpers accept either
form so tests stay aligned with the CLI.

Fixes Azure#248
@Abhineshhh
Abhinesh Jha (Abhineshhh) requested a review from a team as a code owner July 14, 2026 19:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support for snake_case environment variables across the azmpf CLI (via Cobra/Viper), while preserving and prioritizing the existing legacy concatenated env var names for backward compatibility (per issue #248).

Changes:

  • Implement dual env var binding per flag (legacy + snake_case) with legacy precedence via centralized binding logic in cmd/envbinding.go.
  • Add unit coverage for camel→snake conversion, env name derivation, and binding precedence/behavior.
  • Align docs and E2E tests to accept either env var form (legacy-first lookup helper).

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated no comments.

Show a summary per file
File Description
README.md Updates examples to prefer snake_case env vars while noting legacy compatibility.
docs/commandline-flags-and-env-variables.md Documents snake_case vs legacy naming rules and lists both env var forms per flag.
cmd/rootCmd.go Switches config initialization to use the new dual-env binding helper.
cmd/envbinding.go Introduces camel→snake conversion, env name derivation, and dual BindEnv behavior.
cmd/envbinding_test.go Adds unit tests for conversion, env name generation, and precedence (CLI > legacy env > snake_case).
e2eTests/env_helpers.go Adds mpfEnv() helper to read legacy-first, then snake_case env vars in tests.
e2eTests/e2eTerraformWithImportAndTargeting_test.go Updates terraform path env lookup to accept both env var forms.
e2eTests/e2eTerraformInvalid_test.go Updates terraform path env lookup to accept both env var forms.
e2eTests/e2eTerraformAuthPermissionMismatch_test.go Updates terraform path env lookup to accept both env var forms.
e2eTests/e2eTerraformAuthorizationRequestDenied_test.go Updates terraform path env lookup to accept both env var forms.
e2eTests/e2eTerraform_test.go Updates terraform path env lookup to accept both env var forms.
e2eTests/e2eBicepInvalid_test.go Updates bicep exec path env lookup to accept both env var forms.
e2eTests/e2eBicep_test.go Updates bicep exec path env lookup to accept both env var forms.
e2eTests/e2eArm_test.go Updates ARM E2E env var reads to accept both legacy and snake_case forms.
AGENTS.md Updates agent instructions/examples to use snake_case env vars (preferred).

@maniSbindra

Copy link
Copy Markdown
Contributor

Abhinesh Jha (@Abhineshhh) Thanks for the PR. It looks good. Can you look that linting PR check which is failing

@maniSbindra Mani Bindra (maniSbindra) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Abhinesh Jha (@Abhineshhh) Thanks for the PR. It looks good. Can you look that linting PR check which is failing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants