Skip to content

fix(variable): use StringArrayVar to preserve special characters in --key values#221

Closed
stantheman0128 wants to merge 1 commit intozeabur:mainfrom
stantheman0128:fix/variable-string-to-string-flag
Closed

fix(variable): use StringArrayVar to preserve special characters in --key values#221
stantheman0128 wants to merge 1 commit intozeabur:mainfrom
stantheman0128:fix/variable-string-to-string-flag

Conversation

@stantheman0128
Copy link
Copy Markdown
Contributor

@stantheman0128 stantheman0128 commented Apr 10, 2026

Summary

  • Fixes variable create and variable update --key flag silently truncating or emptying values that contain ${}, commas, or other special characters
  • Switches from Cobra's StringToStringVarP (which uses an internal CSV parser) to StringArrayVarP with manual SplitN on the first =
  • Values like DATABASE_URL=${POSTGRESQL.POSTGRES_CONNECTION_STRING} now work correctly

Root Cause

StringToStringVarP internally uses Go's encoding/csv reader to parse KEY=VALUE pairs. This parser treats ${}, commas, and quotes as special characters, causing values to be silently truncated or emptied.

Changes

File Change
internal/cmd/variable/create/create.go Replace StringToStringVarP with StringArrayVarP + parseRawKeys()
internal/cmd/variable/update/update.go Same fix applied

Usage (unchanged)

# Before: value gets truncated to ""
zeabur variable create --id <id> -k 'DATABASE_URL=${POSTGRESQL.POSTGRES_CONNECTION_STRING}' -i=false

# After: value preserved correctly
zeabur variable create --id <id> -k 'DATABASE_URL=${POSTGRESQL.POSTGRES_CONNECTION_STRING}' -i=false

# Multiple variables (now use separate -k flags instead of comma-separated)
zeabur variable create --id <id> -k 'KEY1=value1' -k 'KEY2=value,with,commas' -i=false

Test plan

  • Verify variable create -k 'KEY=${REF}' preserves ${REF} in the value
  • Verify variable create -k 'KEY=val,with,commas' preserves commas
  • Verify variable update -k 'KEY=new_value' works correctly
  • Verify multiple -k flags work: -k 'A=1' -k 'B=2'
  • Verify interactive mode still works as before

Fixes #201

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Enhanced --key/-k flag in variable create and update commands to accept repeatable KEY=VALUE format.
    • Improved handling of special characters (including =) within variable values.
  • Bug Fixes

    • Added validation to reject malformed key inputs and provide clearer error messages.

…key flag

StringToStringVar uses an internal CSV parser that cannot handle values
containing ${}, commas, or other special characters. This causes values
like ${POSTGRESQL.POSTGRES_CONNECTION_STRING} to be truncated or emptied.

Switch to StringArrayVar and manually split each entry on the first "="
only, preserving arbitrary characters in values.

Fixes zeabur#201

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 10, 2026

Caution

Review failed

Pull request was closed or merged during review

Walkthrough

The variable create and variable update commands were refactored to handle --key flag input as a repeatable string array instead of a map type. A new parsing function manually splits each KEY=VALUE entry on the first = delimiter, preserving special characters like ${} in values, and validates for malformed entries.

Changes

Cohort / File(s) Summary
Variable Key Flag Parsing Refactor
internal/cmd/variable/create/create.go, internal/cmd/variable/update/update.go
Changed --key flag from StringToStringVarP to StringArrayVarP with new parseRawKeys() function that manually splits each entry on the first = only, supporting values with special characters. Added validation for empty keys and missing delimiters; parsed keys flow through both interactive and non-interactive execution paths.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: replacing StringToStringVar with StringArrayVar to preserve special characters in --key values, which directly addresses the root cause of the bug.
Linked Issues check ✅ Passed All coding objectives from issue #201 are met: flag changed from StringToStringVarP to StringArrayVarP, manual parsing with SplitN on first '=' only, validation for malformed input, and special character preservation in both create and update commands.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the --key flag parsing issue in the variable create and update commands; no unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@stantheman0128
Copy link
Copy Markdown
Contributor Author

Closing as duplicate of #206 which addresses the same issue. Sorry for the overlap!

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.

variable create --key flag cannot handle values containing ${} (Cobra StringToStringVar limitation)

1 participant