]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
tty: Produce colored output in CI environments
authorTobias Brunner <tobias@strongswan.org>
Thu, 10 Apr 2025 07:06:18 +0000 (09:06 +0200)
committerTobias Brunner <tobias@strongswan.org>
Thu, 10 Apr 2025 07:33:19 +0000 (09:33 +0200)
src/libstrongswan/utils/utils/tty.c

index 753e624224e5505c5aaec7dc18a136ec6e0cffac..556726400f32ab19eebdde3007a24f64eaac8c10 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2014 Tobias Brunner
+ * Copyright (C) 2008-2025 Tobias Brunner
  * Copyright (C) 2005-2008 Martin Willi
  *
  * Copyright (C) secunet Security Networks AG
@@ -18,6 +18,7 @@
 #include <utils/utils.h>
 
 #include <unistd.h>
+#include <stdio.h>
 
 ENUM(tty_color_names, TTY_RESET, TTY_BG_DEF,
        "\e[0m",
@@ -44,12 +45,30 @@ ENUM(tty_color_names, TTY_RESET, TTY_BG_DEF,
        "\e[49m",
 );
 
+/**
+ * Check if the output goes to stdout/stderr in a CI environment, where colored
+ * output is usually supported.
+ */
+static bool is_ci(int fd)
+{
+       static bool ci_checked = FALSE, ci_found = FALSE;
+
+       if (!ci_checked)
+       {
+               ci_checked = TRUE;
+               ci_found = getenv("CI") && (getenv("GITHUB_ACTIONS") ||
+                                                                       getenv("CIRRUS_CI") ||
+                                                                       getenv("APPVEYOR"));
+       }
+       return ci_found && (fd == fileno(stdout) || fd == fileno(stderr));
+}
+
 /**
  * Get the escape string for a given TTY color, empty string on non-tty FILE
  */
 char* tty_escape_get(int fd, tty_escape_t escape)
 {
-       if (!isatty(fd))
+       if (!isatty(fd) && !is_ci(fd))
        {
                return "";
        }