dasel icon indicating copy to clipboard operation
dasel copied to clipboard

Preserve comments when editing files

Open ypicard opened this issue 3 years ago • 11 comments

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.

  • create an original yaml file named example.yaml:
apiVersion: v2
type: application
# Comment 1
version: 0.1.0
# Comment 2
appVersion: 0.1.0
  • execute: dasel put string -f example.yaml .version 0.2.0
  • Output looks like:
apiVersion: v2
appVersion: 0.1.0
type: application
version: 0.2.0

Expected output:

apiVersion: v2
type: application
# Comment 1
version: 0.2.0
# Comment 2
appVersion: 0.1.0

ypicard avatar Nov 01 '21 18:11 ypicard

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

TomWright avatar Nov 03 '21 16:11 TomWright

@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.

olblak avatar Nov 07 '21 20:11 olblak

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

TomWright avatar Nov 07 '21 21:11 TomWright

@olblak Where did you see that v3 supports comments?

TomWright avatar Nov 09 '21 17:11 TomWright

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.

olblak avatar Nov 12 '21 09:11 olblak

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.

tiogate avatar Dec 09 '21 10:12 tiogate

@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

r10r avatar Jun 13 '22 15:06 r10r

@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

flokli avatar Jun 27 '22 09:06 flokli

@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.

tiogate avatar Jun 28 '22 07:06 tiogate

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

TomWright avatar Jun 29 '22 10:06 TomWright