From b9b3f558a5c0a5667383b73ebf538613d599373a Mon Sep 17 00:00:00 2001 From: Sam Minot Date: Mon, 16 Nov 2026 11:02:20 -0800 Subject: [PATCH] Add --tag option to cirro upload Tags are sent as keyless values in the dataset creation request, so a dataset uploaded from the CLI carries its tags from the start. Interactive mode asks for optional comma-separated tags after the description. Co-Authored-By: Claude Opus 5.5 --- README.md | 4 +- cirro/cli/cli.py | 4 ++ cirro/cli/controller.py | 5 +- cirro/cli/interactive/upload_args.py | 13 +++++ cirro/cli/models.py | 1 + tests/test_cli_upload.py | 75 ++++++++++++++++++++++++++++ 6 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 tests/test_cli_upload.py diff --git a/README.md b/README.md index 2226d57..c013fe2 100644 --- a/README.md +++ b/README.md @@ -72,13 +72,14 @@ Options: --data-type, --process TEXT Name or ID of the data type (--process is deprecated) --data-directory TEXT Directory you wish to upload --file TEXT Relative path of the file(s) to upload (optional, can be used multiple times) + --tag TEXT Tag to apply to the dataset (optional, can be used multiple times) -i, --interactive Gather arguments interactively --include-hidden Include hidden files in the upload (e.g., files starting with .) --help Show this message and exit. ``` ```bash -$ cirro upload --project "Test Project 1" --name "test" --file "sample1.fastq.gz" --file "sample2.fastq.gz" --data-directory "~/data" --data-type "Paired DNAseq (FASTQ)" +$ cirro upload --project "Test Project 1" --name "test" --file "sample1.fastq.gz" --file "sample2.fastq.gz" --data-directory "~/data" --data-type "Paired DNAseq (FASTQ)" --tag "study-x" --tag "batch: 2" ``` #### Resuming an upload: @@ -188,6 +189,7 @@ $ cirro upload --interactive ? What type of files? Illumina Sequencing Run ? What is the name of this dataset? test ? Enter a description of the dataset (optional) +? Enter tags for the dataset, separated by commas (optional) study-x, batch: 2 ``` ## Python Usage diff --git a/cirro/cli/cli.py b/cirro/cli/cli.py index b4e9dff..d41736c 100644 --- a/cirro/cli/cli.py +++ b/cirro/cli/cli.py @@ -101,6 +101,10 @@ def download(**kwargs): help='Relative path of the file(s) to upload (optional, can be used multiple times)', default=[], multiple=True) +@click.option('--tag', + help='Tag to apply to the dataset (optional, can be used multiple times)', + default=[], + multiple=True) @click.option('-i', '--interactive', help='Gather arguments interactively', is_flag=True, default=False) diff --git a/cirro/cli/controller.py b/cirro/cli/controller.py index 10d6f31..79af7a0 100644 --- a/cirro/cli/controller.py +++ b/cirro/cli/controller.py @@ -4,7 +4,7 @@ import sys from pathlib import Path -from cirro_api_client.v1.models import UploadDatasetRequest, Status, Executor +from cirro_api_client.v1.models import UploadDatasetRequest, Status, Executor, Tag from cirro.cirro_client import CirroApi from cirro.cli.interactive.auth_args import gather_auth_config @@ -93,7 +93,8 @@ def run_ingest(input_params: UploadArguments, interactive=False): process_id=process.id, name=input_params['name'], description=input_params['description'], - expected_files=files + expected_files=files, + tags=[Tag(value=tag) for tag in input_params['tag']] ) project_id = get_id_from_name(projects, input_params['project']) diff --git a/cirro/cli/interactive/upload_args.py b/cirro/cli/interactive/upload_args.py index 5566dd4..4492e79 100644 --- a/cirro/cli/interactive/upload_args.py +++ b/cirro/cli/interactive/upload_args.py @@ -52,6 +52,18 @@ def ask_description(input_value: str) -> str: return answers['description'] +def ask_tags(input_value: List[str]) -> List[str]: + tags_prompt = { + 'type': 'input', + 'name': 'tags', + 'message': 'Enter tags for the dataset, separated by commas (optional)', + 'default': ', '.join(input_value or []) + } + + answers = prompt_wrapper(tags_prompt) + return [tag.strip() for tag in answers['tags'].split(',') if tag.strip()] + + def confirm_data_files(data_directory: str, files: List[str]): stats = get_files_stats([ Path(data_directory) / file @@ -174,4 +186,5 @@ def gather_upload_arguments(input_params: UploadArguments, projects: List[Projec default_name = input_params.get('name') or data_directory_name input_params['name'] = ask_name(default_name) input_params['description'] = ask_description(input_params.get('description')) + input_params['tag'] = ask_tags(input_params.get('tag')) return input_params, files diff --git a/cirro/cli/models.py b/cirro/cli/models.py index 19b1260..0630f00 100644 --- a/cirro/cli/models.py +++ b/cirro/cli/models.py @@ -20,6 +20,7 @@ class UploadArguments(TypedDict): include_hidden: bool interactive: bool file: Optional[list[str]] + tag: Optional[list[str]] threads: int diff --git a/tests/test_cli_upload.py b/tests/test_cli_upload.py new file mode 100644 index 0000000..172b91e --- /dev/null +++ b/tests/test_cli_upload.py @@ -0,0 +1,75 @@ +import tempfile +import unittest +from pathlib import Path +from unittest.mock import MagicMock, patch + +from click.testing import CliRunner + +from cirro.cli.cli import run +from cirro.cli.interactive.upload_args import ask_tags + + +def _named(id_, name): + item = MagicMock(id=id_) + item.name = name + return item + + +class TestUploadTags(unittest.TestCase): + def setUp(self): + self.data_dir = tempfile.TemporaryDirectory() + Path(self.data_dir.name, "sample.fastq.gz").write_text("reads") + + self.cirro = MagicMock() + self.cirro.projects.list.return_value = [_named("project-1", "Test Project")] + process = _named("process-1", "Paired DNAseq (FASTQ)") + self.cirro.processes.list.return_value = [process] + self.cirro.processes.get.return_value = process + + def tearDown(self): + self.data_dir.cleanup() + + def _upload(self, *extra_args): + args = [ + "upload", + "--project", "Test Project", + "--data-type", "Paired DNAseq (FASTQ)", + "--name", "test", + "--data-directory", self.data_dir.name, + *extra_args + ] + with patch("cirro.cli.controller._init_cirro_client", return_value=self.cirro): + result = CliRunner().invoke(run, args, catch_exceptions=False) + self.assertEqual(result.exit_code, 0, result.output) + return self.cirro.datasets.create.call_args.kwargs["upload_request"] + + def test_tags_passed_to_create_request(self): + request = self._upload("--tag", "microbiome-portal: metaphlan", "--tag", "study-x") + tags = request.to_dict()["tags"] + self.assertEqual([tag["value"] for tag in tags], ["microbiome-portal: metaphlan", "study-x"]) + # Tags are keyless; `key` is deprecated + self.assertTrue(all("key" not in tag for tag in tags)) + + def test_no_tags(self): + request = self._upload() + self.assertEqual(request.tags, []) + + +class TestAskTags(unittest.TestCase): + def _ask(self, answer, input_value=None): + with patch("cirro.cli.interactive.upload_args.prompt_wrapper", return_value={"tags": answer}) as prompt: + tags = ask_tags(input_value) + return tags, prompt.call_args.args[0]["default"] + + def test_splits_and_strips(self): + tags, _ = self._ask(" microbiome-portal: wgs-reads , study-x,, ") + self.assertEqual(tags, ["microbiome-portal: wgs-reads", "study-x"]) + + def test_blank_answer_gives_no_tags(self): + tags, default = self._ask("") + self.assertEqual(tags, []) + self.assertEqual(default, "") + + def test_defaults_to_command_line_tags(self): + _, default = self._ask("a, b", input_value=("a", "b")) + self.assertEqual(default, "a, b")