Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add possibility to define data.json-path via env-var #135

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ $ jinja2 helloworld.tmpl data.json --format=json
$ cat data.json | jinja2 helloworld.tmpl
$ curl -s http://httpbin.org/ip | jinja2 helloip.tmpl
$ curl -s http://httpbin.org/ip | jinja2 helloip.tmpl > helloip.html
$ JINJA_INPUT_DATA_PATH=payload.json jinja2 helloworld.tmpl
```

## Install
Expand All @@ -28,6 +29,15 @@ Options:
template
```

## Reading input data path from environment variable

Set the value of the environment variable `JINJA_INPUT_DATA_PATH` to the path to your input data.
This way the input data get read from there and you don't have to specify it on the command line:

```
JINJA_INPUT_DATA_PATH=payload.json jinja2 helloworld.tmpl
```

## Optional YAML support
If `PyYAML` is present, you can use YAML as an input data source.

Expand Down
4 changes: 2 additions & 2 deletions jinja2cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def is_fd_alive(fd):
def cli(opts, args):
template_path, data = args
format = opts.format
if data in ("-", ""):
if data in ("-", "") and not "JINJA_INPUT_DATA_PATH" in os.environ:
if data == "-" or (data == "" and is_fd_alive(sys.stdin)):
data = sys.stdin.read()
if format == "auto":
Expand All @@ -302,7 +302,7 @@ def cli(opts, args):
else:
format = "json"
else:
path = os.path.join(os.getcwd(), os.path.expanduser(data))
path = os.path.join(os.environ["JINJA_INPUT_DATA_PATH"]) if "JINJA_INPUT_DATA_PATH" in os.environ else os.path.join(os.getcwd(), os.path.expanduser(data))
if format == "auto":
ext = os.path.splitext(path)[1][1:]
if has_format(ext):
Expand Down