替换一个 YAML 块

huangapple go评论76阅读模式
英文:

Replace a block of YAML

问题

I want to replace an array block in a yaml file with a specific different array. I don't necessarily know how many elements are in the array to be replaced, nor what their values are.

yq seems like the best tool for the job (but I am open to other suggestions).

My yaml file looks kind of like this:

name: mything

parameters:
  - name: environment
    type: string
    values:
      - storytest-DEV
      - storytest-PRD
  - name: destroy
    type: boolean
    default: false
  - name: validate
    type: boolean
    default: true

variables:
  - group: storytest
  - name: version
    value: "1.0.0"

and I want to replace the values of the parameters block with the name environment with a single element, lets say, foo.

so far I've got: yq '.parameters[] | select(.name == "environment").values = ["foo"]' infile.yml

which gives me

name: environment
type: string
values:
  - foo
name: destroy
type: boolean
default: false
name: validate
type: boolean
default: true

which is what I want except that, I assume because of the .parameters[] |, I've lost the rest of my file (and the name fields no longer show dashes - meaning it's lost the fact that they're array elements, I assume

How can I replace the

- storytest-DEV
- storytest-PRD

with

- foo

and leave the rest of the file intact?

英文:

I want to replace an array block in a yaml file with a specific different array. I don't necessarily know how many elements are in the array to be replaced, nor what their values are.

yq seems like the best tool for the job (but I am open to other suggestions).

My yaml file looks kind of like this:

name: mything

parameters:
  - name: environment
    type: string
    values:
      - storytest-DEV
      - storytest-PRD
  - name: destroy
    type: boolean
    default: false
  - name: validate
    type: boolean
    default: true

variables:
  - group: storytest
  - name: version
    value: "1.0.0"

and I want to replace the values of the parameters block with the name environment with a single element, lets say, foo.

so far I've got: yq '.parameters[] | select(.name == "environment").values = ["foo"]' infile.yml

which gives me

name: environment
type: string
values:
  - foo
name: destroy
type: boolean
default: false
name: validate
type: boolean
default: true

which is what I want except that, I assume because of the .parameters[] |, I've lost the rest of my file (and the name fields no longer show dashes - meaning it's lost the fact that they're array elements, I assume

How can I replace the

- storytest-DEV
- storytest-PRD

with

- foo

and leave the rest of the file intact?

答案1

得分: 0

使用括号来保留迭代之外的上下文:

(.parameters[] | select(.name == "environment")).values = ["foo"]

或者让 map 进行迭代并更新数组:

.parameters |= map(select(.name == "environment").values = ["foo"])

这两种方法适用于 kislyuk/yqmikefarah/yqitchyny/gojq,结果如下:

name: mything
parameters:
  - name: environment
    type: string
    values:
      - foo
  - name: destroy
    type: boolean
    default: false
  - name: validate
    type: boolean
    default: true
variables:
  - group: storytest
  - name: version
    value: "1.0.0"
英文:

Use parentheses to retain the context outside the iteration:

(.parameters[] | select(.name == "environment")).values = ["foo"]

Or let map do the iteration, and update the array:

.parameters |= map(select(.name == "environment").values = ["foo"])

Both work with kislyuk/yq, mikefarah/yq, and itchyny/gojq, resulting in:

name: mything
parameters:
  - name: environment
    type: string
    values:
      - foo
  - name: destroy
    type: boolean
    default: false
  - name: validate
    type: boolean
    default: true
variables:
  - group: storytest
  - name: version
    value: "1.0.0"

huangapple
  • 本文由 发表于 2023年6月22日 03:22:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定