Yq: Writing Scalars/Blocks/Newline Literals

Created on 4 Jan 2018  路  5Comments  路  Source: mikefarah/yq

Given /tmp/yam:

top:
  yam:
    notyam: nope

Actual result:

# cat <<EOF | yq w /tmp/yam top.ugh -
# pipe heredoc> |
# pipe heredoc>   ugh
# pipe heredoc> EOF
top:
  yam:
    notyam: nope
  ugh: '-'

# yq w /tmp/yam top.ugh "$(echo -e '|\n  wat')"
top:
  yam:
    notyam: nope
  ugh: |-
    |
      wat

# yq w /tmp/yam top.ugh "$(echo -e '\nwat')"
top:
  yam:
    notyam: nope
  ugh: |2-

    wat

# yq w /tmp/yam top "$(echo -e 'ugh: |\n  wat')"
top: |-
  ugh: |
    wat

Expected result:

top:
  yam:
    notyam: nope
  ugh: |
      wat

Ideally, there'd be a flag to treat stdin as you're value in write mode instead of source file content or a [<symbol>] "filter" to denote a multiline value to gain representation as a scalar in w mode(s).

As a workaround, writing out a separate file with the preceding/parent mappings and then using the m (merge) mode works:

# cat <<EOF > /tmp/ugh && yq m /tmp/yam /tmp/ugh
# top:
#   ugh: |
#     wat
# EOF
top:
  ugh: |
    wat
  yam:
    notyam: nope

Am I.. doing it wrong?

enhancement yaml-v3

Most helpful comment

Just released a new version of yaml that fixes this - it's a pretty significant update so it's still in beta: https://github.com/mikefarah/yq/releases/tag/3.0.0-beta

All 5 comments

No you're not doing it wrong - yq doesn't yet support this ;(

Ah ok. I was trying to sort this out all day. Funnily what i got from

yq w test.yaml objects[0].tls.certificate -- "$(cat cert.pem)"

was

!! binary |
[[the base64 encoded string of the value I wanted to add]]

Would be awesome if this feature was added. Is this a feature being planned?

FWIW: I ended up going with https://www.npmjs.com/package/yaml-cli for my use case (consuming yaml and producing a json packer) wherein there's a ton of heredoc-style shell scripts/yaml scalars. I'd still love to see this feature in yq tho.

Just released a new version of yaml that fixes this - it's a pretty significant update so it's still in beta: https://github.com/mikefarah/yq/releases/tag/3.0.0-beta

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jodh-intel picture jodh-intel  路  3Comments

il1yaz picture il1yaz  路  3Comments

nicholasdille picture nicholasdille  路  7Comments

cdaguerre picture cdaguerre  路  4Comments

IdanAdar picture IdanAdar  路  9Comments