yq/pkg/yqlib/doc/Comment Operators.md

152 lines
2.1 KiB
Markdown
Raw Normal View History

2020-11-17 22:44:16 +00:00
Use these comment operators to set or retrieve comments.
2021-02-26 00:31:43 +00:00
Like the `=` and `|=` assign operators, the same syntax applies when updating comments:
### plain form: `=`
This will assign the LHS nodes comments to the expression on the RHS. The RHS is run against the matching nodes in the pipeline
### relative form: `|=`
Similar to the plain form, however the RHS evaluates against each matching LHS node! This is useful if you want to set the comments as a relative expression of the node, for instance its value or path.
2020-11-22 02:16:54 +00:00
## Set line comment
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
```
then
```bash
yq eval '.a lineComment="single"' sample.yml
```
will output
```yaml
a: cat # single
```
2021-01-06 09:22:50 +00:00
## Use update assign to perform relative updates
Given a sample.yml file of:
```yaml
a: cat
b: dog
```
then
```bash
yq eval '.. lineComment |= .' sample.yml
```
will output
```yaml
a: cat # cat
b: dog # dog
```
2020-11-22 02:16:54 +00:00
## Set head comment
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
```
then
```bash
yq eval '. headComment="single"' sample.yml
```
will output
```yaml
# single
a: cat
```
2020-11-22 02:16:54 +00:00
## Set foot comment, using an expression
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
```
then
```bash
yq eval '. footComment=.a' sample.yml
```
will output
```yaml
a: cat
# cat
```
2020-11-22 02:16:54 +00:00
## Remove comment
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat # comment
b: dog # leave this
```
then
```bash
yq eval '.a lineComment=""' sample.yml
```
will output
```yaml
a: cat
b: dog # leave this
```
2020-11-22 02:16:54 +00:00
## Remove all comments
2021-01-13 22:12:14 +00:00
Note the use of `...` to ensure key nodes are included.
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat # comment
2021-01-13 22:12:14 +00:00
# great
b: # key comment
2020-11-17 22:44:16 +00:00
```
then
```bash
2021-01-13 22:12:14 +00:00
yq eval '... comments=""' sample.yml
2020-11-17 22:44:16 +00:00
```
will output
```yaml
a: cat
2021-01-13 22:12:14 +00:00
b:
2020-11-17 22:44:16 +00:00
```
2020-11-22 02:16:54 +00:00
## Get line comment
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat # meow
```
then
```bash
yq eval '.a | lineComment' sample.yml
```
will output
```yaml
meow
```
2020-11-22 02:16:54 +00:00
## Get head comment
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat # meow
```
then
```bash
yq eval '. | headComment' sample.yml
```
will output
```yaml
2020-11-19 05:45:05 +00:00
2020-11-17 22:44:16 +00:00
```
2020-11-22 02:16:54 +00:00
## Get foot comment
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat # meow
```
then
```bash
yq eval '. | footComment' sample.yml
```
will output
```yaml
2020-11-19 05:45:05 +00:00
2020-11-17 22:44:16 +00:00
```