libervia-backend/libervia/cli/entry.py
Goffi 995200a978 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.
2026-11-03 18:02:05 +01:00

59 lines
2.1 KiB
Python

#!/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 <http://www.gnu.org/licenses/>.
"""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()