Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Fix not being able to use the env format with stdin#42

Merged
kolypto merged 1 commit intokolypto:masterfrom
atomicptr:fix/env-stdin
Jul 18, 2019
Merged

Fix not being able to use the env format with stdin#42
kolypto merged 1 commit intokolypto:masterfrom
atomicptr:fix/env-stdin

Conversation

@atomicptr
Copy link
Copy Markdown

The problem

You can't use the env format in combination with stdin

# example 1
$ cat data.env | j2 template.yml.j2
# example 2
$ j2 template.yml.j2 < data.env
# both produce a stacktrace like this:
# ...
jinja2.exceptions.UndefinedError: 'IP_ADDRESS' is undefined

Example files

data.env

IP_ADDRESS=127.0.0.1

template.yml.j2

host:
  ip: {{ IP_ADDRESS }}

What I did

I added a unit test for the format env that uses stdin (which fails) and fixed the problem by checking if the stdin is connected to a shell like environment and if thats not the case (for instance when data is piped into the command) to use the regular code that would handle stdin.

@kolypto kolypto merged commit d3d78db into kolypto:master Jul 18, 2019
@kolypto
Copy link
Copy Markdown
Owner

kolypto commented Jul 18, 2019

That's a really good one, @atomicptr ! Thank you so much :)

@kolypto
Copy link
Copy Markdown
Owner

kolypto commented Jul 18, 2019

Published to pip: j2cli=0.3.11

@kolypto
Copy link
Copy Markdown
Owner

kolypto commented Jul 19, 2019

@atomicptr unfortunately, I had to pull this release from pip because it has broken other people's builds: see #43

As it turned out, there was a good reason years ago not to include this sort of feature into j2cli: this is because in Linux, as it turns out, you cannot reliably determine whether there will be any data at stdin! Some environments (like Puppet, it seems) will open stdin just in case. Then, j2cli mistakenly thinks that it will be given some data, and swithes from the real environment to the one it never receives.

Info: https://stackoverflow.com/a/699459/134904

Therefore, if you want to feed environment variables into stdin, you'll need to tell j2cli explicitly that there will be data at stdin! Most applications do it by using the "-" instead of a filename.

And this is something I've just taught j2cli to do. The new j2cli==0.3.12b0 is already out: as beta. When people confirm that their builds are not failing, this will be the way to read dotenv from stdin:

$ j2 template.yml.j2 - < data.env

I hope it works for you :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants