yq/pkg/yqlib/doc/Style.md
2021-09-12 21:52:02 +10:00

2.8 KiB

The style operator can be used to get or set the style of nodes (e.g. string style, yaml style)

Update and set style of a particular node (simple)

Given a sample.yml file of:

a:
  b: thing
  c: something

then

yq eval '.a.b = "new" | .a.b style="double"' sample.yml

will output

a:
  b: "new"
  c: something

Update and set style of a particular node using path variables

Given a sample.yml file of:

a:
  b: thing
  c: something

then

yq eval 'with(.a.b ; . = "new" | . style="double")' sample.yml

will output

a:
  b: "new"
  c: something

Set tagged style

Given a sample.yml file of:

a: cat
b: 5
c: 3.2
e: true

then

yq eval '.. style="tagged"' sample.yml

will output

!!map
a: !!str cat
b: !!int 5
c: !!float 3.2
e: !!bool true

Set double quote style

Given a sample.yml file of:

a: cat
b: 5
c: 3.2
e: true

then

yq eval '.. style="double"' sample.yml

will output

a: "cat"
b: "5"
c: "3.2"
e: "true"

Set double quote style on map keys too

Given a sample.yml file of:

a: cat
b: 5
c: 3.2
e: true

then

yq eval '... style="double"' sample.yml

will output

"a": "cat"
"b": "5"
"c": "3.2"
"e": "true"

Set single quote style

Given a sample.yml file of:

a: cat
b: 5
c: 3.2
e: true

then

yq eval '.. style="single"' sample.yml

will output

a: 'cat'
b: '5'
c: '3.2'
e: 'true'

Set literal quote style

Given a sample.yml file of:

a: cat
b: 5
c: 3.2
e: true

then

yq eval '.. style="literal"' sample.yml

will output

a: |-
  cat  
b: |-
  5  
c: |-
  3.2  
e: |-
  true  

Set folded quote style

Given a sample.yml file of:

a: cat
b: 5
c: 3.2
e: true

then

yq eval '.. style="folded"' sample.yml

will output

a: >-
  cat  
b: >-
  5  
c: >-
  3.2  
e: >-
  true  

Set flow quote style

Given a sample.yml file of:

a: cat
b: 5
c: 3.2
e: true

then

yq eval '.. style="flow"' sample.yml

will output

{a: cat, b: 5, c: 3.2, e: true}

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:

a: cat
"b": 5
'c': 3.2
"e": true

then

yq eval '... style=""' sample.yml

will output

a: cat
b: 5
c: 3.2
e: true

Set style relatively with assign-update

Given a sample.yml file of:

a: single
b: double

then

yq eval '.[] style |= .' sample.yml

will output

a: 'single'
b: "double"

Read style

Given a sample.yml file of:

{a: "cat", b: 'thing'}

then

yq eval '.. | style' sample.yml

will output

flow
double
single