简体   繁体   中英

Using yq to update a yaml file with artifacthub.io annotations

I have a bash script which is updating a helm yaml file with artifacthub.io annotations . However, my script is using variables that, I believe, requires the command to use double quotes instead of single quotes. Also, the artifacthub.io causes problems where artifact and io are separated. Which yq command can I use to update the changes and images annotation? I've also tried using sed to no avail.

annotations:
  artifacthub.io/changes: |
    - Fixed linting issues.
  artifacthub.io/images: |
    - name: transmission
      image: ghcr.io/linuxserver/transmission:3.00-r0-ls75

I tried something like below but wasn't successful.

image=foo
yq e ".annotations."artifacthub.io/images"=\"${image}\"" -i "${chart_file_path}"

To keep the yq query as readable as possible, I try to avoid escaping double quotes. By using single quotes around the yq query, double quotes do not have to be escaped. Additionally, single quotes can be close and reopen to concatenate bash variable to the query.

As for the keys with specials characters, you need to enclose them inside double quotes and brackets : .annotations.["artifacthub.io/images"]

Given the file:

# file.yml
annotations:
  artifacthub.io/changes: |
    - Fixed linting issues.
  artifacthub.io/images: |
    - name: transmission
      image: ghcr.io/linuxserver/transmission:3.00-r0-ls75

Executing this script:

image="foo"
yq eval '.annotations.["artifacthub.io/images"] = "'${image}'"' file.yml
#       |              |                     |    ||        |||
#       |              |                     |    ||        ||└> (a.1) close yq query
#       |              |                     |    ||        |└> (c) end string value
#       |              |                     |    ||        └> (a.2) open yq query (end concatenation)
#       |              |                     |    |└> (a.2) close yq query (start concatenation)
#       |              |                     |    └> (c) start string value
#       |              |                     └> (b) end key w/ special chars
#       |              └> (b) start key w/ special chars
#       └> (a.1) open yq query

Generates this outputs:

annotations:
  artifacthub.io/changes: |
    - Fixed linting issues.
  artifacthub.io/images: |-
    foo

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