Issue
I have the following Variables in my config file:
tags = {
environment = "development",
provider = "ServiceOne",
ansible_role = "nfs-role",
comment = "mysql"
}
line = "b01"
my_lb_ports = "[53, 80, 443, 389, 636, 3268, 3269, 6443]"
dns_servers = "[\"8.8.8.8\", \"9.9.9.9\"]"
For my Pipeline i need to convert it to the following:
tfh pushvars -overwrite-all -dry-run false -hcl-var "tags={environment=\"development\", provider=\"ServiceOne\", ansible_role=\"nfs-rolep\",comment= \"mysql\"}"
tfh pushvars -overwrite-all -dry-run false -hcl-var "line=\"b01\""
tfh pushvars -overwrite-all -dry-run false -hcl-var "my_lb_ports=[\"53\", \"80\", \"443\", \"389\", \"636\", \"3268\", \"3269\", \"6443\"]"
tfh pushvars -overwrite-all -dry-run false -hcl-var "dns_servers=[\"8.8.8.8\", \"9.9.9.9\"]"
Following script was created with support from @Ed Morton, but it still not identifies all patterns:
#!/usr/bin/env bash
(( $# == 2 )) || { echo "==> Usage: ./${0##*/} <<INPUT_FILE>> <<OUTPUT_FILE>>"; exit 1; }
vars_file="$1"
output_file="$2"
awk '
BEGIN {
ORS = ""
print "tfh pushvars -overwrite-all -dry-run false -hcl-var \""
}
NF && !/^#/ {
gsub(/[[:space:]]/,"")
gsub(/"/,"\\\\&")
print
}
END {
print "\"\n"
}
' "$vars_file" > "$output_file"
awk -F'=' '{ gsub( /\x22/ ,"\x5c\\&", $2 ); gsub( / / ,"", $1 ); gsub( / / ,"", $2 ); print "tfh pushvars -overwrite-all -dry-run false -hcl-var \x22" $1 "=" $2 "\x22"}' "$vars_file" >> "$output_file"
Solution
Here's a good start, you'll need to figure out how to adjust the "
s in the lines for output:
$ cat tst.sh
#!/usr/bin/env bash
(( $# == 2 )) || { echo "==> Usage: ./${0##*/} <<INPUT_FILE>> <<OUTPUT_FILE>>"; exit 1; }
vars_file="$1"
output_file="$2"
awk '
BEGIN {
ORS = ""
beg = "tfh pushvars -overwrite-all -dry-run false -hcl-var \""
end = "\"\n"
}
/^[[:alnum:]_]+[[:space:]]*=/ {
gotBlockBeg = 1
gotBlockEnd = ( $NF == "{" ? 0 : 1 )
}
gotBlockBeg {
block = block $0
if ( $NF == "}" ) {
gotBlockEnd = 1
}
}
gotBlockEnd {
gsub(/[[:space:]]/,"",block)
gsub(/"/,"\\\\&",block)
print beg block end
gotBlockBeg = gotBlockEnd = 0
block = ""
}
' "$vars_file" > "$output_file"
$ ./tst.sh file foo
$ cat foo
tfh pushvars -overwrite-all -dry-run false -hcl-var "tags={environment=\"development\",provider=\"ServiceOne\",ansible_role=\"nfs-role\",comment=\"mysql\"}"
tfh pushvars -overwrite-all -dry-run false -hcl-var "line=\"b01\""
tfh pushvars -overwrite-all -dry-run false -hcl-var "my_lb_ports=\"[53,80,443,389,636,3268,3269,6443]\""
tfh pushvars -overwrite-all -dry-run false -hcl-var "dns_servers=\"[\\"8.8.8.8\\",\\"9.9.9.9\\"]\""
Add debugging print
statements anywhere you don't understand what's happening in the awk script or run it using the gawk debugger (gawk -D ...
).
Answered By - Ed Morton Answer Checked By - David Goodson (WPSolving Volunteer)