feat: add --export flag to azd env get-values for shell sourcing#7364
feat: add --export flag to azd env get-values for shell sourcing#7364
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new --export mode to azd env get-values so the command can emit shell-ready export KEY="VALUE" lines intended for direct sourcing/eval in POSIX-like shells, addressing the workflow described in #4384.
Changes:
- Introduces
--exportflag forazd env get-valuesand a helper to format/escape values asexport KEY="VALUE". - Adds table-driven unit tests for exported output vs existing dotenv output.
- Updates Fig spec and usage snapshots to reflect the new flag.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| cli/azd/cmd/env.go | Adds --export flag, help text, and writeExportedEnv() implementation used by env get-values. |
| cli/azd/cmd/env_get_values_test.go | Adds unit tests validating export formatting and escaping behavior. |
| cli/azd/cmd/testdata/TestUsage-azd-env-get-values.snap | Updates usage snapshot to include --export flag. |
| cli/azd/cmd/testdata/TestFigSpec.ts | Updates generated Fig completion spec with the new --export option. |
Comments suppressed due to low confidence (1)
cli/azd/cmd/env.go:1385
--exportoutput is not safe toevalwhen an environment key is not a valid shell identifier. Keys are inserted unquoted intoexport %s=..., andazd env setcurrently accepts arbitrary keys, so a crafted key containing shell metacharacters/command substitutions could lead to command execution or a syntax error. Consider validating keys (e.g., POSIX identifier regex) and returning an error (or skipping with a warning) when a key is invalid before writing the export line.
keys := slices.Sorted(maps.Keys(values))
for _, key := range keys {
val := values[key]
escaped := strings.NewReplacer(
`\`, `\\`,
`"`, `\"`,
`$`, `\$`,
"`", "\\`",
"\n", `\n`,
"\r", `\r`,
).Replace(val)
line := fmt.Sprintf("export %s=\"%s\"\n", key, escaped)
if _, err := io.WriteString(writer, line); err != nil {
cli/azd/cmd/env.go
Outdated
|
|
||
| // writeExportedEnv writes environment variables in shell-ready | ||
| // format (export KEY="VALUE") to the given writer. Values are | ||
| // double-quoted with embedded double quotes and backslashes escaped. |
There was a problem hiding this comment.
The doc comment for writeExportedEnv says only double quotes and backslashes are escaped, but the implementation also escapes $, backticks, newlines, and carriage returns. Please update the comment to accurately describe the escaping behavior (or adjust the escaping to match the comment).
| // double-quoted with embedded double quotes and backslashes escaped. | |
| // double-quoted, with embedded double quotes, backslashes, dollar signs, | |
| // backticks, newlines, and carriage returns escaped. |
spboyer
left a comment
There was a problem hiding this comment.
All 4 review comments addressed:
- ✅
t.Context()— Replacedcontext.Background()witht.Context(), removed unused import. - ✅ Security test cases — Added tests for backslashes (
C:\path\to\dir), backticks + command substitution, and carriage returns. - ✅ Mutual exclusion —
--exportand--outputnow return an error when both specified. Added test. - ✅ Doc comment — Updated
writeExportedEnvcomment to list all escaped characters.
Please resolve the threads.
8f59373 to
877ac34
Compare
Add --export flag that outputs environment variables in shell-ready format (export KEY="VALUE" for bash/zsh). This enables easy shell integration: eval "$(azd env get-values --export)" Escapes backslashes, double quotes, dollar signs, backticks, newlines, and carriage returns for safe eval usage. Returns error when combined with --output flag (mutually exclusive). Fixes #4384 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dcaa317 to
50cc7be
Compare
Azure Dev CLI Install InstructionsInstall scriptsMacOS/Linux
bash: pwsh: WindowsPowerShell install MSI install Standalone Binary
MSI
Documentationlearn.microsoft.com documentationtitle: Azure Developer CLI reference
|
Summary
Fixes #4384
Adds
--exportflag toazd env get-valuesthat outputs environment variables in shell-ready format for direct sourcing.Usage
Changes
cmd/env.go— Added--exportflag andwriteExportedEnv()helper with proper escaping of\\,",$, backticks, newlines, and carriage returnscmd/env_get_values_test.go— Table-driven tests covering basic values, special characters, newlines, empty values, and non-export modeSecurity
The
writeExportedEnv()function escapes all shell-dangerous characters:\\→\\\\"→\\"$→\\$\n→\\n(prevents newline injection)\r→\\rOutput is safe to
evalwithout risk of command execution from env var values.