Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In place write strips out comments #19

Closed
jmreicha opened this issue Aug 23, 2017 · 27 comments
Closed

In place write strips out comments #19

jmreicha opened this issue Aug 23, 2017 · 27 comments
Labels

Comments

@jmreicha
Copy link

The CLI strips out comments given the following yaml file:

# comment
a:
  b:

Using the following command for my test:

yaml w -i test.yml a.b.c[0] "test"

@jmreicha
Copy link
Author

jmreicha commented Aug 23, 2017

Apparently it also strips out both single ' and double " quotes from arrays.

a:
  b:
    c:
    - 'test'
    - "test2"

Command I used:

yaml w test.yml a.b.c[1] "test2"

Let me know if you want a separate issue for this.

@mikefarah
Copy link
Owner

yep this is because it parses the yaml file using https://godoc.org/gopkg.in/yaml.v2, and then spits it out again.

This means it will not parse comments nor will it differentiate between those quotes if it is required to (for instance if the value is double quotes surrounded by singles ('"') ).

@jmreicha
Copy link
Author

Got it, thanks.

@nemchik
Copy link

nemchik commented Jun 5, 2018

@mikefarah is there a way to get yq to NOT strip quotes? I'm using yq for Docker Compose and I need to keep quotes around ports like this

ports:
 - "3000"
 - "3000-3005"
 - "8000:8000"
 - "9090-9091:8080-8081"
 - "49100:22"
 - "127.0.0.1:8001:8001"
 - "127.0.0.1:5000-5010:5000-5010"
 - "6060:6060/udp"

@mikefarah
Copy link
Owner

Not at the moment - and given how it works with the yaml.v2 I'm not sure it can sorry :(

There may be a workaround I'm not aware of, I'll reopen this issue.

@mikefarah mikefarah reopened this Jun 5, 2018
@nemchik
Copy link

nemchik commented Jun 5, 2018

Ok. Thanks for reopening the issue. Here is the source I'm referencing https://docs.docker.com/compose/compose-file/

Note: When mapping ports in the HOST:CONTAINER format, you may experience erroneous results when using a container port lower than 60, because YAML parses numbers in the format xx:yy as a base-60 value. For this reason, we recommend always explicitly specifying your port mappings as strings.

@IdanAdar
Copy link

Is there any way to re-add the quotes via some command, though?

@jeking3
Copy link

jeking3 commented Jan 4, 2019

Too bad it does this, I have to abandon using it because it removes comments.

@devdream02
Copy link

@IdanAdar Have you found the solution of re-adding the quotes?
@mikefarah Any update when this functionality be added ?
Thanks!

@IdanAdar
Copy link

I have not.

@devdream02
Copy link

I have not.

Can you suggest any work around or some other library?

@IdanAdar
Copy link

No. In my case it just happened to continue working even without the quotes.

@evanstucker-hates-2fa
Copy link

This might be useful...

https://kev.inburke.com/kevin/more-comment-preserving-configuration-parsers/

I don't know Go, but someone wrote a library for it here:

https://gist.github.com/kevinburke/07a21855df700b0acdea9e91c6fde7cf

@danielfbm
Copy link

+1 for preserving comments. yq is a great tool, and we currently use it to automatically update our helm charts when a docker image changes

As in most helm chart's values.yaml file, the optional values that have a complex format/complex objects are generally commented, therefore it is really required to keep the comments in order to inform the user on how to customize these values.

I can think of a few workarounds to this using different files in our pipeline etc., but would be really appreciated if yq just preserved the comments

@shaleh
Copy link

shaleh commented Jul 18, 2019

https://ubuntu.com/blog/api-v3-of-the-yaml-package-for-go-is-available

It looks like moving to the v3 parser is the solution.

@andye2004
Copy link

@shaleh reading the release details you linked to I can see that it will solve the comments issue, but I can't see anything about quoted values. Am I missing something?

@mikefarah
Copy link
Owner

Yup - I've looked into the new library, looks really neat - but it requires significant rework of yq which I just don't have time for atm

@mikefarah
Copy link
Owner

@andye2004 when I played with it you could preserve quotes

@danielfbm
Copy link

any update on this issue? I am willing to collaborate if you will have me

@alexlemaire
Copy link

Following up on that: would really love to have comments preserved when updating files in place 👍

I am using yq to update config files where I'd like to leave comments to give some extra hints to the users of this config file via comments.

@mikefarah
Copy link
Owner

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

@jhagege
Copy link

jhagege commented Jan 21, 2020

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

Hi, looks great.
Is it possible to update a value with double quotes ?
yq w a.yml services.a.command ""tox -vv -e${ENVLIST}""

I get the following: command: '"tox -vv -e${ENVLIST}"' (enclosed within single quotes)

@mikefarah
Copy link
Owner

Yep - if you're wrapping with double quotes (so you can interpolate ENVLIST) then you'll need to escape the inner quotes.

x=thing
yq n services "\"cat${x}\""

will yield

services: '"catthing"'

That this therefore has the value "catthing" when reading the yaml.

If you want a single pair of quotes:

services: "catthing"

Then at the moment v3 does not support this, instead it only quotes values when required. It could support an override by passing in a 'style' flag (https://godoc.org/gopkg.in/yaml.v3#Style).

@mikefarah
Copy link
Owner

Sidenote:

services: catthing

and

services: "catthing"

Both effectively have the same values...

@jhagege
Copy link

jhagege commented Jan 22, 2020 via email

@mikefarah
Copy link
Owner

Fixed in https://github.com/mikefarah/yq/releases/tag/3.0.1

libby added a commit to Consensys/qubernetes that referenced this issue Oct 28, 2020
* add external_nodes to configyaml used by qctl, so qctl can interact with it, 
   e.g. add, delete, ls
* add support for listing the external nodes in a network:
    > qctl ls extnodes --all
* add support for adding an external node:
  > qctl add extnode --enode="enode://[email protected]:7000" --tmurl=http://1.2.3.4:9000  --nodekeyaddr=0x1234334
*  add support deleting external nodes:
    >  qctl delete external-node
* allow the external_node `Node_Acct_Addr` field to be set to a hex without quotes
   or a string hex in the qubernetes yaml. If it is set as a hex without quotes, the hex value will 
   be converted to a BigNum, cover both cases in `istanbul-validator.toml.erb`. 
   the yaml parser (https://godoc.org/gopkg.in/yaml.v2) used does not preserve double 
   quotes around the hex  mikefarah/yq#19
@mat926
Copy link

mat926 commented Jun 20, 2024

I'm still having this issue even on 4.44.2 . I have a command like
yq '.rffmpeg.directories.owner = "dockerlimited"' /config/rffmpeg.yml -i

But the quotes around dockerlimited do not get preserved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests