carvel-dev / ytt

YAML templating tool that works on YAML structure instead of text
https://carvel.dev/ytt
Apache License 2.0
1.69k stars 136 forks source link

Specify File Extensions for --output-files #271

Open scottd018 opened 3 years ago

scottd018 commented 3 years ago

Describe the problem/challenge you have Some people are opinionated that YAML files should end with a .yaml extension, however others prefer a .yml extension for brevity. Additionally, there may be complex cases where users are transitioning from other templating languages (e.g. Jinja2 *.yaml.j2) and would like the resultant output to be a proper YAML extension.

Describe the solution you'd like Very simply, I think an argument such as --output-extension to be used with --output-files would suffice. The extension should be a proper extension as input by the user and begin with a .. Even more restrictive, users could be allowed to output with either .yaml or .yml file extensions.

Anything else you would like to add: No additional info at this time.

scottd018 commented 3 years ago

Sample Input Example (SIMPLE)

Input Contents - simple-input1.yml and simple-input2.yml

YTT can be given some input files (simple-input1.yml and simple-input2.yml in this example) with valid input:

simple-input1.yml

---
thing1: "cool thing"
thing2: "bad thing"

simple-input2.yml

---
thing3: "awesome thing"
thing4: "awful thing"

Command

A command to output the files with an extension is run:

ytt -f simple-input1.yml -f simple-input2.yml --output-files /tmp --output-extension '.yaml.output'

Result

An output file is written at the --output-files location with the --output-extension extension (/tmp/simple-input1.yaml.output and /tmp/simple-input2.yaml.output in this example).

cat /tmp/simple-input1.yaml.output /tmp/simple-input2.yaml.output
---
thing1: "cool thing"
thing2: "bad thing"
---
thing3: "awesome thing"
thing4: "awful thing"

Sample Input Example (SLIGHTLY MORE COMPLEX)

Input Contents - simple-input1.yaml.j2 and simple-input2.yaml.j2

NOTE: Jinja2 is being used as an example only for reference. YTT should generically be able to take in and validate/parse the YAML, regardless of its file extension (it seems to do this already based on my experiences). Any crazy file markers/templating that forces the input to become invalid will throw an error.

YTT can be given some input files (simple-input1.yaml.j2 and simple-input2.yaml.j2 in this example) with valid input:

simple-input1.yaml.j2

---
thing1: {{ thing1 }}
thing2: {{ thing2 }}

simple-input2.yaml.j2

---
thing3: {{ thing3 }}
thing4: {{ thing4 }}

Command

A command to output the files with an extension is run:

ytt -f simple-input1.yaml.j2 -f simple-input2.yaml.j2 --file-mark "*.yaml.j2:type=yaml-plain" --output-files /tmp --output-extension '.yaml'

NOTE: In the above example we mark the files as plain YAML, otherwise YTT won't recognize them.

Result

An output file is written at the --output-files location with the --output-extension extension (/tmp/simple-input1.yaml and /tmp/simple-input2.yaml in this example).

cat /tmp/simple-input1.yaml /tmp/simple-input2.yaml
---
thing1: '{{ thing1 }}'
thing2: '{{ thing2 }}'
---
thing3: '{{ thing3 }}'
thing4: '{{ thing4 }}'

NOTE: the above is not necessarily an exact representation of the file contents of what a J2 file may look like.

gcheadle-vmware commented 3 years ago

@scottd018 Thank you for the detailed examples! This issue will be prioritized soon.