I have a file config.yaml
that looks like this:
myenv1:
abc: efg
host: ( ENV["HOST1"] )
db: ( ENV["DB1"] )
user: <%= ENV["USER1"] %>
pass: <%= ENV["VE_PASS1"] %>
time: 00
myenv2:
abc: efg
host: ( ENV["HOST2"] )
db: ( ENV["DB2"] )
user: <%= ENV["USER2"] %>
pass: <%= ENV["VE_PASS2"] %>
time: 800
And another file values.yaml
that looks like this:
secrets:
- MY_PASS_1
- MY_PASS_2
abc:
- pqr
psp:
- jdfhs
I need to update/replace the value (it is a list) of a particular key secret
in the file values.yaml
with a list of words like VE_PASS1
and VE_PASS2
present in the file config.yaml
.
After running the commands, the values.yaml
should look like this:
secrets:
- VE_PASS1
- VE_PASS2
abc:
- pqr
psp:
- jdfhs
What would be the Linux command or script to do that?
CodePudding user response:
You can first get the list of filtered words into a temporary file temp
like this:
- VE_PASS1
- VE_PASS2
And then apply this list to the value of key secrets
in values.yaml
using yq
.
The script/commands would look like this:
#!/bin/bash
source_file=config.yaml
destination_file=values.yaml
temp_file=temp
readarray -t my_array < <(grep -E '\bVE_' $source_file | awk -v RS=[ -v FS=] 'NR>1{print $1}' | sed 's/"//g')
# yq version 4
touch $temp_file
for i in "${my_array[@]}"; do
printf '\055 %s\n' $i >> $temp_file
done
yq eval '.secrets |= load("temp")' $destination_file --inplace
After running the script, values.yaml
will look like this:
secrets:
- VE_PASS1
- VE_PASS2
abc:
- pqr
psp:
- jdfhs
CodePudding user response:
You can translate your readarray
awk
sed
approach to mikefarah/yq using sub
as follows:
yq -i '
.secrets = (
load("config.yaml")
| map(.pass | sub("^.*\[\"(.*)\"\].*$"; "$1"))
)
' values.yaml
Then, values.yaml
would contain:
secrets:
- VE_PASS1
- VE_PASS2
abc:
- pqr
psp:
- jdfhs