medialab / xan

The CSV magician
The Unlicense
171 stars 7 forks source link

fmt overwrites input file #272

Open jayvdb opened 2 months ago

jayvdb commented 2 months ago

The following will overwrite the input file.

xan fmt --output input.csv input.csv

Most beautifiers have an "in place" mode where the file being formatted will be replaced with the improved version.

Yomguithereal commented 2 months ago

Hello @jayvdb. I see your point, but I am not sure I am fond of implicit inplace edition with CLI tools myself. It is also sometimes a flag in some utilities such as sed (-i) and is nothing more than writing to a temporary file besides the one you are editing and a move right after, right? This is equivalent to xan fmt -o data.tmp.csv data.csv && mv data.tmp.csv data.csv, no?

Then, to me xan fmt --output input.csv input.csv obviously fail in my mental model because you are opening the input as a buffer to truncate to it cannot work, but I agree it looks silly and one might intuitively think it to work as you want it to.

So, my main issue here is to maybe find a way to add this without too much development cost, which means a dedicated flag is out of the question. Maybe some wrapper around a Write implementor opening a temporary file next to the read one with a Drop implementation executing the move is a smart way to go? But is would still need to know the operation completed successfully before executing the move at the end. And I don't want to add a call to a finalization method everywhere.

Any idea?

Also @boogheta, how do you feel about this?

Yomguithereal commented 2 months ago

Looks like it: https://docs.rs/in-place/latest/in_place/ but still needs to call a finalization method of course.