Skip to content

Commit 6e0bc13

Browse files
authored
✨ Add unlink command to delete local FastAPI Cloud configuration (#80)
* ✨ Add reset command to delete local FastAPI Cloud configuration * Add tests * Lint * Update * Lint * Rename reset command to unlink * Lint
1 parent 8d5735b commit 6e0bc13

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

src/fastapi_cloud_cli/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .commands.env import env_app
55
from .commands.login import login
66
from .commands.logout import logout
7+
from .commands.unlink import unlink
78
from .commands.whoami import whoami
89
from .logging import setup_logging
910
from .utils.sentry import init_sentry
@@ -20,6 +21,7 @@
2021
app.command()(login)
2122
app.command()(logout)
2223
app.command()(whoami)
24+
app.command()(unlink)
2325

2426
app.add_typer(env_app, name="env")
2527

src/fastapi_cloud_cli/commands/deploy.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,13 @@ def deploy(
592592
"App not found. Make sure you're logged in the correct account."
593593
)
594594

595-
raise typer.Exit(1)
595+
if not app:
596+
toolkit.print_line()
597+
toolkit.print(
598+
"If you deleted this app, you can run [bold]fastapi unlink[/] to unlink the local configuration.",
599+
tag="tip",
600+
)
601+
raise typer.Exit(1)
596602

597603
logger.debug("Creating archive for deployment")
598604
archive_path = archive(path or Path.cwd()) # noqa: F841
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import logging
2+
import shutil
3+
from pathlib import Path
4+
from typing import Any
5+
6+
import typer
7+
8+
from fastapi_cloud_cli.utils.cli import get_rich_toolkit
9+
10+
logger = logging.getLogger(__name__)
11+
12+
13+
def unlink() -> Any:
14+
"""
15+
Unlink by deleting the `.fastapicloud` directory.
16+
"""
17+
with get_rich_toolkit(minimal=True) as toolkit:
18+
config_dir = Path.cwd() / ".fastapicloud"
19+
20+
if not config_dir.exists():
21+
toolkit.print(
22+
"No FastAPI Cloud configuration found in the current directory."
23+
)
24+
logger.debug(f"Configuration directory not found: {config_dir}")
25+
raise typer.Exit(1)
26+
27+
shutil.rmtree(config_dir)
28+
toolkit.print("FastAPI Cloud configuration has been unlinked successfully! 🚀")
29+
logger.debug(f"Deleted configuration directory: {config_dir}")

tests/test_cli_unlink.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from pathlib import Path
2+
from unittest.mock import patch
3+
4+
from typer.testing import CliRunner
5+
6+
from fastapi_cloud_cli.cli import app
7+
8+
runner = CliRunner()
9+
10+
11+
def test_unlink_removes_fastapicloud_dir(tmp_path: Path) -> None:
12+
config_dir = tmp_path / ".fastapicloud"
13+
config_dir.mkdir(parents=True)
14+
15+
cloud_json = config_dir / "cloud.json"
16+
cloud_json.write_text('{"app_id": "123", "team_id": "456"}')
17+
18+
readme_file = config_dir / "README.md"
19+
readme_file.write_text("# FastAPI Cloud Configuration")
20+
21+
gitignore_file = config_dir / ".gitignore"
22+
gitignore_file.write_text("*")
23+
24+
with patch("fastapi_cloud_cli.commands.unlink.Path.cwd", return_value=tmp_path):
25+
result = runner.invoke(app, ["unlink"])
26+
27+
assert result.exit_code == 0
28+
assert (
29+
"FastAPI Cloud configuration has been unlinked successfully! 🚀"
30+
in result.output
31+
)
32+
33+
assert not config_dir.exists()
34+
assert not cloud_json.exists()
35+
assert not readme_file.exists()
36+
assert not gitignore_file.exists()
37+
38+
39+
def test_unlink_when_no_configuration_exists(tmp_path: Path) -> None:
40+
config_dir = tmp_path / ".fastapicloud"
41+
assert not config_dir.exists()
42+
43+
with patch("fastapi_cloud_cli.commands.unlink.Path.cwd", return_value=tmp_path):
44+
result = runner.invoke(app, ["unlink"])
45+
46+
assert result.exit_code == 1
47+
assert (
48+
"No FastAPI Cloud configuration found in the current directory."
49+
in result.output
50+
)

0 commit comments

Comments
 (0)