mattrobenolt / jinja2-cli

CLI for Jinja2
BSD 2-Clause "Simplified" License
505 stars 181 forks source link

Piping to stdin sometimes does not work #113

Open ddribin opened 1 year ago

ddribin commented 1 year ago

Piping to stdin sometimes does not work. Let's take the httpbin.org example:

> cat helloip.tmpl
Your IP address is {{ origin }}.
> curl -s http://httpbin.org/ip | jinja2 helloip.tmpl
Your IP address is .

Yet, the curl command does output JSON:

> curl -s http://httpbin.org/ip
{
  "origin": "xx.xx.xx.127"
}

In fact saving this to a file and then using cat to pipe it to jinja2 does work:

> curl -s http://httpbin.org/ip > httpbin.json
> cat httpbin.json | jinja2 helloip.tmpl
Your IP address is xx.xx.xx.127.

Not sure why piping cat would be any different than piping curl.

I'm on macOS Ventura 13.3.1 (Intel) running Python installed from Homebrew and jinja2-cli installed into a venv.

> jinja2 --version
jinja2-cli v0.8.2
 - Jinja2 v3.1.2

> python --version
Python 3.11.3

> pip list
Package    Version
---------- -------
Jinja2     3.1.2
jinja2-cli 0.8.2
MarkupSafe 2.1.2
pip        23.0.1
setuptools 67.6.1

(Edited to remove my real IP address.)

ddribin commented 1 year ago

Oddly using - as the input works:

> curl -s http://httpbin.org/ip | jinja2 helloip.tmpl
Your IP address is .
> curl -s http://httpbin.org/ip | jinja2 helloip.tmpl -
Your IP address is xx.xx.xx.127.

(Edited to remove my real IP address.)

ece-mohammad commented 11 months ago

https://github.com/mattrobenolt/jinja2-cli/blob/cc6fc7eaeedcb3421495e6a63c7014214a955fb4/jinja2cli/cli.py#L291

yeah, looks like is_fd_alive(sys.stdin) returns False because select() returns a empty lists [], [], []. I guess sys.stdin.isatty() should work here, but I'm not too sure.