From 995200a978f0b9365d80c06fca148238ea282d6a Mon Sep 17 00:00:00 2001 From: Goffi Date: Fri, 30 Oct 2026 23:58:23 +0100 Subject: [PATCH] cli (base): Add instance isolation options: - New ``-i``/``--isolation`` argument is added, it must be used before first positional argument (i.e., first comamnd). - The new `entry.py` module is now the entry point, so it can set the `LIBERVIA_ISOLATION` environment variable before the backend read it. --- libervia/cli/base.py | 6 +++++ libervia/cli/entry.py | 59 +++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 6 ++--- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 libervia/cli/entry.py diff --git a/libervia/cli/base.py b/libervia/cli/base.py index 02c59089..2751e849 100644 --- a/libervia/cli/base.py +++ b/libervia/cli/base.py @@ -596,6 +596,12 @@ class LiberviaCli: ) ), ) + self.parser.add_argument( + "-i", + "--isolation", + metavar="NAME", + help=("use the isolated instance NAME (must be used before the command)"), + ) def register_output(self, type_, name, callback, description="", default=False): if type_ not in C.OUTPUT_TYPES: diff --git a/libervia/cli/entry.py b/libervia/cli/entry.py new file mode 100644 index 00000000..30f7bf17 --- /dev/null +++ b/libervia/cli/entry.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 + +# Libervia: an XMPP client +# Copyright (C) 2009-2026 Jérôme Poisson (goffi@goffi.org) + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +"""Entry point for the Libervia CLI frontends.""" + +import os +import sys + + +def extract_isolation_arg(argv: list[str]) -> str | None: + """Extract the isolation argument. + + Only arguments used before the first command are considered, avoiding conflicts with + sub-commands. + + @param argv: The command line arguments, without the program name + @return: The isolation name, or None if the argument is not used + """ + for i, arg in enumerate(argv): + if arg in ("-i", "--isolation"): + if i + 1 >= len(argv): + return None + return argv[i + 1] + if arg.startswith("-i") and len(arg) > 2: + # combined short option, e.g. "-iname" + return arg[2:] + if arg.startswith("--isolation="): + return arg.split("=", 1)[1] + if not arg.startswith("-"): + # first positional argument (the command), we stop here + break + return None + + +def main() -> None: + """Run the Libervia CLI.""" + # The backend reads the environment variable at import time, we have to process the + # isolation argument here, before first "libervia.backend" import. + isolation = extract_isolation_arg(sys.argv[1:]) + if isolation is not None: + os.environ["LIBERVIA_ISOLATION"] = isolation + from libervia.cli.base import LiberviaCli + + LiberviaCli.run() diff --git a/pyproject.toml b/pyproject.toml index 34f29b67..381621c3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -169,10 +169,10 @@ docs = [ [project.scripts] # Legacy name, will be removed in future version -jp = "libervia.cli.base:LiberviaCli.run" -li = "libervia.cli.base:LiberviaCli.run" +jp = "libervia.cli.entry:main" +li = "libervia.cli.entry:main" libervia-backend = "libervia.backend.core.launcher:Launcher.run" -libervia-cli = "libervia.cli.base:LiberviaCli.run" +libervia-cli = "libervia.cli.entry:main" libervia-tui = "libervia.tui.base:LiberviaTUIApp.run" # Legacy name, will be removed in future version primitivus = "libervia.tui.base:LiberviaTUIApp.run"