Skip to content

Commit 18dbae6

Browse files
grondomergify[bot]
authored andcommitted
flux-overlay: replace status --no-color option with --color=WHEN
Problem: The flux-overlay status command requires manual disabling of colorized output using a --no-color option, but other Flux commands use the semi-standard --color=WHEN option. Replace --no-color with `-L, --color=WHEN`, where WHEN can be one of 'auto', 'never', or 'always'. The default is 'auto' if the option is not specified, or 'always' if the option is specified without an argument. Since --color=auto is now the default, color will not be displayed if stdout is not connected to a tty. Therefore the use of --no-color in the testsuite can simply be removed.
1 parent 79d0ad7 commit 18dbae6

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/cmd/builtin/overlay.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#if HAVE_CONFIG_H
1212
# include <config.h>
1313
#endif
14+
#include <unistd.h>
1415
#include <jansson.h>
1516
#include <flux/core.h>
1617

@@ -57,8 +58,9 @@ static struct optparse_option status_opts[] = {
5758
.usage = "Do not fill in presumed state of nodes that are"
5859
" inaccessible behind offline/lost overlay parents",
5960
},
60-
{ .name = "no-color", .has_arg = 0,
61-
.usage = "Do not use color to highlight offline/lost nodes",
61+
{ .name = "color", .key = 'L', .has_arg = 2, .arginfo = "WHEN",
62+
.usage = "Colorize output when supported; WHEN can be 'always' "
63+
"(default if omitted), 'never', or 'auto' (default)."
6264
},
6365
{ .name = "wait", .key = 'w', .has_arg = 1, .arginfo = "STATE",
6466
.usage = "Wait until subtree enters STATE before reporting"
@@ -77,6 +79,7 @@ static struct optparse_option disconnect_opts[] = {
7779
struct status {
7880
flux_t *h;
7981
int verbose;
82+
int color;
8083
double timeout;
8184
optparse_t *opt;
8285
struct timespec start;
@@ -173,7 +176,7 @@ static const char *status_colorize (struct status *ctx,
173176
{
174177
static char buf[128];
175178

176-
if (!optparse_hasopt (ctx->opt, "no-color")) {
179+
if (ctx->color) {
177180
if (streq (status, "lost") && !ghost) {
178181
snprintf (buf, sizeof (buf), "%s%s%s",
179182
ansi_red, status, ansi_default);
@@ -555,6 +558,24 @@ static bool validate_wait (const char *wait)
555558
return true;
556559
}
557560

561+
static int status_use_color (optparse_t *p)
562+
{
563+
const char *when;
564+
int color;
565+
566+
if (!(when = optparse_get_str (p, "color", "auto")))
567+
when = "always";
568+
if (streq (when, "always"))
569+
color = 1;
570+
else if (streq (when, "never"))
571+
color = 0;
572+
else if (streq (when, "auto"))
573+
color = isatty (STDOUT_FILENO) ? 1 : 0;
574+
else
575+
log_msg_exit ("Invalid argument to --color: '%s'", when);
576+
return color;
577+
}
578+
558579
static int subcmd_status (optparse_t *p, int ac, char *av[])
559580
{
560581
int rank = optparse_get_int (p, "rank", 0);
@@ -563,6 +584,7 @@ static int subcmd_status (optparse_t *p, int ac, char *av[])
563584

564585
ctx.h = builtin_get_flux_handle (p);
565586
ctx.verbose = optparse_get_int (p, "verbose", 0);
587+
ctx.color = status_use_color (p);
566588
ctx.timeout = optparse_get_duration (p, "timeout", default_timeout);
567589
if (ctx.timeout == 0)
568590
ctx.timeout = -1.0; // disabled

t/t3303-system-healthcheck.t

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ test_expect_success 'flux overlay status -vv works' '
9191

9292
test_expect_success 'flux overlay status shows rank 3 offline' '
9393
echo "3 fake3: offline" >health.exp &&
94-
flux overlay status --timeout=0 --no-pretty --no-color \
94+
flux overlay status --timeout=0 --no-pretty \
9595
| grep fake3 >health.out &&
9696
test_cmp health.exp health.out
9797
'
@@ -109,14 +109,14 @@ test_expect_success 'flux overlay status -vv' '
109109
'
110110

111111
test_expect_success 'flux overlay status: 0,1:partial, 3:offline' '
112-
flux overlay status --timeout=0 --no-color --no-pretty >health2.out &&
112+
flux overlay status --timeout=0 --no-pretty >health2.out &&
113113
grep "0 fake0: partial" health2.out &&
114114
grep "1 fake1: partial" health2.out &&
115115
grep "3 fake3: offline" health2.out
116116
'
117117

118118
test_expect_success 'flux overlay status: 0-1:partial, 3,7-8:offline' '
119-
flux overlay status --timeout=0 --no-color --no-pretty >health3.out &&
119+
flux overlay status --timeout=0 --no-pretty >health3.out &&
120120
grep "0 fake0: partial" health3.out &&
121121
grep "1 fake1: partial" health3.out &&
122122
grep "3 fake3: offline" health3.out &&
@@ -125,7 +125,7 @@ test_expect_success 'flux overlay status: 0-1:partial, 3,7-8:offline' '
125125
'
126126

127127
test_expect_success 'flux overlay status: 0,1:partial, 3,7-8:offline, rest:full' '
128-
flux overlay status --timeout=0 --no-color --no-pretty >health4.out &&
128+
flux overlay status --timeout=0 --no-pretty >health4.out &&
129129
grep "0 fake0: partial" health4.out &&
130130
grep "1 fake1: partial" health4.out &&
131131
grep "3 fake3: offline" health4.out &&
@@ -234,7 +234,7 @@ test_expect_success 'stop broker 12' '
234234
'
235235

236236
test_expect_success 'flux overlay status prints connection timed out on 12' '
237-
flux overlay status --no-pretty --no-color >status.out &&
237+
flux overlay status --no-pretty >status.out &&
238238
grep "fake12: Connection timed out" status.out
239239
'
240240

t/t3305-system-rpctrack-up.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ test_expect_success 'report health status' '
9797
'
9898
test_expect_success 'health status for rank 6 is lost' '
9999
echo "6 fake6: lost" >status.exp &&
100-
flux overlay status --timeout=0 --down --no-color --no-pretty \
100+
flux overlay status --timeout=0 --down --no-pretty \
101101
| grep fake6 >status.out &&
102102
test_cmp status.exp status.out
103103
'

0 commit comments

Comments
 (0)