]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: cli: refactor CLI wait command parser
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Wed, 9 Sep 2026 08:11:55 +0000 (10:11 +0200)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Wed, 14 Oct 2026 09:33:40 +0000 (11:33 +0200)
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 <ctx.cond>
member. The second part of the function reuses <ctx.cond> 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

index f7ad9cf79c27a2c341f2471e9fce73fa27003622..a2fe4c35b8a49a676a8d6784767f42670511271e 100644 (file)
--- 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 (<backend>/<server>).\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 (<backend>/<server>).\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);