yq v4 根据更深层密钥的存在获取根密钥

yq v4 get root keys based on existence of deeper keys

我有这样的结构:

foo:
  image: 123

bar:
  image: 456

baz:
  config: "my config"

并且我想根据子“image”的存在打印根键(即 foo、bar、baz)

在 yq 版本 3 中,我可以这样做:

$ yq read test.yaml --printMode p "*.image" | awk -F'.' '{print }'
foo
bar

但我在 v4 中找不到等效项。 yq + jq 的解决方案是:

$ yq -j e test.yaml | jq -r 'to_entries[] | select(.value | has("image")) | [.key][]' 
foo
bar

知道如何使用 yq v4 做到这一点吗?

您可以使用path operator获取包含标签image

的匹配对象的路径
yq e '.[] | select(has("image")) | path | .[]' yaml