1

我有一个 bash 脚本,它正在使用artifacthub.io 注释更新 helm yaml 文件。但是,我认为我的脚本使用的变量需要命令使用双引号而不是单引号。此外,artifacthub.io导致 artifact 和 io 分离的问题。我可以使用哪个yq命令来更新changesimages注释?我也试过用sed没用。

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

我尝试了类似下面的方法,但没有成功。

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

1 回答 1

1

为了使 yq 查询尽可能可读,我尽量避免转义双引号。通过在 yq 查询周围使用单引号,不必转义双引号。此外,单引号可以关闭并重新打开以将 bash 变量连接到查询。

对于带有特殊字符的键,您需要将它们括在双引号括号内.annotations.["artifacthub.io/images"]

给定文件:

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

执行此脚本:

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

生成此输出:

annotations:
  artifacthub.io/changes: |
    - Fixed linting issues.
  artifacthub.io/images: |-
    foo
于 2021-04-11T19:55:40.893 回答