harbor run -t openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-6055harbor download openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-6055Task Description:
Implement a Bash script that processes command-line arguments in a specified format to update or add variables in a Terraform configuration file named terraform.tfvars. The script should be saved as /app/update_tfvars.sh.
Functionality Requirements:
- and must be followed by a corresponding value.terraform.tfvars file located in the current directory. If a variable (flag) already exists in the file, its value should be updated. If it does not exist, a new line should be appended with the new variable and its value.Expected Inputs:
-key1 value1 -key2 value2 ...- prefix.Expected Outputs:
terraform.tfvars file by either updating existing entries or adding new ones based on the provided command-line arguments.ERROR: Missing value for variable <key>, and exit with status code 1.ERROR: Received two values for variable <key> or variable not properly passed as flag, and exit with status code 1.File Location:
/app/update_tfvars.sh. The script should ensure that it can create or modify the terraform.tfvars file in the directory it is run from.The following test script will be used to verify your solution:
#!/bin/bash
tfvars="./terraform.tfvars"
red='\e[0;31m'
list_of_args=$(echo "$@" | sed 's/=/ /g')
pingpong="pong"
for arg in $list_of_args
do
if [[ "$arg" == "-"* ]]; then
if [[ "$pingpong" == "ping" ]]; then
echo -e "${red}ERROR: Missing value for variable $key"
exit 1
fi
pingpong="ping"
key=$(echo $arg | sed 's/-//g')
else
if [[ "$pingpong" == "pong" ]]; then
echo -e "${red}ERROR: Received two values for variable $key or variable not properly passed as flag"
exit 1
fi
pingpong="pong"
value=$arg
grep -qe "$key *= *" $tfvars
if [ $? -eq 0 ]
then
sed -i "s|\($key *= *\).*|\1\"$value\"|" "$tfvars"
else
echo "$key = \"$value\"" >> "$tfvars"
fi
fi
done