-
Notifications
You must be signed in to change notification settings - Fork 1
Transform terraform hcl var #13
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| #!/bin/bash | ||
|
|
||
| install_jq(){ | ||
| OS=$(uname -s) | ||
| if [[ "$OS" == "Darwin" ]]; then | ||
| OS="macos" | ||
| elif [[ "$OS" == "Linux" ]]; then | ||
| OS="linux" | ||
| else | ||
| echo "Unsupported OS: $OS" | ||
| exit 1 | ||
| fi | ||
|
|
||
| ARCH=$(uname -m) | ||
taherkk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| JQ_BIN="/tmp/jq" | ||
| url="https://github.com/jqlang/jq/releases/download/jq-1.8.1/jq-${OS}-${ARCH}" | ||
taherkk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| curl -L -o $JQ_BIN $url | ||
| chmod +x $JQ_BIN | ||
|
|
||
| } | ||
|
|
||
| install_hcl2json(){ | ||
| OS=$(uname -s) | ||
| if [[ "$OS" == "Darwin" ]]; then | ||
| OS="darwin" | ||
| elif [[ "$OS" == "Linux" ]]; then | ||
| OS="linux" | ||
| else | ||
| echo "Unsupported OS: $OS" | ||
| exit 1 | ||
| fi | ||
|
|
||
| ARCH=$(uname -m) | ||
taherkk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| HCL2JSON_BIN="/tmp/hcl2json" | ||
|
|
||
| url="https://github.com/tmccombs/hcl2json/releases/download/v0.6.7/hcl2json_${OS}_${ARCH}" | ||
taherkk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| echo $url | ||
| curl -L -o $HCL2JSON_BIN $url | ||
| chmod +x $HCL2JSON_BIN | ||
| } | ||
|
|
||
|
|
||
| INPUT_FILE_JSON="$1" | ||
| if [ -z "$INPUT_FILE_JSON" ]; then | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's switch from single Also, if we require file as input, check with: |
||
| echo "Usage: $0 <input_file.json>" | ||
| exit 1 | ||
| fi | ||
|
|
||
taherkk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| install_jq | ||
| install_hcl2json | ||
|
|
||
| # Read entire JSON array into a variable | ||
| json_data=$(cat "$INPUT_FILE_JSON") | ||
|
|
||
| # Use jq to get the length of array | ||
| length=$($JQ_BIN length <<<"$json_data") | ||
|
|
||
| # Create a temporary file to store updated objects | ||
| tmpfile=$(mktemp) | ||
|
|
||
| for ((i=0; i<length; i++)); do | ||
| # Extract ith object | ||
| obj=$($JQ_BIN ".[$i]" <<<"$json_data") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make sure to wrap all |
||
|
|
||
| JSON_PATH=".VCSConfig.iacInputData.data" | ||
|
|
||
| # Extract the value at JSON_PATH from the object | ||
| val=$($JQ_BIN -c "$JSON_PATH" <<<"$obj") | ||
|
|
||
| # If val is null or not an object, skip | ||
| if [[ "$val" == "null" || $($JQ_BIN 'type' <<<"$val") != "\"object\"" ]]; then | ||
| echo "$obj" >> "$tmpfile" | ||
|
|
||
| continue | ||
| fi | ||
|
|
||
| # Initialize new_val as empty object | ||
| new_val="{}" | ||
|
|
||
| # Loop over key-value pairs in val | ||
| keys=$($JQ_BIN -r 'keys[]' <<<"$val") | ||
| for key in $keys; do | ||
| # Get the string value for the key | ||
| value=$($JQ_BIN --arg k "$key" '.[$k]' <<<"$val" | sed 's/\\"/"/g') | ||
| value="${value%\"}" | ||
| value="${value#\"}" | ||
| value="temp = $value" | ||
|
|
||
| # Heuristic: if value contains '=', treat as HCL string | ||
| if [[ "$value" == *"="* ]]; then | ||
| # Convert HCL to JSON using hcl2json | ||
| parsed=$(echo -e "$value" | $HCL2JSON_BIN | $JQ_BIN -c '.temp') | ||
| if [[ $? -eq 0 && "$parsed" != "" ]]; then | ||
| # Add parsed json as the key's value | ||
| new_val=$($JQ_BIN --arg k "$key" --argjson v "$parsed" '. + {($k): $v}' <<<"$new_val") | ||
| else | ||
| # If parse fails, keep original string | ||
| echo "parsing failed: $value" | ||
| new_val=$($JQ_BIN --arg k "$key" --arg v "$value" '. + {($k): $v}' <<<"$new_val") | ||
| fi | ||
| else | ||
| # Not HCL, keep as string | ||
| new_val=$($JQ_BIN --arg k "$key" --arg v "$value" '. + {($k): $v}' <<<"$new_val") | ||
| fi | ||
| done | ||
|
|
||
| # Update the object by assigning new_val back at JSON_PATH | ||
| updated_obj=$($JQ_BIN --argjson nv "$new_val" "$JSON_PATH = \$nv" <<<"$obj") | ||
|
|
||
| # Save updated object | ||
| echo "$updated_obj" >> "$tmpfile" | ||
| done | ||
|
|
||
| # Combine updated objects into an array and overwrite the original file | ||
| $JQ_BIN -s '.' "$tmpfile" > "$INPUT_FILE_JSON" | ||
|
|
||
| rm "$tmpfile" $HCL2JSON_BIN $JQ_BIN | ||
taherkk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,4 +15,4 @@ terraform { | |
| version = "~> 3.2.1" | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.