|
56 | 56 | RevisionOpt, |
57 | 57 | SearchOpt, |
58 | 58 | TokenOpt, |
| 59 | + VolumesOpt, |
59 | 60 | api_object_to_dict, |
60 | 61 | get_hf_api, |
61 | 62 | make_expand_properties_parser, |
| 63 | + parse_volumes, |
62 | 64 | typer_factory, |
63 | 65 | ) |
64 | 66 | from ._output import OutputFormatWithAuto, out |
|
81 | 83 | ] |
82 | 84 |
|
83 | 85 | spaces_cli = typer_factory(help="Interact with spaces on the Hub.") |
| 86 | +volumes_cli = typer_factory(help="Manage volumes for a Space on the Hub.") |
| 87 | +spaces_cli.add_typer(volumes_cli, name="volumes") |
84 | 88 |
|
85 | 89 |
|
86 | 90 | @spaces_cli.command( |
@@ -482,3 +486,80 @@ def _editor_open(local_path: str) -> int | Literal["no-tty", "no-editor"]: |
482 | 486 | command = [*shlex.split(editor_command), local_path] |
483 | 487 | res = subprocess.run(command, start_new_session=True) |
484 | 488 | return res.returncode |
| 489 | + |
| 490 | + |
| 491 | +@volumes_cli.command( |
| 492 | + "list | ls", |
| 493 | + examples=[ |
| 494 | + "hf spaces volumes ls username/my-space", |
| 495 | + ], |
| 496 | +) |
| 497 | +def volumes_ls( |
| 498 | + space_id: Annotated[str, typer.Argument(help="The space ID (e.g. `username/repo-name`).")], |
| 499 | + format: FormatWithAutoOpt = OutputFormatWithAuto.auto, |
| 500 | + token: TokenOpt = None, |
| 501 | +) -> None: |
| 502 | + """List volumes mounted in a Space.""" |
| 503 | + api = get_hf_api(token=token) |
| 504 | + info = api.space_info(space_id) |
| 505 | + if info.runtime is None: |
| 506 | + raise CLIError(f"Runtime not available for Space '{space_id}'.") |
| 507 | + volumes = info.runtime.volumes or [] |
| 508 | + items = [api_object_to_dict(v) for v in volumes] |
| 509 | + out.table(items) |
| 510 | + out.hint( |
| 511 | + f"Use `hf spaces volumes set {space_id} -v hf://<repo_type>/<repo_id>:/<mount_path>` to set volumes for a Space." |
| 512 | + ) |
| 513 | + |
| 514 | + |
| 515 | +@volumes_cli.command( |
| 516 | + "set", |
| 517 | + examples=[ |
| 518 | + "hf spaces volumes set username/my-space -v hf://models/username/my-model:/models", |
| 519 | + "hf spaces volumes set username/my-space -v hf://buckets/username/my-bucket:/data -v hf://datasets/username/my-dataset:/datasets:ro", |
| 520 | + ], |
| 521 | +) |
| 522 | +def volumes_set( |
| 523 | + space_id: Annotated[str, typer.Argument(help="The space ID (e.g. `username/repo-name`).")], |
| 524 | + volume: VolumesOpt = None, |
| 525 | + format: FormatWithAutoOpt = OutputFormatWithAuto.auto, |
| 526 | + token: TokenOpt = None, |
| 527 | +) -> None: |
| 528 | + """Set (replace) volumes for a Space.""" |
| 529 | + volumes = parse_volumes(volume) |
| 530 | + if not volumes: |
| 531 | + raise CLIError("At least one volume must be specified with -v/--volume.") |
| 532 | + api = get_hf_api(token=token) |
| 533 | + api.set_space_volumes(space_id, volumes=volumes) |
| 534 | + out.result("Volumes set", space_id=space_id, volumes=[v.to_hf_handle() for v in volumes]) |
| 535 | + out.hint(f"Use `hf spaces volumes ls {space_id}` to list volumes for a Space.") |
| 536 | + |
| 537 | + |
| 538 | +@volumes_cli.command( |
| 539 | + "delete", |
| 540 | + examples=[ |
| 541 | + "hf spaces volumes delete username/my-space", |
| 542 | + "hf spaces volumes delete username/my-space --yes", |
| 543 | + ], |
| 544 | +) |
| 545 | +def volumes_delete( |
| 546 | + space_id: Annotated[str, typer.Argument(help="The space ID (e.g. `username/repo-name`).")], |
| 547 | + yes: Annotated[ |
| 548 | + bool, |
| 549 | + typer.Option( |
| 550 | + "-y", |
| 551 | + "--yes", |
| 552 | + help="Answer Yes to prompt automatically.", |
| 553 | + ), |
| 554 | + ] = False, |
| 555 | + format: FormatWithAutoOpt = OutputFormatWithAuto.auto, |
| 556 | + token: TokenOpt = None, |
| 557 | +) -> None: |
| 558 | + """Remove all volumes from a Space.""" |
| 559 | + out.confirm(f"You are about to remove all volumes from Space '{space_id}'. Proceed?", yes=yes) |
| 560 | + api = get_hf_api(token=token) |
| 561 | + api.delete_space_volumes(space_id) |
| 562 | + out.result("Volumes deleted", space_id=space_id) |
| 563 | + out.hint( |
| 564 | + f"Use `hf spaces volumes set {space_id} -v hf://<repo_type>/<repo_id>:/<mount_path>` to set volumes for a Space." |
| 565 | + ) |
0 commit comments