From 23441a3d882930b854b827172343c4222a2c9935 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Fri, 4 Oct 2024 18:08:35 +0200 Subject: sd-json,tree-wide: add sd_json_format_enabled() and use it everwhere We often used a pattern like if (!FLAGS_SET(flags, SD_JSON_FORMAT_OFF)), which is rather verbose and also contains a double negative, which we try to avoid. Add a little helper to avoid an explicit bit check. This change clarifies an aditional thing: in some cases we treated SD_JSON_FORMAT_OFF as a flag (flags & SD_JSON_FORMAT_OFF), while in other cases we treated it as an independent enum value (flags == SD_JSON_FORMAT_OFF). In the first form, flags like SD_JSON_FORMAT_SSE do _not_ turn the json output on, while in the second form they do. Let's use the first form everywhere. No functional change intended. Initially I wasn't sure if this helper should be made public or just internal, but it seems such a common pattern that if we expose the flags, we might just as well expose it too, to make life easier for any consumers. --- src/hostname/hostnamectl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/hostname') diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index 4bbfa670b7..a537312d25 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -491,7 +491,7 @@ static int show_status(int argc, char **argv, void *userdata) { sd_bus *bus = userdata; int r; - if (arg_json_format_flags != SD_JSON_FORMAT_OFF) { + if (sd_json_format_enabled(arg_json_format_flags)) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; -- cgit v1.2.3