cmdeploy: replace globals() subcommand scan with explicit SUBCOMMANDS list

The get_parser() loop that scanned globals() for *_cmd names was fragile
and forced # noqa: F401 on all lxc imports (ruff couldn't see they were
used dynamically).

Replace it with an explicit SUBCOMMANDS list of
(cmd_func, options_func, needs_config) tuples.  This makes the full set
of subcommands visible at a glance, their registration order defined,
and the imports unconditionally used (no more noqa suppressions).
This commit is contained in:
holger krekel
2026-03-08 18:19:06 +01:00
parent 86e5708709
commit 674e496a53

View File

@@ -16,8 +16,8 @@ import pyinfra
from chatmaild.config import read_config, write_initial_config
from packaging import version
from . import dns, remote # noqa: F401
from .lxc.cli import ( # noqa: F401
from . import dns, remote
from .lxc.cli import (
lxc_start_cmd,
lxc_start_cmd_options,
lxc_status_cmd,
@@ -379,6 +379,23 @@ Setup your chatmail server configuration and
deploy it via SSH to your remote location.
"""
# Explicit subcommand registry: (cmd_func, options_func_or_None, needs_config).
# LXC commands don't need a chatmail.ini (no config); all others do.
SUBCOMMANDS = [
(init_cmd, init_cmd_options, True),
(run_cmd, run_cmd_options, True),
(dns_cmd, dns_cmd_options, True),
(status_cmd, status_cmd_options, True),
(test_cmd, test_cmd_options, True),
(fmt_cmd, fmt_cmd_options, True),
(bench_cmd, None, True),
(webdev_cmd, None, True),
(lxc_start_cmd, lxc_start_cmd_options, False),
(lxc_stop_cmd, lxc_stop_cmd_options, False),
(lxc_status_cmd, lxc_status_cmd_options, False),
(lxc_test_cmd, lxc_test_cmd_options, False),
]
def get_parser():
"""Return an ArgumentParser for the 'cmdeploy' CLI"""
@@ -395,15 +412,10 @@ def get_parser():
)
subparsers = parser.add_subparsers(title="subcommands")
# find all subcommands in the module namespace
glob = globals()
for name, func in glob.items():
if name.endswith("_cmd"):
needs_config = not name.startswith("lxc_")
subparser = add_subcommand(subparsers, func, add_config=needs_config)
addopts = glob.get(name + "_options")
if addopts is not None:
addopts(subparser)
for func, addopts, needs_config in SUBCOMMANDS:
subparser = add_subcommand(subparsers, func, add_config=needs_config)
if addopts is not None:
addopts(subparser)
return parser