用yq覆盖覆盖两个yaml文件

Overlay and override two yaml documents with yq

我想将一个 yaml 文档叠加在另一个文档之上,将任何数组更新为叠加值。

给定 base/source 文件:

#base.yaml
include:
  - directory: foo/foo1
  - directory: bar/bar1

和覆盖文件:

#overlay.yaml
include:
  - directory: foo/foo1
    extra: true
    stuff:
      stuff1: true
      stuff2: true
  - directory: something/else

结果应该是这样的

#results.yaml
include:
  - directory: foo/foo1
    extra: true
    stuff:
      stuff1: true
      stuff2: true
  - directory: bar/bar1
  - directory: something/else

我想我已经接近于使用 中的 yq,但是列表元素没有被覆盖。我反而得到 foo/foo1

的副本

yq eval-all '. as $item ireduce ({}; . *+ $item)' base.yaml overlay.yaml 产生

#base.yaml
#overlay.yaml
include:
  - directory: foo/foo1
  - directory: bar/bar1
  - directory: foo/foo1
    extra: true
    stuff:
      stuff1: true
      stuff2: true
  - directory: something/else

删除 * 之后的 + 将从输出中删除 bar/bar1

基本上我认为我是在 include 上操作,而不是 include 键的值。我将不胜感激任何帮助使叠加层正常工作。

看看这个例子:https://mikefarah.gitbook.io/yq/operators/multiply-merge#merge-arrays-of-objects-together-matching-on-a-key

基本上你需要:

yq eval-all '
(
  ((.include[] | {.directory: .}) as $item ireduce ({}; . * $item )) as $uniqueMap
  | ( $uniqueMap  | to_entries | .[]) as $item ireduce([]; . + $item.value)
) as $mergedArray
| select(fi == 0) | .include= $mergedArray
' sample.yml another.yml

爆料:我写了yq