Coverage for src/fastapi_cloud_cli/commands/env/get.py: 100%
51 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 pathlib import Path 1vwxyzAB
2from typing import Annotated, Any 1vwxyzAB
4import typer 1vwxyzAB
5from pydantic import BaseModel, Field 1vwxyzAB
6from rich.table import Table 1vwxyzAB
7from rich_toolkit import RichToolkit 1vwxyzAB
8from rich_toolkit.menu import Option 1vwxyzAB
10from fastapi_cloud_cli.api import APIClient, EnvironmentVariable 1vwxyzAB
11from fastapi_cloud_cli.commands._auth import UserCommand, get_user_command_context 1vwxyzAB
12from fastapi_cloud_cli.commands.env._app import env_app 1vwxyzAB
13from fastapi_cloud_cli.commands.env._shared import ( 1vwxyzAB
14 _find_environment_variable,
15 _format_env_var_value,
16)
17from fastapi_cloud_cli.utils.apps import resolve_app_id_or_fail 1vwxyzAB
18from fastapi_cloud_cli.utils.execution import JsonOutputOption 1vwxyzAB
21class EnvironmentVariableGetOutput(BaseModel): 1vwxyzAB
22 app_id: str 1vwxyzAB
23 variable: EnvironmentVariable 1vwxyzAB
24 show_tag: Annotated[bool, Field(exclude=True)] = True 1vwxyzAB
27def _render_environment_variable_get_output( 1vwhxyizABj
28 data: EnvironmentVariableGetOutput, toolkit: RichToolkit
29) -> None:
30 variable = data.variable 1aklbhmcnodipeqrfstgju
31 table = Table.grid(padding=(0, 2), pad_edge=False) 1aklbhmcnodipeqrfstgju
32 table.add_column(no_wrap=True) 1aklbhmcnodipeqrfstgju
33 table.add_column() 1aklbhmcnodipeqrfstgju
34 table.add_row("name:", variable.name) 1aklbhmcnodipeqrfstgju
35 table.add_row("value:", _format_env_var_value(variable)) 1aklbhmcnodipeqrfstgju
37 if data.show_tag: 1aklbhmcnodipeqrfstgju
38 toolkit.print_title("environment variables") 1klhmnoipqrstju
40 toolkit.print_line() 1aklbhmcnodipeqrfstgju
41 toolkit.print(table, bullet=False) 1aklbhmcnodipeqrfstgju
44@env_app.command("get", cls=UserCommand) 1vwxyzAB
45def get_variable( 1vw#xy$zAB%
46 ctx: typer.Context,
47 name: Annotated[
48 str | None,
49 typer.Argument(
50 help="The name of the environment variable to return.",
51 ),
52 ] = None,
53 path: Annotated[
54 Path | None,
55 typer.Option(
56 "--path",
57 help=(
58 "Path to the directory with your app's pyproject.toml "
59 "(defaults to current directory)"
60 ),
61 ),
62 ] = None,
63 app_id: Annotated[
64 str | None,
65 typer.Option(
66 "--app-id",
67 help="ID of the app whose environment variable should be returned.",
68 ),
69 ] = None,
70 json_output: JsonOutputOption = False,
71) -> Any:
72 """
73 Get an environment variable for the app.
74 """
76 toolkit = get_user_command_context(ctx).toolkit 1X4akJKlCY5bhLMmDZ6cnNOoE07diPQpF18eqRSrG29fsTUtH3!gjVWuI
78 target_app_id = resolve_app_id_or_fail(toolkit, app_id=app_id, path=path) 1X4akJKlCY5bhLMmDZ6cnNOoE07diPQpF18eqRSrG29fsTUtH3!gjVWuI
79 name_provided = name is not None 1X4akJKlCY5bhLMmDZ6cnNOoE07diPQpF18eqRSrG29fsTUtH3!gjVWuI
81 if name is None and toolkit.mode == "json": 1X4akJKlCY5bhLMmDZ6cnNOoE07diPQpF18eqRSrG29fsTUtH3!gjVWuI
82 toolkit.fail( 1456789!
83 "missing_required_input",
84 "Environment variable name is required.",
85 hint="Pass NAME to choose an environment variable.",
86 )
88 with APIClient() as client: 1XakJKlCYbhLMmDZcnNOoE0diPQpF1eqRSrG2fsTUtH3gjVWuI
89 with toolkit.progress( 1XakJKlCYbhLMmDZcnNOoE0diPQpF1eqRSrG2fsTUtH3gjVWuI
90 "Fetching environment variables...", transient=True
91 ) as progress:
92 with client.handle_http_errors(progress): 1XakJKlCYbhLMmDZcnNOoE0diPQpF1eqRSrG2fsTUtH3gjVWuI
93 environment_variables = client.get_environment_variables( 1XakJKlCYbhLMmDZcnNOoE0diPQpF1eqRSrG2fsTUtH3gjVWuI
94 app_id=target_app_id
95 )
97 if name is None: 1XakJKlCYbhLMmDZcnNOoE0diPQpF1eqRSrG2fsTUtH3gjVWuI
98 toolkit.print_title("environment variables") 1aCbDcEdFeGfHgI
99 toolkit.print_line() 1aCbDcEdFeGfHgI
101 if not environment_variables.data: 1aCbDcEdFeGfHgI
102 toolkit.print("No environment variables found.", bullet=False) 1CDEFGHI
103 return 1CDEFGHI
105 name = toolkit.ask( 1abcdefg
106 "Select the environment variable to get:",
107 options=[
108 Option({"name": env_var.name, "value": env_var.name})
109 for env_var in environment_variables.data
110 ],
111 bullet=False,
112 )
114 variable = _find_environment_variable(environment_variables.data, name) 1XakJKlYbhLMmZcnNOo0diPQp1eqRSr2fsTUt3gjVWu
116 if variable is None: 1XakJKlYbhLMmZcnNOo0diPQp1eqRSr2fsTUt3gjVWu
117 toolkit.fail( 1XYZ0123
118 "not_found",
119 f"Environment variable {name} not found.",
120 hint="Run `fastapi cloud env list` to see available variables.",
121 )
122 assert variable is not None 1akJKlbhLMmcnNOodiPQpeqRSrfsTUtgjVWu
124 toolkit.success( 1akJKlbhLMmcnNOodiPQpeqRSrfsTUtgjVWu
125 EnvironmentVariableGetOutput(
126 app_id=target_app_id,
127 variable=variable,
128 show_tag=name_provided,
129 ),
130 render_output=_render_environment_variable_get_output,
131 )