edit-yaml
In this section, you will find a reference to a Declarative Hook of the
edit-yamltype.
A Declarative Hook of the edit-yaml type is used to define changes made to existing YAML files. Below, you can see an example of a Declarative Hook definition of the edit-yaml type:
name: "hook-edit-yaml"
description: Test edit-yaml in hooks
types:
- app
spec:
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": "ola 123",
"ola": "como vai você"
}
- yamlpath: "$"
remove:
- name: private
- name: devDependencies
- yamlpath: "$.log"
clear: true
- yamlpath: "$.spec.components[?name = '{{INTERPOLAR-INPUT-AQUI}}']"
update:
value: |
traits:
- type: teste
properties:
port: {{port}}
Available Actions
path:
Defines the path of the file that will be edited. The path can be composed of Jinja expressions.
path: package.yaml
trigger:
Field to set triggers that define when file editing should occur.
before-input:
Executes the Declarative Hook before receiving the input parameters from the user.
trigger: before-input
before-render:
Executes the Declarative Hook before the Template generates files in the project.
trigger: before-render
after-render:
Executes the Declarative Hook after the Template generates files in the project.
trigger: after-render
encoding:
This sets the encoding type to be written to the file and accepts the formats ascii, utf-8 or 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 of spaces is 2.
indent: 4
Use only integers from
1to9.
changes:
It is a list of editing actions that will be executed in the order they appear. Each action block has the yamlpath parameter as required.
During the search process on the changes attribute, all nodes that match the criteria (the yamlpath attribute) are changed at once. You can make multiple changes to a file just by using expressions.
Check out some of the supported expressions:
See detailed expressions
| Expression | Description |
|---|---|
@ | Defines the current element. |
$ | Defines the root element. |
() | Script expression that uses the underlying script engine. |
[start:end] | Returns a copy of part of an array between the start and end positions (end not included). |
?() | Applies a filter to the script expression. |
[] | Child operator used in expressions. |
.. | Represents a recursive descent. |
* | Used on object elements regardless of their names. |
[] | In JSON and JavaScript, it is the native array operator. |
[,] | Allows you to define a set of indexes and array names in a single JSON path. |
yamlpath:
Allows you to define the JSONPath expression used to search in the YAML file. Its use is mandatory.
changes:
- yamlpath: "$.packages.version"
...
If necessary, check the Yamlpath (jsonpath) support
You can 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 = '{{INTERPOLAR-INPUT-AQUI}}']"
update:
value: |
traits:
- type: teste
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 be added 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 the name.
name:
Gets the name of the element to be removed from the YAML dictionary. You can interpolate the values of the name field with Jinja.
- yamlpath: "$.scripts"
remove:
- name: prepare
clear:
Removes all contents of the YAML node, given the yamlpath context. Accepted values are true or false.
- yamlpath: "$.devDependencies"
clear: true
when:
Conditional action that evaluates if the insertion will take place. The available option is not-exists, which receives the text or file to be evaluated by the condition.
not-exists:
Checks whether the values entered in the value field exist in the file. If they exist, the update, remove, and clear actions will not be performed.
changes:
- yamlpath: "$.some"
update:
value: |
not-existing:
item: 2
when:
not-exists: "$.some.not-existing"