Skip to content

Commit

Permalink
stdin/stdout handling for CLI (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
drnextgis authored Apr 19, 2024
1 parent c6369d7 commit e91a947
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ An example of running it might look like:
src/mytask/mytask.py run --logging DEBUG --local my-input-file.json
Payload can be read from stdin:

.. code-block:: console
cat my-input-file.json | src/mytask/mytask.py run --logging DEBUG --local
The first argument is the command, of which the only option currently is `run`.

Expand Down
17 changes: 12 additions & 5 deletions stactask/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@ def parse_args(cls, args: List[str]) -> Dict[str, Any]:
help="Process STAC Item Collection",
)
parser.add_argument(
"input", help="Full path of item collection to process (s3 or local)"
"input",
nargs="?",
help="Full path of item collection to process (s3 or local)",
)

parser.add_argument(
Expand Down Expand Up @@ -532,18 +534,23 @@ def cli(cls) -> None:
logging.getLogger(ql).propagate = False

if cmd == "run":
href = args.pop("input")
href = args.pop("input", None)
href_out = args.pop("output", None)

# read input
with fsspec.open(href) as f:
payload = json.loads(f.read())
if href is None:
payload = json.load(sys.stdin)
else:
with fsspec.open(href) as f:
payload = json.loads(f.read())

# run task handler
payload_out = cls.handler(payload, **args)

# write output
if href_out is not None:
if href_out is None:
json.dump(payload_out, sys.stdout)
else:
with fsspec.open(href_out, "w") as f:
f.write(json.dumps(payload_out))

Expand Down

0 comments on commit e91a947

Please sign in to comment.