yq/pkg/yqlib/doc/operators/style.md

332 lines
3.6 KiB
Markdown
Raw Normal View History

2021-11-03 04:00:58 +00:00
# Style
2024-02-08 23:45:49 +00:00
The style operator can be used to get or set the style of nodes (e.g. string style, yaml style).
Use this to control the formatting of the document in yaml.
2021-11-03 04:00:58 +00:00
## Update and set style of a particular node (simple)
Given a sample.yml file of:
```yaml
a:
b: thing
c: something
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.a.b = "new" | .a.b style="double"' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
a:
b: "new"
c: something
```
## Update and set style of a particular node using path variables
Given a sample.yml file of:
```yaml
a:
b: thing
c: something
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'with(.a.b ; . = "new" | . style="double")' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
a:
b: "new"
c: something
```
## Set tagged style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
2024-02-08 23:45:49 +00:00
f:
- 1
- 2
- 3
g:
something: cool
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.. style="tagged"' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
!!map
a: !!str cat
b: !!int 5
c: !!float 3.2
e: !!bool true
2024-02-08 23:45:49 +00:00
f: !!seq
- !!int 1
- !!int 2
- !!int 3
g: !!map
something: !!str cool
2021-11-03 04:00:58 +00:00
```
## Set double quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
2024-02-08 23:45:49 +00:00
f:
- 1
- 2
- 3
g:
something: cool
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.. style="double"' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
a: "cat"
b: "5"
c: "3.2"
e: "true"
2024-02-08 23:45:49 +00:00
f:
- "1"
- "2"
- "3"
g:
something: "cool"
2021-11-03 04:00:58 +00:00
```
## Set double quote style on map keys too
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
2024-02-08 23:45:49 +00:00
f:
- 1
- 2
- 3
g:
something: cool
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '... style="double"' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
"a": "cat"
"b": "5"
"c": "3.2"
"e": "true"
2024-02-08 23:45:49 +00:00
"f":
- "1"
- "2"
- "3"
"g":
"something": "cool"
2021-11-03 04:00:58 +00:00
```
## Set single quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
2024-02-08 23:45:49 +00:00
f:
- 1
- 2
- 3
g:
something: cool
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.. style="single"' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
a: 'cat'
b: '5'
c: '3.2'
e: 'true'
2024-02-08 23:45:49 +00:00
f:
- '1'
- '2'
- '3'
g:
something: 'cool'
2021-11-03 04:00:58 +00:00
```
## Set literal quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
2024-02-08 23:45:49 +00:00
f:
- 1
- 2
- 3
g:
something: cool
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.. style="literal"' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
a: |-
cat
b: |-
5
c: |-
3.2
e: |-
true
2024-02-08 23:45:49 +00:00
f:
- |-
1
- |-
2
- |-
3
g:
something: |-
cool
2021-11-03 04:00:58 +00:00
```
## Set folded quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
2024-02-08 23:45:49 +00:00
f:
- 1
- 2
- 3
g:
something: cool
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.. style="folded"' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
a: >-
cat
b: >-
5
c: >-
3.2
e: >-
true
2024-02-08 23:45:49 +00:00
f:
- >-
1
- >-
2
- >-
3
g:
something: >-
cool
2021-11-03 04:00:58 +00:00
```
## Set flow quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
2024-02-08 23:45:49 +00:00
f:
- 1
- 2
- 3
g:
something: cool
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.. style="flow"' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
2024-02-08 23:45:49 +00:00
{a: cat, b: 5, c: 3.2, e: true, f: [1, 2, 3], g: {something: cool}}
2021-11-03 04:00:58 +00:00
```
## Reset style - or pretty print
Set empty (default) quote style, note the usage of `...` to match keys too. Note that there is a `--prettyPrint/-P` short flag for this.
Given a sample.yml file of:
```yaml
2024-02-08 23:45:49 +00:00
{a: cat, "b": 5, 'c': 3.2, "e": true, f: [1,2,3], "g": { something: "cool"} }
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '... style=""' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
a: cat
b: 5
c: 3.2
e: true
2024-02-08 23:45:49 +00:00
f:
- 1
- 2
- 3
g:
something: cool
2021-11-03 04:00:58 +00:00
```
## Set style relatively with assign-update
Given a sample.yml file of:
```yaml
a: single
b: double
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.[] style |= .' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
a: 'single'
b: "double"
```
## Read style
Given a sample.yml file of:
```yaml
{a: "cat", b: 'thing'}
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.. | style' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
flow
double
single
```