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

DATA parameter should directly support stdin #77

Open
kriswuollett opened this issue Jul 5, 2020 · 1 comment
Open

DATA parameter should directly support stdin #77

kriswuollett opened this issue Jul 5, 2020 · 1 comment

Comments

@kriswuollett
Copy link

The chevron (v0.13.1) program currently only supports standard files for the -d (DATA) parameter. It should support the commonly used convention of the dash, -, to represent stdin. The current workaround for me is to use the filename /dev/stdin on Linux, but would guess that some platforms may not support that. Example use case is piping in output from yq or jq that merges multiple data sources without creating temporary files.

@tekknolagi
Copy link
Contributor

It's not clear which of the parameters (--data, --template) should get stdin when passing -, I think. This is a fairly trivial patch (below), but this reads from stdin for both params (which seems broken):

diff --git a/chevron/main.py b/chevron/main.py
index 83bfa93..4e4fdff 100755
--- a/chevron/main.py
+++ b/chevron/main.py
@@ -18,7 +18,9 @@ except (ValueError, SystemError):  # python 2
 
 def main(template, data=None, **kwargs):
     with io.open(template, 'r', encoding='utf-8') as template_file:
-        if data is not None:
+        if data == "-":
+            data = json.load(sys.stdin)
+        elif data is not None:
             with io.open(data, 'r', encoding='utf-8') as data_file:
                 data = json.load(data_file)
         else:
@@ -39,6 +41,9 @@ def cli_main():
     import os
 
     def is_file_or_pipe(arg):
+        if arg == "-":
+            # Special case for passing stdin as `-`
+            return arg
         if not os.path.exists(arg) or os.path.isdir(arg):
             parser.error('The file {0} does not exist!'.format(arg))
         else:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants