|
1 | 1 | import argparse |
| 2 | +from typing import Any, Union |
2 | 3 |
|
3 | 4 | from requests import HTTPError |
4 | 5 | from rich.table import Table |
5 | 6 |
|
6 | 7 | import dstack.api.server |
7 | 8 | from dstack._internal.cli.commands import BaseCommand |
8 | | -from dstack._internal.cli.utils.common import confirm_ask, console |
| 9 | +from dstack._internal.cli.utils.common import add_row_from_dict, confirm_ask, console |
9 | 10 | from dstack._internal.core.errors import ClientError, CLIError |
10 | 11 | from dstack._internal.core.services.configs import ConfigManager |
11 | 12 | from dstack._internal.utils.logging import get_logger |
@@ -58,6 +59,10 @@ def _register(self): |
58 | 59 | # List subcommand |
59 | 60 | list_parser = subparsers.add_parser("list", help="List configured projects") |
60 | 61 | list_parser.set_defaults(subfunc=self._list) |
| 62 | + for parser in [self._parser, list_parser]: |
| 63 | + parser.add_argument( |
| 64 | + "-v", "--verbose", action="store_true", help="Show more information" |
| 65 | + ) |
61 | 66 |
|
62 | 67 | # Set default subcommand |
63 | 68 | set_default_parser = subparsers.add_parser("set-default", help="Set default project") |
@@ -122,30 +127,32 @@ def _list(self, args: argparse.Namespace): |
122 | 127 | table = Table(box=None) |
123 | 128 | table.add_column("PROJECT", style="bold", no_wrap=True) |
124 | 129 | table.add_column("URL", style="grey58") |
125 | | - table.add_column("USER", style="grey58") |
| 130 | + if args.verbose: |
| 131 | + table.add_column("USER", style="grey58") |
126 | 132 | table.add_column("DEFAULT", justify="center") |
127 | 133 |
|
128 | 134 | for project_config in config_manager.list_project_configs(): |
129 | 135 | project_name = project_config.name |
130 | 136 | is_default = project_name == default_project.name if default_project else False |
131 | | - |
132 | | - # Get username from API |
133 | | - try: |
134 | | - api_client = dstack.api.server.APIClient( |
135 | | - base_url=project_config.url, token=project_config.token |
136 | | - ) |
137 | | - user_info = api_client.users.get_my_user() |
138 | | - username = user_info.username |
139 | | - except ClientError: |
140 | | - username = "(invalid token)" |
141 | | - |
142 | | - table.add_row( |
143 | | - project_name, |
144 | | - project_config.url, |
145 | | - username, |
146 | | - "✓" if is_default else "", |
147 | | - style="bold" if is_default else None, |
148 | | - ) |
| 137 | + row: dict[Union[str, int], Any] = { |
| 138 | + "PROJECT": project_name, |
| 139 | + "URL": project_config.url, |
| 140 | + "DEFAULT": "✓" if is_default else "", |
| 141 | + } |
| 142 | + |
| 143 | + if args.verbose: |
| 144 | + # Get username from API |
| 145 | + try: |
| 146 | + api_client = dstack.api.server.APIClient( |
| 147 | + base_url=project_config.url, token=project_config.token |
| 148 | + ) |
| 149 | + user_info = api_client.users.get_my_user() |
| 150 | + username = user_info.username |
| 151 | + except ClientError: |
| 152 | + username = "(invalid token)" |
| 153 | + row["USER"] = username |
| 154 | + |
| 155 | + add_row_from_dict(table, row, style="bold" if is_default else None) |
149 | 156 |
|
150 | 157 | console.print(table) |
151 | 158 |
|
|
0 commit comments