From fde73f8e7aecba80efc6ae582f4e90c45e620229 Mon Sep 17 00:00:00 2001 From: Sebastian Gumprich Date: Fri, 22 Nov 2024 13:40:16 +0100 Subject: [PATCH] feat(cli): add possibility to define data.json-path via env-var --- README.md | 10 ++++++++++ jinja2cli/cli.py | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1e7f09c..ba24502 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/jinja2cli/cli.py b/jinja2cli/cli.py index 5a3dc0a..41eb63a 100644 --- a/jinja2cli/cli.py +++ b/jinja2cli/cli.py @@ -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": @@ -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):