Skip to content

Commit ab87b80

Browse files
committed
✨ Add support for creating secrets
1 parent 6f68eb5 commit ab87b80

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/fastapi_cloud_cli/commands/env.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ def set(
206206
help="A path to the folder containing the app you want to deploy"
207207
),
208208
] = None,
209+
secret: Annotated[
210+
bool,
211+
typer.Option(
212+
"--secret",
213+
help="Mark the environment variable as secret (value will be hidden)",
214+
),
215+
] = False,
209216
) -> Any:
210217
"""
211218
Set an environment variable for the app.
@@ -247,6 +254,9 @@ def set(
247254
assert value is not None
248255

249256
with handle_http_errors(progress):
250-
_set_environment_variable(app_config.app_id, name, value)
257+
_set_environment_variable(app_config.app_id, name, value, secret)
251258

252-
toolkit.print(f"Environment variable [bold]{name}[/] set.")
259+
if secret:
260+
toolkit.print(f"Secret environment variable [bold]{name}[/] set.")
261+
else:
262+
toolkit.print(f"Environment variable [bold]{name}[/] set.")

tests/test_env_set.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ def test_shows_a_message_if_app_is_not_configured(logged_in_cli: None) -> None:
4747
def test_shows_a_message_if_something_is_wrong(
4848
logged_in_cli: None, respx_mock: respx.MockRouter, configured_app: Path
4949
) -> None:
50-
respx_mock.post("/apps/123/environment-variables/").mock(return_value=Response(500))
50+
respx_mock.post(
51+
"/apps/123/environment-variables/",
52+
json={"name": "SOME_VAR", "value": "secret", "is_secret": False},
53+
).mock(return_value=Response(500))
5154

5255
with changing_dir(configured_app):
5356
result = runner.invoke(app, ["env", "set", "SOME_VAR", "secret"])
@@ -63,7 +66,10 @@ def test_shows_a_message_if_something_is_wrong(
6366
def test_shows_message_when_it_sets(
6467
logged_in_cli: None, respx_mock: respx.MockRouter, configured_app: Path
6568
) -> None:
66-
respx_mock.post("/apps/123/environment-variables/").mock(return_value=Response(200))
69+
respx_mock.post(
70+
"/apps/123/environment-variables/",
71+
json={"name": "SOME_VAR", "value": "secret", "is_secret": False},
72+
).mock(return_value=Response(200))
6773

6874
with changing_dir(configured_app):
6975
result = runner.invoke(app, ["env", "set", "SOME_VAR", "secret"])
@@ -78,7 +84,10 @@ def test_asks_for_name_and_value(
7884
) -> None:
7985
steps = [*"SOME_VAR", Keys.ENTER, *"secret", Keys.ENTER]
8086

81-
respx_mock.post("/apps/123/environment-variables/").mock(return_value=Response(200))
87+
respx_mock.post(
88+
"/apps/123/environment-variables/",
89+
json={"name": "SOME_VAR", "value": "secret", "is_secret": False},
90+
).mock(return_value=Response(200))
8291

8392
with (
8493
changing_dir(configured_app),
@@ -94,3 +103,19 @@ def test_asks_for_name_and_value(
94103
assert "Environment variable SOME_VAR set" in result.output
95104

96105
assert "*" * 6 in result.output
106+
107+
108+
@pytest.mark.respx(base_url=settings.base_api_url)
109+
def test_sets_secret_flag(
110+
logged_in_cli: None, respx_mock: respx.MockRouter, configured_app: Path
111+
) -> None:
112+
respx_mock.post(
113+
"/apps/123/environment-variables/",
114+
json={"name": "SOME_VAR", "value": "secret", "is_secret": True},
115+
).mock(return_value=Response(200))
116+
117+
with changing_dir(configured_app):
118+
result = runner.invoke(app, ["env", "set", "SOME_VAR", "secret", "--secret"])
119+
120+
assert result.exit_code == 0
121+
assert "Secret environment variable SOME_VAR set" in result.output

0 commit comments

Comments
 (0)