简体   繁体   中英

Using yq to modify yml files

I am trying to add new arrays to the existing yml file

for example the test.yml looks like this

visibility:
  subscribe:
    enabled: true
    type: authenticated
  view:
    enabled: true
    type: public

and I am trying to modify it to look like this

visibility:
  subscribe:
    tags: []
    orgs: []
    enabled: true
    type: authenticated
  view:
    tags: []
    orgs: []
    enabled: true
    type: public

I have used yq to do that but there seems to be an issue it does not add any new field Here is the bash script I made

yq '.visibility.subscribe |= {"orgs":"[]"} + .' test.yml
yq '.visibility.subscribe |= {"tags":"[]"} + .' test.yml
yq '.visibility.view |= {"orgs":"[]"} + .' test.yml
yq '.visibility.view |= {"tags":"[]"} + .' test.yml

Am I using the right logic?

Presuming that you're using the yq tool that wraps jq (which appears to be the case from your starting syntax):

yq '
  .visibility.subscribe.orgs = []
| .visibility.subscribe.tags = []
| .visibility.view.orgs = []
| .visibility.view.tags = []
' test.yml

...is a simpler way to achieve output that's semantically identical (not necessarily syntactically identical.) to your stated/desired output. The most important change necessary was changing "[]" to [] .

Note that the new document is written to stdout -- test.yml is not modified in-place unless the -i argument is used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM