Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix Duplication of Flattened JSON Keys in .env File During Multiple Runs #1618

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions scripts/parse_env.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function Flatten-Json {
return $flattened
}

$output = @()
$output = @{}

foreach ($line in Get-Content -Path $envFile) {
Write-Host "Processing line: $line"
Expand Down Expand Up @@ -65,17 +65,22 @@ foreach ($line in Get-Content -Path $envFile) {

# Add each flattened key-value pair to the output
foreach ($flattenedKey in $flattenedJson.Keys) {
$output += "$flattenedKey=`"$($flattenedJson[$flattenedKey])`""
$output[$flattenedKey] = "`"$($flattenedJson[$flattenedKey])`""
}
} catch {
Write-Error "Failed to parse JSON for key: $key, value: $value"
}
} else {
# Keep non-JSON key-value pairs as-is
$output += "$key=$value"
$output[$key] = $value
}
}

# Convert the hashtable to an array of strings in the format KEY=VALUE, sorted by key name
$output = $output.GetEnumerator() |
Sort-Object -Property Key |
ForEach-Object { "$($_.Key)=$($_.Value)" }

# Write the processed content back to the .env file
$output | Set-Content -Path $envFile -Force
Write-Host "Flattened .env file written back to: $envFile"
22 changes: 15 additions & 7 deletions scripts/parse_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ flatten_json() {
echo "$json_object" | jq -r "to_entries | .[] | \"${prefix}\(.key | ascii_upcase)=\(.value | @sh)\""
}

output=()
declare -A output

# Read the .env file line by line
while IFS= read -r line; do
Expand Down Expand Up @@ -57,19 +57,27 @@ while IFS= read -r line; do
"AZURE_KEY_VAULT_INFO") prefix="AZURE_KEY_VAULT_" ;;
esac
# Flatten the JSON object
flattened_json=$(flatten_json "$prefix" "$json_object")
output+=("$flattened_json")
while IFS= read -r flattened_line; do
flattened_key=$(echo "$flattened_line" | cut -d'=' -f1)
flattened_value=$(echo "$flattened_line" | cut -d'=' -f2-)
output["$flattened_key"]="$flattened_value"
done < <(flatten_json "$prefix" "$json_object")
else
echo "Failed to parse JSON for key: $key, value: $value"
fi
;;
*)
# Keep non-JSON key-value pairs as-is
output+=("$line")
output["$key"]="$value"
;;
esac
done < "$envFile"

# Write the processed content back to the .env file
printf "%s\n" "${output[@]}" > "$envFile"
echo "Flattened .env file written back to: $envFile"
# Write the processed content back to the .env file, sorted by key
{
for key in $(printf "%s\n" "${!output[@]}" | sort); do
echo "$key=${output[$key]}"
done
} > "$envFile"

echo "Flattened and sorted .env file written back to: $envFile"
Loading