Skip to content

Commit 21e29c5

Browse files
committed
server (env) override
1 parent 5f46fea commit 21e29c5

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ To run redfetch from the command line:
6666
> ### 📦 Resource Management
6767
> - `update` - Update all watched and special resources
6868
> - `--force` / `-f` - Force re-download of all watched resources
69+
> - `--server` / `-s` - Switch to this server before updating (`LIVE`, `TEST`, `EMU`)
6970
> - `download <ID_OR_URL>` - Download a specific resource by ID or URL
7071
> - `--force` / `-f` - Force re-download by resetting this resource's download date
72+
> - `--server` / `-s` - Switch to this server before downloading (`LIVE`, `TEST`, `EMU`)
7173
> - `list` - List resources and dependencies in the cache database
7274
> - `reset` - Reset download dates for watched resources
7375
>

src/redfetch/main.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ def parse_resource_id_or_fail(value: str) -> str:
5656
return parsed
5757

5858

59+
def _apply_server_override(server: "Optional[Env]" = None) -> None:
60+
if server is not None and server.value != config.settings.ENV:
61+
config.switch_environment(server.value)
62+
63+
5964
def _initialize_auth():
6065
"""Initialize configuration, update check, and auth (no DB, no network)."""
6166
config.initialize_config()
@@ -65,9 +70,10 @@ def _initialize_auth():
6570
auth.authorize()
6671

6772

68-
def initialize_db_only():
73+
def initialize_db_only(server: "Optional[Env]" = None):
6974
"""Initialize configuration, auth, and local cache database (no network)."""
7075
_initialize_auth()
76+
_apply_server_override(server)
7177
db_name = f"{config.settings.ENV}_resources.db"
7278
store.initialize_db(db_name)
7379
db_path = store.get_db_path(db_name)
@@ -247,8 +253,9 @@ async def download_command_async(db_name: str, db_path: str, id_or_url: str, for
247253
)
248254
def update_command(
249255
force: bool = typer.Option(False, "--force", "-f", help="Force re-download of all watched resources."),
256+
server: Optional[Env] = typer.Option(None, "--server", "-s", case_sensitive=False, help="Switch to this server before updating ([green]LIVE[/green], [yellow]TEST[/yellow], [cyan]EMU[/cyan])."),
250257
):
251-
db_name, db_path = initialize_db_only()
258+
db_name, db_path = initialize_db_only(server=server)
252259
asyncio.run(update_command_async(db_name=db_name, db_path=db_path, force=force))
253260

254261

@@ -260,8 +267,9 @@ def update_command(
260267
def download(
261268
id_or_url: str = typer.Argument(..., metavar="ID_OR_URL", help="RedGuides resource ID or URL"),
262269
force: bool = typer.Option(False, "--force", "-f", help="Force re-download by resetting this resource's download date."),
270+
server: Optional[Env] = typer.Option(None, "--server", "-s", case_sensitive=False, help="Switch to this server type before downloading ([green]LIVE[/green], [yellow]TEST[/yellow], [cyan]EMU[/cyan])."),
263271
):
264-
db_name, db_path = initialize_db_only()
272+
db_name, db_path = initialize_db_only(server=server)
265273
asyncio.run(download_command_async(db_name=db_name, db_path=db_path, id_or_url=id_or_url, force=force))
266274

267275

@@ -295,6 +303,10 @@ def check_command(
295303
None, "--caller-resource-id",
296304
help="Resource ID of the calling program (e.g. 1974 for Very Vanilla MQ Live).",
297305
),
306+
server: Optional[Env] = typer.Option(
307+
None, "--server", "-s", case_sensitive=False,
308+
help="Switch to this server before checking ([green]LIVE[/green], [yellow]TEST[/yellow], [cyan]EMU[/cyan]).",
309+
),
298310
):
299311
real_stdout = sys.stdout
300312
sys.stdout = sys.stderr
@@ -311,6 +323,7 @@ def check_command(
311323

312324
# Phase 2 — init + check
313325
config.initialize_config()
326+
_apply_server_override(server)
314327
auth.initialize_keyring()
315328
db_name = f"{config.settings.ENV}_resources.db"
316329
store.initialize_db(db_name)

tests/test_env_selection.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Tests for environment selection via --server."""
2+
from types import SimpleNamespace
3+
4+
import pytest
5+
6+
from redfetch.main import Env, _apply_server_override
7+
from redfetch import config
8+
9+
10+
@pytest.fixture
11+
def fake_config(monkeypatch):
12+
"""Patch config.settings with a fake and capture switch_environment calls."""
13+
fake = SimpleNamespace(ENV="LIVE")
14+
monkeypatch.setattr(config, "settings", fake)
15+
switched = []
16+
17+
def fake_switch(new_env):
18+
fake.ENV = new_env
19+
switched.append(new_env)
20+
21+
monkeypatch.setattr(config, "switch_environment", fake_switch)
22+
return fake, switched
23+
24+
25+
def test_server_flag_switches_environment(fake_config):
26+
"""--server EMU while on LIVE must trigger a persistent switch to EMU."""
27+
settings, switched = fake_config
28+
_apply_server_override(server=Env.EMU)
29+
assert switched == ["EMU"]
30+
assert settings.ENV == "EMU"

0 commit comments

Comments
 (0)