|
| 1 | +import argparse |
| 2 | +from typing import Any, Union |
| 3 | + |
| 4 | +from rich.table import Table |
| 5 | + |
| 6 | +from dstack._internal.cli.commands import APIBaseCommand |
| 7 | +from dstack._internal.cli.utils.common import add_row_from_dict, console |
| 8 | +from dstack._internal.core.models.imports import Import |
| 9 | + |
| 10 | + |
| 11 | +class ImportCommand(APIBaseCommand): |
| 12 | + NAME = "import" |
| 13 | + DESCRIPTION = "Manage imports" |
| 14 | + |
| 15 | + def _register(self): |
| 16 | + super()._register() |
| 17 | + self._parser.set_defaults(subfunc=self._list) |
| 18 | + subparsers = self._parser.add_subparsers(dest="action") |
| 19 | + |
| 20 | + list_parser = subparsers.add_parser( |
| 21 | + "list", help="List imports", formatter_class=self._parser.formatter_class |
| 22 | + ) |
| 23 | + list_parser.set_defaults(subfunc=self._list) |
| 24 | + |
| 25 | + def _command(self, args: argparse.Namespace): |
| 26 | + super()._command(args) |
| 27 | + args.subfunc(args) |
| 28 | + |
| 29 | + def _list(self, args: argparse.Namespace): |
| 30 | + imports = self.api.client.imports.list(self.api.project) |
| 31 | + print_imports_table(imports) |
| 32 | + |
| 33 | + |
| 34 | +def print_imports_table(imports: list[Import]): |
| 35 | + table = Table(box=None) |
| 36 | + table.add_column("NAME", no_wrap=True) |
| 37 | + table.add_column("FLEETS") |
| 38 | + |
| 39 | + for imp in imports: |
| 40 | + name = f"{imp.export.project_name}/{imp.export.name}" |
| 41 | + fleets = ( |
| 42 | + ", ".join([f.name for f in imp.export.exported_fleets]) |
| 43 | + if imp.export.exported_fleets |
| 44 | + else "-" |
| 45 | + ) |
| 46 | + |
| 47 | + row: dict[Union[str, int], Any] = { |
| 48 | + "NAME": name, |
| 49 | + "FLEETS": fleets, |
| 50 | + } |
| 51 | + add_row_from_dict(table, row) |
| 52 | + |
| 53 | + console.print(table) |
| 54 | + console.print() |
0 commit comments