From 114cd158d90746898b31720addec5b169cb5ab88 Mon Sep 17 00:00:00 2001 From: Robert Sturla Date: Tue, 23 Jan 2024 01:08:17 +0000 Subject: [PATCH] Refactor logic to separate findDeploymentIndex function Signed-off-by: Robert Sturla --- src/ostree/ot-admin-builtin-status.c | 46 +++++++++++++++------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/ostree/ot-admin-builtin-status.c b/src/ostree/ot-admin-builtin-status.c index ee3a4fba43..0ca5152c95 100644 --- a/src/ostree/ot-admin-builtin-status.c +++ b/src/ostree/ot-admin-builtin-status.c @@ -229,42 +229,33 @@ ot_admin_builtin_status (int argc, char **argv, OstreeCommandInvocation *invocat } else if (opt_booted_index) { - for (guint i = 0; i < deployments->len; ++i) + index = findDeploymentIndex (booted_deployment, deployments); + if (index != G_MAXUINT) { - OstreeDeployment *deployment = deployments->pdata[i]; - if (deployment == booted_deployment) - { - g_print ("%d\n", i); - return TRUE; - } + g_print ("%d\n", index); + return TRUE; } return FALSE; } else if (opt_pending_index) { - for (guint i = 0; i < deployments->len; ++i) + index = findDeploymentIndex (pending_deployment, deployments); + if (index != G_MAXUINT) { - OstreeDeployment *deployment = deployments->pdata[i]; - if (deployment == pending_deployment) - { - g_print ("%d\n", i); - return TRUE; - } + g_print ("%d\n", index); + return TRUE; } return FALSE; } else if (opt_rollback_index) { - for (guint i = 0; i < deployments->len; ++i) + index = findDeploymentIndex (rollback_deployment, deployments); + if (index != G_MAXUINT) { - OstreeDeployment *deployment = deployments->pdata[i]; - if (deployment == rollback_deployment) - { - g_print ("%d\n", i); - return TRUE; - } + g_print ("%d\n", index); + return TRUE; } return FALSE; @@ -283,3 +274,16 @@ ot_admin_builtin_status (int argc, char **argv, OstreeCommandInvocation *invocat return TRUE; } + +static guint +findDeploymentIndex (OstreeDeployment *targetDeployment, GPtrArray *deployments) +{ + for (guint i = 0; i < deployments->len; ++i) + { + OstreeDeployment *deployment = deployments->pdata[i]; + if (deployment == targetDeployment) + return i; + } + + return G_MAXUINT; +}