j2cli icon indicating copy to clipboard operation
j2cli copied to clipboard

Support multiple data files

Open kilna opened this issue 5 years ago • 4 comments

It'd be nice to support multiple input data files instead of just one.

This could be achieved by adopting the following format:

  • [filename] - load the file at the root of the data tree using default type spec, overwriting/merging with any prior files
  • [type]=[filename] - Load the filename as format [type] (overrides -f)
  • [type]@[loc]=[filename] - Load the filename as format [type] into the data tree at location [loc]
  • @[loc]=[filename] - Load the filename as default format (specified with -f) into the data tree at location [loc]

kilna avatar Aug 31 '19 04:08 kilna

Hmm this is a bit of an issue with envtpl (now unmaintained) I cannot simply convert over as I would have.

ie:

JSON_FILE1=$(<jsonfile1.json) \
JSON_FILE2=$(<jsonfile2.json) \
envtpl --keep-template ~/template.j2 -o ~/output.txt

If I have two for loops that use different JSON source data.

dngray avatar Oct 29 '19 13:10 dngray

Just letting you know you can do this to merge your files and take in via stdin using jq:

For two files:

jq -rs 'reduce .[] as $item ({}; . * $item)' "file1.json file2.json" \
    | j2 -f json -o output.txt input.j2

For any number of files:

jq -rs 'reduce .[] as $item ({}; . * $item)' ./*.json \
    | j2 -f json -o output.txt input.j2

dngray avatar Oct 30 '19 09:10 dngray

But what if I wanted to use yaml (or toml.. or) :( - I would very much prefer to be able to specify multiple data files. This would be very useful, f.ex. if I want to generate 10 files, and have a common dataset for all 10 files - I'd like to only keep in one file - and then keep only variations in the 10 data files.. so I'd have:

common.yaml: key1: value1 key2: value2 ... key99: value99

file1.yaml: file1key: somevalue

So when I render file1 - I'd also give j2cli 2 data files: file1.yaml and common.yaml - to avoid having the common values in all 10 datafiles.

KlavsKlavsen avatar Jan 10 '21 22:01 KlavsKlavsen

Hi! This project gets some interest, but sadly, I'm not maintaining it =\

Maintainer wanted! Discussion: Maintainer Wanted

kolypto avatar Feb 10 '24 16:02 kolypto