From 37adac0625903f83436f518d8e87e90f696f54d5 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Tue, 29 Sep 2026 14:20:00 +0200 Subject: refactor(commands): define update queries statically Move the pacman and checkupdates package queries into module-level constants alongside the base package queries. Select the appropriate update query when constructing the runtime query list, reducing branching and keeping query definitions together. --- prometheus_pacman_exporter/__init__.py | 40 ++++++++++++++++------------------ 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/prometheus_pacman_exporter/__init__.py b/prometheus_pacman_exporter/__init__.py index e957662..c6bed96 100644 --- a/prometheus_pacman_exporter/__init__.py +++ b/prometheus_pacman_exporter/__init__.py @@ -75,6 +75,20 @@ BASE_QUERIES: tuple[PackageQuery, ...] = ( ), ) +PACMAN_UPDATEABLE_QUERY = PackageQuery( + (*PACMAN_BASE_COMMAND, "--upgrades"), + "updateable packages", + metrics.UPDATEABLE_PACKAGES, + empty_exit_codes=PACMAN_EMPTY_EXIT_CODES, +) + +CHECKUPDATES_QUERY = PackageQuery( + ("checkupdates",), + "updateable packages", + metrics.UPDATEABLE_PACKAGES, + accepted_exit_codes=(0, 2), +) + def print_version( ctx: click.Context, param: click.Parameter | None, value: bool @@ -150,8 +164,8 @@ def run_command( """ command_string = shlex.join(command) debug("Running command:", command_string, err=True) - result = subprocess.run(command, capture_output=True, encoding="utf-8", check=False) + result = subprocess.run(command, capture_output=True, encoding="utf-8", check=False) if result.returncode in accepted_exit_codes: return result @@ -266,26 +280,10 @@ def prometheus_pacman_exporter( debug(" verbose:", str(verbose_flag), err=True) debug(" quiet:", str(quiet_flag), err=True) - queries: list[PackageQuery] = [*BASE_QUERIES] - - if use_checkupdates_flag: - queries.append( - PackageQuery( - ("checkupdates",), - "updateable packages", - metrics.UPDATEABLE_PACKAGES, - (0, 2), - ) - ) - else: - queries.append( - PackageQuery( - (*PACMAN_BASE_COMMAND, "--upgrades"), - "updateable packages", - metrics.UPDATEABLE_PACKAGES, - empty_exit_codes=PACMAN_EMPTY_EXIT_CODES, - ) - ) + queries: list[PackageQuery] = [ + *BASE_QUERIES, + CHECKUPDATES_QUERY if use_checkupdates_flag else PACMAN_UPDATEABLE_QUERY, + ] if aur_flag: queries.append( -- cgit v1.3.1