Skip to content

Commit b4043ff

Browse files
nsheapsclaudejack-nsheaps[bot]
authored
fix: handle multiline values in field extraction (#8)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Jack Oat <jack-nsheaps[bot]@users.noreply.github.com>
1 parent 783c425 commit b4043ff

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

bin/op-exec

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ is_op_ref() {
6363
resolve_ref() {
6464
local ref="$1"
6565
debug "Resolving: $ref"
66-
op read "$ref" 2>/dev/null || echo ""
66+
op read "$ref" || true
6767
}
6868

6969
# -----------------------------------------------------------------------------
@@ -119,9 +119,10 @@ fetch_fields() {
119119
debug "Fetching: vault=$vault item=$item"
120120

121121
# Get item as JSON and extract fields
122-
# Filter to STRING and CONCEALED types, output as label<TAB>value
123-
op item get "$item" --vault "$vault" --format json 2>/dev/null |
124-
jq -r '.fields[]? | select(.type == "STRING" or .type == "CONCEALED") | "\(.label)\t\(.value // "")"'
122+
# Filter to STRING and CONCEALED types, output one compact JSON object per line.
123+
# Using jq -c avoids jq -r converting escaped \n sequences to real newlines.
124+
op item get "$item" --vault "$vault" --format json |
125+
jq -c '.fields[]? | select(.type == "STRING" or .type == "CONCEALED") | {label: .label, value: (.value // "")}'
125126
}
126127

127128
# -----------------------------------------------------------------------------
@@ -208,10 +209,13 @@ main() {
208209
# Fetch and process fields
209210
declare -A env_vars
210211

211-
while IFS=$'\t' read -r label value; do
212+
while IFS= read -r line; do
213+
[[ -z "$line" ]] && continue
214+
local label value env_name
215+
label=$(echo "$line" | jq -r '.label')
216+
value=$(echo "$line" | jq -r '.value')
212217
[[ -z "$label" ]] && continue
213218

214-
local env_name
215219
env_name=$(to_env_name "$label")
216220

217221
debug "Processing: $label -> $env_name"
@@ -226,8 +230,11 @@ main() {
226230
# If no command provided, just print exports
227231
if [[ $# -eq 0 ]]; then
228232
for name in "${!env_vars[@]}"; do
229-
# Escape value for shell
230-
printf 'export %s=%q\n' "$name" "${env_vars[$name]}"
233+
# Use heredoc wrapper for readability, especially for multiline values (e.g. PEM keys)
234+
echo "export ${name}=\$(cat <<'__OPEXEC_EOF__'"
235+
echo "${env_vars[$name]}"
236+
echo "__OPEXEC_EOF__"
237+
echo ")"
231238
done
232239
exit 0
233240
fi

0 commit comments

Comments
 (0)