Skip to content

Commit

Permalink
feat: add support for stdin to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
ejseqera committed Mar 4, 2024
1 parent 6059e8d commit f8269d5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
14 changes: 8 additions & 6 deletions seqerakit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import logging
import sys

from pathlib import Path
from seqerakit import seqeraplatform, helper, overwrite
from seqerakit.seqeraplatform import ResourceExistsError, ResourceCreationError
from seqerakit import __version__
Expand Down Expand Up @@ -66,7 +65,6 @@ def parse_args(args=None):
yaml_processing = parser.add_argument_group("YAML Processing Options")
yaml_processing.add_argument(
"yaml",
type=Path,
nargs="*",
help="One or more YAML files with Seqera Platform resource definitions.",
)
Expand Down Expand Up @@ -154,10 +152,14 @@ def main(args=None):
return

if not options.yaml:
logging.error(
" No YAML(s) provided. Please provide atleast one YAML configuration file."
)
sys.exit(1)
if sys.stdin.isatty():
logging.error(
" No YAML(s) provided and no input from stdin. Please provide "
"at least one YAML configuration file or pipe input from stdin."
)
sys.exit(1)
else:
options.yaml = [sys.stdin]

# Parse CLI arguments into a list
cli_args_list = options.cli_args.split() if options.cli_args else []
Expand Down
50 changes: 34 additions & 16 deletions seqerakit/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"""
import yaml
from seqerakit import utils
import sys


def parse_yaml_block(yaml_data, block_name):
Expand Down Expand Up @@ -56,24 +57,41 @@ def parse_all_yaml(file_paths, destroy=False):
# If multiple yamls, merge them into one dictionary
merged_data = {}

for file_path in file_paths:
with open(file_path, "r") as f:
data = yaml.safe_load(f)
# Special handling for stdin represented by "-"
if not file_paths or "-" in file_paths:
# Read YAML directly from stdin
data = yaml.safe_load(sys.stdin)
if data is None or not data:
raise ValueError(
" The input from stdin is empty or does not contain valid YAML data."
)

# Check if the YAML file is empty or contains no valid data
if data is None or not data:
raise ValueError(
f" The file '{file_path}' is empty or does not contain valid data."
)
merged_data.update(data)

for key, value in data.items():
if key in merged_data:
try:
merged_data[key].extend(value)
except AttributeError:
merged_data[key] = [merged_data[key], value]
else:
merged_data[key] = value
for file_path in file_paths:
if file_path == "-":
continue
try:
with open(file_path, "r") as f:
data = yaml.safe_load(f)
if data is None or not data:
raise ValueError(
f" The file '{file_path}' is empty or "
"does not contain valid data."
)
merged_data.update(data)
except FileNotFoundError:
print(f"Error: The file '{file_path}' was not found.")
sys.exit(1)

for key, value in data.items():
if key in merged_data:
try:
merged_data[key].extend(value)
except AttributeError:
merged_data[key] = [merged_data[key], value]
else:
merged_data[key] = value

block_names = list(merged_data.keys())

Expand Down

0 comments on commit f8269d5

Please sign in to comment.