Skip to main content
Version: v4.5.0

edit-yaml


Declarative Hook edit-yaml defines changes made to existing YAML files. See an example defining a declarative hook of type edit-yaml:

name: "hook-edit-yaml"
description: Test edit-yaml in hooks
types:
- app
inputs:
- label: Just a text
type: text
name: yaml_body
- label: Just a Tag
type: text
name: yaml_tag
hooks:
- type: edit-yaml
trigger: after-render
path: package.yaml
indent: "4"
encoding: utf-8
changes:
- yamlpath: "$.scripts"
update:
value: |
{
"test": "hello 123",
"hello": "how are you doing"
}
- yamlpath: "$"
remove:
- name: private
- name: devDependencies
- yamlpath: "$.log"
clear: true
- yamlpath: "$.spec.components[?name = '{{INTERPOLATE-INPUT-HERE}}']"
update:
value: |
traits:
- type: test
properties:
port: {{port}}

Available Actions

path:

Defines the path to the file it will be edited. The path can have Jinja expressions.

path: package.yaml

trigger:

Field to set triggers that tell you when file editing should occur.

before-input:
Executes the Declarative Hook before receiving the input parameters from the user.

trigger: before-input

before-render:
Runs the Declarative Hook before the Template generates files in the project.

trigger: before-render

after-render:
Runs the Declarative Hook after the Template generates files in the project.

trigger: after-render

enconding:

This sets the encoding type to be written to the file, and accepts the formats [ascii, utf-8, utf-16]. The default value is utf-8.

encoding: utf-8

indent:

Sets the number of spaces for the indentation of the generated document. The default value for spaces is 2.

indent: 4

Only integers from 1 through 9.


changes:

It is a list of editing actions it will be executed in the order they appear. And your block has the required parameter yamlpath.

info

During the lookup process in the changes attribute, all nodes that match the criteria (yamlpath attribute) are changed at once. You can make several changes in a file just by using expressions.

Check out some accepted expressions:

Click here to expand!
ExpressionDescription
@Define the object's current element.
$Define the object's root element.
()This is a script expression, using the underlying script engine.
[start:end]Returns a copy of part of an array from a subarray created between the start and end positions (the end is not included) of an original array.
?()Apply the filter to the expression of the script.
[]This is a child operator which was used in expressions.
..This is a recursive descent.
*It is used in object elements regardless of using their names.
[]In JSON and javascript, it is the operator of the native array.
[,]JSON path is allowing us a set of array indices and names.

yamlpath:

Allows you to insert the JSON search language into the YAML file. It is Mandatory.

changes:
- yamlpath: "$.packages.version"
...

If necessary, see the yamlpath (jsonpath) support

It is possible to interpolate the values of the yamlpath field with Jinja:

hooks:
- type: edit-yaml
trigger: after-render
path: app.yaml
indent: 2
encoding: utf-8
changes:
- yamlpath: "$.spec.components[?name = '{{INTERPOLATE-INPUT-HERE}}']"
update:
value: |
traits:
- type: test
properties:
port: {{port}}

update: Changes or adds an item within a result from a yamlpath. Available fields are value or snippet.

value
Receives the value to add to the YAML.

  update:
value: "qa"

snippet
Receives the snippet to be added to the YAML.

update:
snippet: snippets/fragment.yaml

remove:
Removes a dictionary element. The available field is name.

name:
Gets the name of the element to be removed from the YAML dictionary. It is possible to interpolate the values of the name field with Jinja.

- yamlpath: "$.scripts"
remove:
- name: prepare

clear:
Removes all contents of the YAML node, given the context of the yamlpath. Accepted values are true or false.

  - yamlpath: "$.devDependencies"
clear: true

when:

Conditional action that evaluates whether the insertion will take effect. The available option is not-exists, which receives the text or file to be evaluated by the condition.

not-exists: Checks if the values entered in the value field exists in the file. If there is, the actions update, remove and clear will not be effective.

hooks:
- yamlpath: "$.some"
update:
value: |
not-existing:
item: 2
when:
not-exists: "$.some.not-existing"

Read Also

Was this page helpful?