Skip to content

Commit a17edfd

Browse files
committed
Add host option to remove need for client configuration
1 parent 65b7e75 commit a17edfd

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

src/blueapi/cli/cli.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def is_str_dict(val: Any) -> TypeGuard[TaskParameters]:
8080
invoke_without_command=True, context_settings={"auto_envvar_prefix": "BLUEAPI"}
8181
)
8282
@click.version_option(version=__version__, prog_name="blueapi")
83+
@click.option("-H", "--host", type=str)
8384
@click.option(
8485
"-c", "--config", type=Path, help="Path to configuration YAML file", multiple=True
8586
)
@@ -99,7 +100,10 @@ def is_str_dict(val: Any) -> TypeGuard[TaskParameters]:
99100
)
100101
@click.pass_context
101102
def main(
102-
ctx: click.Context, config: tuple[Path, ...], log_level: str | None = None
103+
ctx: click.Context,
104+
config: tuple[Path, ...],
105+
host: str | None = None,
106+
log_level: str | None = None,
103107
) -> None:
104108
# if no command is supplied, run with the options passed
105109

@@ -111,7 +115,8 @@ def main(
111115
config_loader.use_values_from_yaml(*config)
112116
except FileNotFoundError as fnfe:
113117
raise ClickException(f"Config file not found: {fnfe.filename}") from fnfe
114-
118+
if host:
119+
config_loader.use_values({"api": {"url": host}})
115120
if log_level:
116121
config_loader.use_values({"logging": {"level": log_level}})
117122

tests/unit_tests/cli/test_cli.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,3 +1395,45 @@ def test_log_level_override(flag: str, level: str, runner: CliRunner):
13951395
runner.invoke(main, [flag])
13961396
mock_log.getLogger().setLevel.assert_called_once_with(level)
13971397
mock_log.StreamHandler().setLevel.assert_called_once_with(level)
1398+
1399+
1400+
@responses.activate
1401+
def test_host_option(runner: CliRunner):
1402+
response = responses.add(
1403+
responses.GET,
1404+
"http://override.example.com:5678/plans",
1405+
json={"plans": []},
1406+
status=200,
1407+
)
1408+
1409+
res = runner.invoke(
1410+
main,
1411+
["--host", "http://override.example.com:5678", "controller", "plans"],
1412+
)
1413+
assert response.call_count == 1
1414+
assert res.exit_code == 0
1415+
1416+
1417+
@responses.activate
1418+
def test_host_overrides_config(runner: CliRunner):
1419+
config_path = "tests/unit_tests/example_yaml/rest_config.yaml"
1420+
response = responses.add(
1421+
responses.GET,
1422+
"http://override.example.com:5678/plans",
1423+
json={"plans": []},
1424+
status=200,
1425+
)
1426+
1427+
res = runner.invoke(
1428+
main,
1429+
[
1430+
"--host",
1431+
"http://override.example.com:5678",
1432+
"--config",
1433+
config_path,
1434+
"controller",
1435+
"plans",
1436+
],
1437+
)
1438+
assert response.call_count == 1
1439+
assert res.exit_code == 0

0 commit comments

Comments
 (0)