Skip to content

Commit ef73fe2

Browse files
Nikita Kurashkinwtarreau
authored andcommitted
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 <nkurashkin@stsoft.ru>
1 parent 5d9abc6 commit ef73fe2

1 file changed

Lines changed: 55 additions & 4 deletions

File tree

src/haproxy.c

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,42 @@ void display_version()
616616
}
617617
}
618618

619+
/* display_mode:
620+
* 0 = short version (e.g., "3.3.1")
621+
* 1 = full version (e.g., "3.3.1-dev5-1bb975-71")
622+
* 2 = branch version (e.g., "3.3")
623+
*/
624+
void display_version_plain(int display_mode)
625+
{
626+
char out[30] = "";
627+
int dots = 0;
628+
int i;
629+
630+
if (display_mode == 1) {
631+
printf("%s\n", haproxy_version);
632+
return;
633+
}
634+
635+
for (i = 0; i < sizeof(out) - 1 && haproxy_version[i]; i++) {
636+
if (display_mode == 2) {
637+
if (haproxy_version[i] == '.') dots++;
638+
if (dots == 2 || haproxy_version[i] == '-') {
639+
out[i] = '\0';
640+
break;
641+
}
642+
} else {
643+
if ((haproxy_version[i] < '0' || haproxy_version[i] > '9') && haproxy_version[i] != '.') {
644+
out[i] = '\0';
645+
break;
646+
}
647+
}
648+
out[i] = haproxy_version[i];
649+
out[i+1] = '\0';
650+
}
651+
652+
printf("%s\n", out);
653+
}
654+
619655
static void display_build_opts()
620656
{
621657
const char **opt;
@@ -658,6 +694,7 @@ static void usage(char *name)
658694
"D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
659695
" [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]\n"
660696
" -v displays version ; -vv shows known build options.\n"
697+
" -vq/-vqs/-vqb only displays version, short version, branch.\n"
661698
" -d enters debug mode ; -db only disables background mode.\n"
662699
" -dM[<byte>,help,...] debug memory (default: poison with <byte>/0x50)\n"
663700
" -dt activate traces on stderr\n"
@@ -1477,10 +1514,24 @@ static void init_args(int argc, char **argv)
14771514

14781515
/* 1 arg */
14791516
if (*flag == 'v') {
1480-
display_version();
1481-
if (flag[1] == 'v') /* -vv */
1482-
display_build_opts();
1483-
deinit_and_exit(0);
1517+
if (flag[1] == 'q' && flag[2] == 's' && flag[3] == '\0') {
1518+
display_version_plain(0); // -vqs
1519+
deinit_and_exit(0);
1520+
}
1521+
else if (flag[1] == 'q' && flag[2] == 'b' && flag[3] == '\0') {
1522+
display_version_plain(2); // -vqb
1523+
deinit_and_exit(0);
1524+
}
1525+
else if (flag[1] == 'q' && flag[2] == '\0') {
1526+
display_version_plain(1); // -vq
1527+
deinit_and_exit(0);
1528+
}
1529+
else {
1530+
display_version();
1531+
if (flag[1] == 'v') // -vv
1532+
display_build_opts();
1533+
deinit_and_exit(0);
1534+
}
14841535
}
14851536
#if defined(USE_EPOLL)
14861537
else if (*flag == 'd' && flag[1] == 'e')

0 commit comments

Comments
 (0)