jsonnet icon indicating copy to clipboard operation
jsonnet copied to clipboard

Create output directories if they do not exist

Open brbbs opened this issue 9 years ago • 6 comments

I'd like to be able to invoke jsonnet like: jsonnet -o foo/bar.json -e "{ foo: 'bar' }" and have it create the foo/ directory if it does not already exist. I'd like this to apply to multi-file output invocations as well. My use case is to have a file of the form:

{
  'a.json': { /* snip */ },
  'subdir/b.json': /* ... */,
  'subdir/c.json': /* ... */,
}

but I'd like to be able to invoke jsonnet -m without having to know which subdirectories to create beforehand.

Currently, both invocations fail with the error:

$ jsonnet -o foo/bar.json -e "{ foo: 'bar' }"
Writing to output file: foo/bar.json: No such file or directory
$ jsonnet -m . foo.jsonnet
./a.json
./subdir/b.json
Opening output file: ./subdir/b.json: No such file or directory

brbbs avatar May 25 '16 18:05 brbbs

The big challenge here is that Jsonnet is currently pure C++ but the ability to create a dir requires posix / win32 / etc.

sparkprime avatar May 25 '16 20:05 sparkprime

Ah, that is a problem.

I suppose as a workaround I can do something like:

jsonnet foo.jsonnet -o out.json
for p in $(jq -r keys[] < out.json); do mkdir -p $(dirname "$p"); done
jsonnet -m . out.json # or something equivalent with jq

Feel free to close this as will not fix if you don't think you'll end up implementing it.

brbbs avatar May 25 '16 20:05 brbbs

I am sort of thinking that it is really the commandline executable that would need this ability, and having that require posix (or having a chunk of functionality disabled if posix is not detected) is a lot less worse than having the libjsonnet lib (i.e. the core VM) need it. So it may not be too bad.

sparkprime avatar May 25 '16 20:05 sparkprime

ref for search to pick up on this issue: The Go jsonnet version has the flag -c or --create-output-dirs to accomplish this.

Duologic avatar Jun 01 '21 11:06 Duologic

If someone wants to pick this up here, feel free to open the PR. Ideally the interface would be compatible with the one in go-jsonnet. (However, if you're using Jsonnet as a command, you probably want to use go-jsonnet implementation anyway).

sbarzowski avatar Jun 01 '21 17:06 sbarzowski