Coverage for src/fastapi_cloud_cli/commands/domains/get.py: 100%
42 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-11-09 00:25 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-11-09 00:25 +0000
1from typing import Annotated, Any 1abcdefg
3import typer 1abcdefg
4from pydantic import BaseModel, Field 1abcdefg
5from rich_toolkit import RichToolkit 1abcdefg
7from fastapi_cloud_cli.api import APIClient, CustomDomain 1abcdefg
8from fastapi_cloud_cli.commands._auth import UserCommand, get_user_command_context 1abcdefg
9from fastapi_cloud_cli.commands.domains._app import domains_app 1abcdefg
10from fastapi_cloud_cli.commands.domains._shared import ( 1abcdefg
11 _find_custom_domain,
12 _select_custom_domain,
13)
14from fastapi_cloud_cli.commands.domains.rendering import render_custom_domain_details 1abcdefg
15from fastapi_cloud_cli.utils.apps import resolve_app_id_or_fail 1abcdefg
16from fastapi_cloud_cli.utils.execution import JsonOutputOption 1abcdefg
19class CustomDomainGetOutput(BaseModel): 1abcdefg
20 app_id: str 1abcdefg
21 domain: CustomDomain 1abcdefg
22 show_title: Annotated[bool, Field(exclude=True)] = True 1abcdefg
25def _render_custom_domain_get_output( 1abocdpefgq
26 data: CustomDomainGetOutput,
27 toolkit: RichToolkit,
28) -> None:
29 if data.show_title: 1rshtuvBCjDoEFGkHIJKLlMpNwxiyzAOPmQRSTUnVqW
30 toolkit.print_title("custom domains") 1rstuvBCDoEFGHIJKLMpNwxyzAOPQRSTUVqW
31 toolkit.print_line() 1rstuvBCDoEFGHIJKLMpNwxyzAOPQRSTUVqW
33 render_custom_domain_details(data.domain, toolkit) 1rshtuvBCjDoEFGkHIJKLlMpNwxiyzAOPmQRSTUnVqW
36@domains_app.command("get", cls=UserCommand) 1abcdefg
37def get_domain( 1ab=cd?efg@
38 ctx: typer.Context,
39 domain: Annotated[
40 str | None,
41 typer.Argument(
42 help="Hostname or ID of the custom domain to return.",
43 ),
44 ] = None,
45 app_id: Annotated[
46 str | None,
47 typer.Option(
48 "--app-id",
49 help="ID of the app that owns the custom domain.",
50 ),
51 ] = None,
52 json_output: JsonOutputOption = False,
53) -> Any:
54 """
55 Get a custom domain for an app.
56 """
57 toolkit = get_user_command_context(ctx).toolkit 1r+shtu64XvB,CjDo%7ZEF-GkHI'80JK.LlMp(91Nw/xiyz!5YAO:PmQR)#2ST;UnVq*$3W
58 app_id = resolve_app_id_or_fail(toolkit, app_id=app_id) 1r+shtu64XvB,CjDo%7ZEF-GkHI'80JK.LlMp(91Nw/xiyz!5YAO:PmQR)#2ST;UnVq*$3W
59 domain_was_provided = domain is not None 1r+shtu64XvB,CjDo%7ZEF-GkHI'80JK.LlMp(91Nw/xiyz!5YAO:PmQR)#2ST;UnVq*$3W
61 if domain is None and toolkit.mode == "json": 1r+shtu64XvB,CjDo%7ZEF-GkHI'80JK.LlMp(91Nw/xiyz!5YAO:PmQR)#2ST;UnVq*$3W
62 toolkit.fail( 1+,-./:;
63 "missing_required_input",
64 "Custom domain is required.",
65 hint="Pass DOMAIN to choose a custom domain.",
66 )
68 with APIClient() as client: 1rshtu64XvBCjDo%7ZEFGkHI'80JKLlMp(91Nwxiyz!5YAOPmQR)#2STUnVq*$3W
69 with ( 1rshtu64Xvwxiyz!5YA
70 toolkit.progress(
71 title="Fetching custom domains",
72 transient=True,
73 ) as progress,
74 client.handle_http_errors(
75 progress,
76 default_message=(
77 "Error fetching custom domains. Please try again later."
78 ),
79 not_found_message="App not found.",
80 toolkit=toolkit,
81 ),
82 ):
83 domains = client.get_custom_domains(app_id=app_id).data 1rshtu64XvBCjDo%7ZEFGkHI'80JKLlMp(91Nwxiyz!5YAOPmQR)#2STUnVq*$3W
85 selected_domain: CustomDomain | None
87 if domain is None: 1rshtu64XvBCjDo%7ZEFGkHI'80JKLlMp(91Nwxiyz!5YAOPmQR)#2STUnVq*$3W
88 toolkit.print_title("custom domains") 1hXjZk0l1iYm2n3
89 toolkit.print_line() 1hXjZk0l1iYm2n3
91 if not domains: 1hXjZk0l1iYm2n3
92 toolkit.print("No custom domains found.", bullet=False) 1XZ01Y23
93 return 1XZ01Y23
95 selected_domain = _select_custom_domain( 1hjklimn
96 toolkit,
97 domains,
98 prompt="Select the custom domain to get:",
99 )
100 toolkit.print_line() 1hjklimn
101 else:
102 if (selected_domain := _find_custom_domain(domains, domain)) is None: 1rstu64vBCDo%7EFGHI'8JKLMp(9Nwxyz!5AOPQR)#STUVq*$W
103 toolkit.fail( 16%'(!)*
104 "not_found",
105 f"Custom domain {domain} not found.",
106 hint=(
107 "Run `fastapi cloud domains list` to see available custom domains."
108 ),
109 )
111 assert selected_domain is not None 1rshtu4vBCjDo7EFGkHI8JKLlMp9Nwxiyz5AOPmQR#STUnVq$W
113 toolkit.success( 1rshtu4vBCjDo7EFGkHI8JKLlMp9Nwxiyz5AOPmQR#STUnVq$W
114 CustomDomainGetOutput(
115 app_id=app_id,
116 domain=selected_domain,
117 show_title=domain_was_provided,
118 ),
119 render_output=_render_custom_domain_get_output,
120 )