con2 / emrichen

A Template engine for YAML & JSON
MIT License
107 stars 11 forks source link

Only truncate --output-file after enriching #54

Closed japsu closed 2 years ago

japsu commented 2 years ago

Consider the following files:

# a.yaml
a: 1
b: 2

# a.in.yaml – template
!Merge
  - !Include a.yaml
  - !Include a.update.yaml

# a.update.yaml – an incremental update to a.yaml
b: 3

Now, if you run emrichen --output-file a.yaml a.in.yaml, the resulting a.yaml will only contain

b: 3

instead of

a: 1
b: 3

because --output-file a.yaml is opened at application startup with w mode, truncating the file, and !Include a.yaml enriches to {}.

I would like to change this so that !Include a.yaml here would enrich to the original contents of a.yaml, and a.yaml would be opened for writing only after enriching the template.

akx commented 2 years ago

Absolutely makes sense. To avoid having to buffer the output to memory before writing, maybe the file should be opened with w+, sought to the beginning and then truncated at the start of write?