yaml - 使用 yq 归档对象数组

标签 yaml yq

我有一个如下所示的 yaml 文件:

apiVersion: v1
entries:
  blue-green-toggle:
  - description: Used to toggle an application between blue and green
    name: blue-green-toggle
    version: 1.0.17
    apiVersion: v2
  - description: Used to toggle an application between blue and green
    name: blue-green-toggle
    version: 1.0.16
    apiVersion: v2
  - description: Used to toggle an application between blue and green
    name: blue-green-toggle
    version: 1.0.15
    apiVersion: v2
  istio-config:
  - description: Used to configure the cluster level settings of istio.
    name: istio-config
    version: 1.0.4
    apiVersion: v2
  - description: Used to configure the cluster level settings of istio.
    name: istio-config
    version: 1.0.1
    apiVersion: v2
  latest-toggle:
    name: latest-toggle
    version: 1.0.5
    apiVersion: v2
  standard-helm-chart:
    name: standard-helm-chart
    version: 1.1.10
    apiVersion: v2
    name: standard-helm-chart
    version: 2.0.1
    apiVersion: v2
    name: standard-helm-chart
    version: 1.0.34
    apiVersion: v2
    name: standard-helm-chart
    version: 1.0.10
    apiVersion: v2
    name: standard-helm-chart
    version: 1.0.9
    apiVersion: v2
generated: 2021-06-22T00:22:33.1554922Z
...

我正在尝试列出以 1.0. 开头并列在 standard-helm-chart 部分中的 version 编号。

到目前为止,我只使用它来获取 standard-helm-chart 的条目:

yq eval '.entries | .standard-arup-helm-chart' index.yaml

效果很好。因此,然后我尝试获取 version 匹配 1.0.* 的行。我阅读了 select documentation对于 yq,但它不指示当您查看对象而不仅仅是字符串时如何匹配。

我试过这个:

yq eval '.entries | .standard-arup-helm-chart | select(. == "1.0.*")' index.yaml

但这失败了。我希望它能,因为它无法将“1.0.*”的字符串与整个对象进行比较。

我也试过:

yq eval '.entries | .standard-arup-helm-chart | select(.version == "1.0.*")' index.yaml

认为这会让 yq 知道我只想查看版本。但它说 Error: Cannot index array with 'version'

然后我想我需要尝试一种数组样式语法:

yq eval '.entries | .standard-arup-helm-chart | select(.[version] == "1.0.*")' index.yaml

但是由于解析错误而失败。

我可以发送什么命令 yq 来获取所有以 1.0. 开头的版本号?

最佳答案

尝试了一些错误,但这是我最终得到的:

yq eval '.entries.standard-helm-chart.[] | select(.version == "1.0.*") | .version' index.yaml

关于yaml - 使用 yq 归档对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68076374/

相关文章:

json - openAPI 无法解析对外部文件的引用。组件名称包含无效字符

java - 在 spring 中获取 application.yml 信息

kubernetes - Helm Chart - 处理多行文件结果

aws-cloudformation - 如何使用 yq 设置带有函数简写(即 !Sub)的 cloudformation 字段?

kubernetes - yq - 将 yaml 添加到 yaml 的问题

python - 为什么这么多的应用程序/框架将它们的配置文件保存为未执行的格式?

docker-compose - 如何使用 yq 删除 YAML 文件中的属性?

bash - yq - 添加多行字符串

yaml - yq 命令没有将内容持久写入文件

c# - 在 C# 中合并 YAML