简体   繁体   English

yaml 模板(可能是 yq)

[英]yaml templating (maybe yq)

Hello currently I try to find a tool (I'm pretty sure yq does not the magic for me) to remove some content from a yaml file.您好,目前我正在尝试找到一个工具(我很确定 yq 对我来说没有魔力)来从 yaml 文件中删除一些内容。 My file looks as following:我的文件如下所示:

  paths:
    /entity/{id}:
      get:
        tags: a
        summary: b
      ...

So its a typical openapi-specification.所以它是一个典型的 openapi 规范。 I would like to add a magic property for example 'env: prod' so that some endpoints look as following:我想添加一个魔术属性,例如“env:prod”,以便某些端点如下所示:

  paths:
    /entity/{id}:
      get:
        env: prod
        tags: a
        summary: b
      ...

Is there a solution to remove all endpoints, which contain env: prod?是否有删除所有端点的解决方案,其中包含 env: prod?

I am also free to change the concept.我也可以自由地改变这个概念。 If there would be some transformation with a if else I would be very happy.如果用 if else 会有一些转变,我会很高兴。

Using kislyuk/yq :使用kislyuk/yq

yq -y '.[][][] |= ({env: "prod"} + .)'

Using mikefarah/yq :使用mikefarah/yq

yq '.[][][] |= ({"env": "prod"} + .)'

Both produce:两者都产生:

paths:
  /entity/{id}:
    get:
      env: prod
      tags: a
      summary: b

This adds env: prod to every object that is three levels deep.这将env: prod添加到每个三层深的对象。 If you want the criteria be more sophisticated, you will have to adapt .[][][] accordingly.如果您希望标准更复杂,则必须相应地调整.[][][]

yq 'del(.. | select(.env == "prod"))' file.yaml

Explanation: You want to delete all the nodes that have a child 'env' property set to 'prod'.说明:您要删除所有将子“env”属性设置为“prod”的节点。

  • .. recursively matches all nodes ..递归匹配所有节点
  • select(.env == "prod") select the ones that have a env property equal to "prod" select(.env == "prod")选择env属性等于 "prod" 的那些
  • del(.. | select(.env == "prod") delete those nodes :) del(.. | select(.env == "prod")删除那些节点 :)

Disclaimer: I wrote mikefarah/yq免责声明:我写了 mikefarah/yq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM