TomWright / dasel

Select, put and delete data from JSON, TOML, YAML, XML and CSV files with a single tool. Supports conversion between formats and can be used as a Go package.
https://daseldocs.tomwright.me
MIT License
7.04k stars 131 forks source link

Preserve comments when editing files #178

Open ypicard opened 2 years ago

ypicard commented 2 years ago

Discussion? Don't really know if I should file this as a feature request or a bug, let me know.

Describe the bug When updating files with the dasel put command, if comments are in the original file, they get stripped out from the output file. I would like to be able to keep them if I want to - or by default.

To Reproduce Edit a yaml file containing comments with the dasel put command. Output does not have comments anymore.

Expected output:

apiVersion: v2
type: application
# Comment 1
version: 0.2.0
# Comment 2
appVersion: 0.1.0
TomWright commented 2 years ago

Similar issue to https://github.com/TomWright/dasel/issues/175.

I'll do some digging to see if I can find a decent solution to this

olblak commented 2 years ago

@TomWright I think this one is a bit different as it doesn't use the same data structure and therefore library. Xml seems to rely on mxj while yaml depends on gopkg.in/yaml.v2 v2.4.0. The version v3 (gopkg.in/yaml.v3) support comments.

TomWright commented 2 years ago

Thanks for the note on v3. It may be as easy as a version upgrade. I'll take a look

TomWright commented 2 years ago

@olblak Where did you see that v3 supports comments?

olblak commented 2 years ago

That's a very good question. I couldn't find the changelog entry mentioning this. But I faced the same issue on https://github.com/updatecli and if I recall correctly switching to v3 solved it.

tiogate commented 2 years ago

The original issue description reports comments being stripped out from a yaml. I would like to report that I observed the same behavior with a toml.

r10r commented 2 years ago

@tiogate I'm also interested in keeping the original comments in a toml config file. Did you find a solution for that ?

It seems that keeping comments is not supported by go-toml library at the moment (or the near future). See also https://github.com/pelletier/go-toml/issues/457

flokli commented 2 years ago

@TomWright did you take a look at an upgrade to yaml.v3, and whether this preserves comments?

I definitely see comment-related fields in the Node struct: https://pkg.go.dev/gopkg.in/yaml.v3#Node

tiogate commented 2 years ago

@tiogate I'm also interested in keeping the original comments in a toml config file. Did you find a solution for that ?

@r10r Unfortunately I did not find a reasonable solution. In my case I only needed to modify one value in a TOML, so I did that with a hacky sed command. I also needed to make more complex modifications to JSON and YAML files though, and for that I used jq and yq.

TomWright commented 2 years ago

Sorry I haven't looked yet - I've been pretty busy lately and haven't been able to find the time to support this as much as I'd like. I haven't forgotten though

danwt commented 4 months ago

I have an example where comments are not preserved with toml

dasel put -f "${ROLLAPP_HOME_DIR}"/config/dymint.toml "settlement_layer" -v "dymension"