简体   繁体   English

使用 yq 更新带有 artifacthub.io 注释的 yaml 文件

[英]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 .我有一个 bash 脚本,它正在使用artifacthub.io 注释更新 helm yaml 文件。 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.此外, artifacthub.io会导致 artifact 和 io 分离的问题。 Which yq command can I use to update the changes and images annotation?我可以使用哪个yq命令来更新changesimages注释? I've also tried using sed to no avail.我也尝试过使用sed无济于事。

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.为了使 yq 查询尽可能可读,我尽量避免使用 escaping 双引号。 By using single quotes around the yq query, double quotes do not have to be escaped.通过在 yq 查询周围使用单引号,不必转义双引号。 Additionally, single quotes can be close and reopen to concatenate bash variable to the query.此外,单引号可以关闭并重新打开以将 bash 变量连接到查询。

As for the keys with specials characters, you need to enclose them inside double quotes and brackets : .annotations.["artifacthub.io/images"]对于带有特殊字符的键,您需要将它们括在双引号括号内.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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