【问题标题】:Set default values for defined keys in yaml为 yaml 中定义的键设置默认值
【发布时间】:2022-06-30 00:07:22
【问题描述】:

我有关注master.yaml 文件

root:
  - name: First
    some_value: One
  - name: Second
    some_value: Two

update.yaml 文件

defaults:
  other_value: true
  one_more: Three

预期结果是:

root:
  - name: First
    some_value: One
    other_value: true
    one_more: Three
  - name: Second
    some_value: Two
    other_value: true
    one_more: Three

我尝试了以下命令:

yq eval-all '(select(fileIndex == 0).root.[] *+ select(fileIndex == 1).defaults)' './master.yaml' './update.yaml'

但结果是:

name: First
some_value: One
other_value: true
one_more: Three
name: Second
some_value: Two
other_value: true
one_more: Three

【问题讨论】:

    标签: yaml yq


    【解决方案1】:

    您也可以使用load function 将第二个文件与第一个文件合并。即

    yq '.root[] *= load("update.yaml").defaults' master.yaml
    

    OP 似乎在 Windows/Powershell 上运行 yq。在其上执行命令行程序需要额外的引号层。见Quotes on Windows Powershell

    【讨论】:

    • 每次我使用加载函数时都会出现流动错误:Error: parsing expression: Lexer error: could not match text starting at 1:17 failing at 1:20. unmatched text: "upd"
    • 您运行的是哪个版本的yq?在当前最新版本(v4.25.3)上对我来说效果很好。另外,您是否像我在答案中显示的那样运行它?
    • yq (github.com/mikefarah/yq) 版本 4.25.1 运行准确答案
    • 可以升级试试吗?
    • 这是 Windows 的最新版本:community.chocolatey.org/packages/yq
    猜你喜欢
    • 2011-08-01
    • 2015-01-10
    • 2016-02-03
    • 2013-09-26
    • 2010-10-28
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    相关资源
    最近更新 更多