]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: version: add -vq, -vqb, and -vqs flags for concise version output
authorNikita Kurashkin <nkurashkin@stsoft.ru>
Tue, 2 Sep 2025 09:34:51 +0000 (11:34 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 5 Sep 2025 06:57:57 +0000 (08:57 +0200)
This patch introduces three new command line flags to display HAProxy version
info more flexibly:

- `-vqs` outputs the short version string without commit info (e.g., "3.3.1").
- `-vqb` outputs only the branch (major.minor) part of the version (e.g., "3.3").
- `-vq` outputs the full version string with suffixes (e.g., "3.3.1-dev5-1bb975-71").

This allows easier parsing of version info in automation while keeping existing -v and -vv behaviors.

The command line argument parsing now calls `display_version_plain()` with a
display_mode parameter to select the desired output format. The function handles
stripping of commit or patch info as needed, depending on the mode.

Signed-off-by: Nikita Kurashkin <nkurashkin@stsoft.ru>
src/haproxy.c

index c56a79024a20efe825abd38036798a8d008747d7..350a43c64f94580b618c6a086e942efad853738c 100644 (file)
@@ -616,6 +616,42 @@ void display_version()
        }
 }
 
+/* display_mode:
+ * 0 = short version (e.g., "3.3.1")
+ * 1 = full version (e.g., "3.3.1-dev5-1bb975-71")
+ * 2 = branch version (e.g., "3.3")
+ */
+void display_version_plain(int display_mode)
+{
+       char out[30] = "";
+       int dots = 0;
+       int i;
+
+       if (display_mode == 1) {
+               printf("%s\n", haproxy_version);
+               return;
+       }
+
+       for (i = 0; i < sizeof(out) - 1 && haproxy_version[i]; i++) {
+               if (display_mode == 2) {
+                       if (haproxy_version[i] == '.') dots++;
+                       if (dots == 2 || haproxy_version[i] == '-') {
+                               out[i] = '\0';
+                               break;
+                       }
+               } else {
+                       if ((haproxy_version[i] < '0' || haproxy_version[i] > '9') && haproxy_version[i] != '.') {
+                               out[i] = '\0';
+                               break;
+                       }
+               }
+               out[i] = haproxy_version[i];
+               out[i+1] = '\0';
+       }
+
+       printf("%s\n", out);
+}
+
 static void display_build_opts()
 {
        const char **opt;
@@ -658,6 +694,7 @@ static void usage(char *name)
                "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
                "        [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n"
                "        -v displays version ; -vv shows known build options.\n"
+               "        -vq/-vqs/-vqb only displays version, short version, branch.\n"
                "        -d enters debug mode ; -db only disables background mode.\n"
                "        -dM[<byte>,help,...] debug memory (default: poison with <byte>/0x50)\n"
                "        -dt activate traces on stderr\n"
@@ -1477,10 +1514,24 @@ static void init_args(int argc, char **argv)
 
                        /* 1 arg */
                        if (*flag == 'v') {
-                               display_version();
-                               if (flag[1] == 'v')  /* -vv */
-                                       display_build_opts();
-                               deinit_and_exit(0);
+                               if (flag[1] == 'q' && flag[2] == 's' && flag[3] == '\0') {
+                                       display_version_plain(0);  // -vqs
+                                       deinit_and_exit(0);
+                               }
+                               else if (flag[1] == 'q' && flag[2] == 'b' && flag[3] == '\0') {
+                                       display_version_plain(2);  // -vqb
+                                       deinit_and_exit(0);
+                               }
+                               else if (flag[1] == 'q' && flag[2] == '\0') {
+                                       display_version_plain(1);  // -vq
+                                       deinit_and_exit(0);
+                               }
+                               else {
+                                       display_version();
+                                       if (flag[1] == 'v')  // -vv
+                                               display_build_opts();
+                                       deinit_and_exit(0);
+                               }
                        }
 #if defined(USE_EPOLL)
                        else if (*flag == 'd' && flag[1] == 'e')