From ef73fe258460915daa44e7bdf8de754d0377adcc Mon Sep 17 00:00:00 2001 From: Nikita Kurashkin Date: Tue, 2 Sep 2025 11:34:51 +0200 Subject: [PATCH] MINOR: version: add -vq, -vqb, and -vqs flags for concise version output 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 --- src/haproxy.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/src/haproxy.c b/src/haproxy.c index c56a79024..350a43c64 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -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 ] [ -N ]\n" " [ -p ] [ -m ] [ -C ] [-- *]\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[,help,...] debug memory (default: poison with /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')