feat(cli): Add CLI command for configuring Revpi features
Introduced a new CLI command to enable, disable, check the status, and list available features for Revpi using D-Bus calls. This implementation provides a structured interface for managing Revpi configurations via command-line actions.
This commit is contained in:
93
src/revpi_middleware/cli_commands/cli_config.py
Normal file
93
src/revpi_middleware/cli_commands/cli_config.py
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
# SPDX-FileCopyrightText: 2025 KUNBUS GmbH
|
||||||
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
"""Command-Line for the picontrol object of CLI."""
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
from logging import getLogger
|
||||||
|
|
||||||
|
from .dbus_helper import BusType, get_properties, simple_call
|
||||||
|
from .. import proginit as pi
|
||||||
|
from ..dbus_middleware1 import extend_interface
|
||||||
|
|
||||||
|
log = getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def add_subparsers(parent_parser: ArgumentParser):
|
||||||
|
parent_parser.add_argument(
|
||||||
|
"action",
|
||||||
|
choices=["enable", "disable", "status", "available", "list-features"],
|
||||||
|
help="Action to be executed: enable, disable, status or available. "
|
||||||
|
"To get all available features, use 'list-features'.",
|
||||||
|
)
|
||||||
|
parent_parser.add_argument(
|
||||||
|
"feature",
|
||||||
|
nargs="?",
|
||||||
|
default="",
|
||||||
|
help="Name of the feature to configer. To list all features use 'list-features' as action.",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> int:
|
||||||
|
action = pi.pargs.action
|
||||||
|
dbus_value = False
|
||||||
|
try:
|
||||||
|
|
||||||
|
if action == "list-features":
|
||||||
|
dbus_value = get_properties(
|
||||||
|
"available_features",
|
||||||
|
interface=extend_interface("RevpiConfig"),
|
||||||
|
bus_type=BusType.SESSION if pi.pargs.use_session_bus else BusType.SYSTEM,
|
||||||
|
)
|
||||||
|
for feature in dbus_value:
|
||||||
|
print(feature)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
# For the following actions, a feature name is required
|
||||||
|
if pi.pargs.feature == "":
|
||||||
|
raise Exception("Feature name is required")
|
||||||
|
|
||||||
|
if action == "enable":
|
||||||
|
simple_call(
|
||||||
|
"Enable",
|
||||||
|
pi.pargs.feature,
|
||||||
|
interface=extend_interface("RevpiConfig"),
|
||||||
|
bus_type=BusType.SESSION if pi.pargs.use_session_bus else BusType.SYSTEM,
|
||||||
|
)
|
||||||
|
|
||||||
|
elif action == "disable":
|
||||||
|
simple_call(
|
||||||
|
"Disable",
|
||||||
|
pi.pargs.feature,
|
||||||
|
interface=extend_interface("RevpiConfig"),
|
||||||
|
bus_type=BusType.SESSION if pi.pargs.use_session_bus else BusType.SYSTEM,
|
||||||
|
)
|
||||||
|
|
||||||
|
elif action == "status":
|
||||||
|
dbus_value = simple_call(
|
||||||
|
"GetStatus",
|
||||||
|
pi.pargs.feature,
|
||||||
|
interface=extend_interface("RevpiConfig"),
|
||||||
|
bus_type=BusType.SESSION if pi.pargs.use_session_bus else BusType.SYSTEM,
|
||||||
|
)
|
||||||
|
|
||||||
|
elif action == "available":
|
||||||
|
dbus_value = simple_call(
|
||||||
|
"GetAvailability",
|
||||||
|
pi.pargs.feature,
|
||||||
|
interface=extend_interface("RevpiConfig"),
|
||||||
|
bus_type=BusType.SESSION if pi.pargs.use_session_bus else BusType.SYSTEM,
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise Exception(f"Unknown action: {action}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
log.error(f"Error: {e}")
|
||||||
|
return 1
|
||||||
|
|
||||||
|
log.debug(
|
||||||
|
f"D-Bus call of method {action} for feature {pi.pargs.feature} returned: {dbus_value}"
|
||||||
|
)
|
||||||
|
print(int(dbus_value))
|
||||||
|
|
||||||
|
return 0
|
||||||
Reference in New Issue
Block a user