From 0295e208fea2abf8eb79cf38f1ee5633f1c2c166 Mon Sep 17 00:00:00 2001 From: Amaury Denoyelle Date: Wed, 9 Sep 2026 10:11:55 +0200 Subject: [PATCH] MINOR: cli: refactor CLI wait command parser Reorganize cli_parse_wait() which is responsible to parse CLI wait command invocation. This patch splits the function in two parts. First, args[2] is compared as string to check the comparison argument and assigned member. The second part of the function reuses to perform the necessary initialization. This patch will allow to extend wait command to add similar conditions to the already existing one, without having to either duplicate the code or perform multiple string comparison. --- src/cli.c | 58 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/src/cli.c b/src/cli.c index f7ad9cf79..a2fe4c35b 100644 --- a/src/cli.c +++ b/src/cli.c @@ -2465,34 +2465,9 @@ static int cli_parse_wait(char **args, char *payload, struct appctx *appctx, voi } if (strcmp(args[2], "srv-removable") == 0) { - struct ist be_name, sv_name; - - if (!*args[3]) - return cli_err(appctx, "Missing server name (/).\n"); - - sv_name = ist(args[3]); - be_name = istsplit(&sv_name, '/'); - if (!istlen(sv_name)) - return cli_err(appctx, "Require 'backend/server'.\n"); - - be_name = istdup(be_name); - sv_name = istdup(sv_name); - if (!isttest(be_name) || !isttest(sv_name)) { - free(istptr(be_name)); - free(istptr(sv_name)); - return cli_err(appctx, "Out of memory trying to clone the server name.\n"); - } - - ctx->args[0] = ist0(be_name); - ctx->args[1] = ist0(sv_name); ctx->cond = CLI_WAIT_COND_SRV_UNUSED; } else if (strcmp(args[2], "be-removable") == 0) { - if (!*args[3]) - return cli_err(appctx, "Missing backend name.\n"); - ctx->args[0] = strdup(args[3]); - if (!ctx->args[0]) - return cli_err(appctx, "Out of memory trying to clone the backend name.\n"); ctx->cond = CLI_WAIT_COND_BE_UNUSED; } else if (*args[2]) { @@ -2514,6 +2489,39 @@ static int cli_parse_wait(char **args, char *payload, struct appctx *appctx, voi return cli_err(appctx, err); } + switch (ctx->cond) { + case CLI_WAIT_COND_SRV_UNUSED: { + const char *bename, *svname; + + if (!*args[3]) + return cli_err(appctx, "Missing server name (/).\n"); + + if (parse_be_srv(args[3], &bename, &svname, &ctx->msg)) + return cli_err(appctx, ctx->msg); + + ctx->args[0] = strdup(bename); + ctx->args[1] = strdup(svname); + if (!ctx->args[0] || !ctx->args[1]) { + free(ctx->args[0]); + free(ctx->args[1]); + return cli_err(appctx, "Out of memory trying to clone the server name.\n"); + } + + break; + } + + case CLI_WAIT_COND_BE_UNUSED: + if (!*args[3]) + return cli_err(appctx, "Missing backend name.\n"); + ctx->args[0] = strdup(args[3]); + if (!ctx->args[0]) + return cli_err(appctx, "Out of memory trying to clone the backend name.\n"); + break; + + case CLI_WAIT_COND_NONE: + break; + } + ctx->start = now_ms; ctx->deadline = tick_add(now_ms, wait_ms); -- 2.47.3