Skip to content

The agent that grows with you

fix(webapp): keep -p <profile> in the --no-open hint after a private launch

unsupportedpastelsc2c5eef14924
diff --git a/hermes_cli/webapp_launcher.py b/hermes_cli/webapp_launcher.py
index d386542e5eb..c9c47b8b84e 100644
--- a/hermes_cli/webapp_launcher.py
+++ b/hermes_cli/webapp_launcher.py
@@ -145,6 +145,17 @@ def _profile_choice(home: Path, target: Path) -> dict:
return {"profile": name} if name and name != "default" else {}
+def _rerun_command(*flags: str) -> str:
+ """``hermes webapp *flags*`` for the profile this run used: ``-p`` re-homed only this process, so a
+ bare rerun gets the sticky profile, and the command keeps ``-p <name>`` when that names another one."""
+ import shlex
+ from hermes_cli.main import explicit_cli_profile
+ from hermes_cli.profiles import get_active_profile
+ name = explicit_cli_profile()
+ profile = ["-p", shlex.quote(name)] if name and name != get_active_profile() else []
+ return " ".join(["hermes", *profile, "webapp", *flags])
+
+
def older_gateway(response: dict) -> bool:
"""Whether *response* is a gateway's answer to a verb it does not know: it predates the Webapp."""
return not response.get("ok") and str(response.get("error") or "").startswith("unknown verb:")
@@ -347,7 +358,7 @@ def cmd_webapp(args) -> int:
# carries it to the browser; failing that, the user opens it by hand.
if not args.no_open and _open_privately(target, link["url"]):
print("Opened the Webapp in your browser.")
- print("If no browser opened, run `hermes webapp --no-open` for a link.")
+ print(f"If no browser opened, run `{_rerun_command('--no-open')}` for a link.")
return 0
print(f"Open this link in your browser within {link['expires_in_seconds']} seconds. It works "
"once and signs the browser in as you, so keep it private:")
diff --git a/tests/hermes_cli/test_webapp_private_launch.py b/tests/hermes_cli/test_webapp_private_launch.py
index 1a59e6c460b..4ff6b5b0acf 100644
--- a/tests/hermes_cli/test_webapp_private_launch.py
+++ b/tests/hermes_cli/test_webapp_private_launch.py
@@ -16,6 +16,7 @@ from urllib.parse import unquote, urlsplit
import pytest
+from hermes_cli import main as cli_main
from hermes_cli import webapp_launcher as launcher
from tests.hermes_cli import test_webapp_launcher as launcher_tests
@@ -42,6 +43,7 @@ def _page_path(url: str) -> Path:
@pytest.mark.platforms("posix")
def test_a_private_link_reaches_the_browser_in_a_page_only_the_user_can_read(private, capsys):
+ private["monkeypatch"].setattr(cli_main, "_explicit_cli_profile", None)
assert launcher.cmd_webapp(_args()) == 0
[opened] = private["calls"]["opened"]
assert "ticket" not in opened
@@ -57,6 +59,21 @@ def test_a_private_link_reaches_the_browser_in_a_page_only_the_user_can_read(pri
assert "If no browser opened, run `hermes webapp --no-open` for a link." in out
[email protected](("flag", "sticky", "command"), [
+ ("work", None, "hermes -p work webapp --no-open"),
+ ("default", "work", "hermes -p default webapp --no-open"),
+ ("work", "work", "hermes webapp --no-open"),
+])
+def test_the_no_open_hint_keeps_the_profile_a_bare_rerun_would_not_pick(private, capsys, flag, sticky, command):
+ """``-p`` re-homes only this process: a bare ``hermes webapp --no-open`` gets the sticky profile."""
+ private["monkeypatch"].setattr(cli_main, "_explicit_cli_profile", flag)
+ if sticky:
+ (private["root"] / "active_profile").write_text(sticky + "\n", encoding="utf-8")
+ assert launcher.cmd_webapp(_args()) == 0
+ assert f"If no browser opened, run `{command}` for a link." in capsys.readouterr().out
+
+
@pytest.mark.platforms("posix")
def test_a_folder_other_users_could_read_is_closed_before_the_page_is_written(private):
folder = private["root"] / "webapp"