From: dongshengyuan <545258830@qq.com> Date: Tue, 14 Jul 2026 10:52:26 +0000 (+0800) Subject: id128: honor json output for single ids X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e72d9b0d7068bb7de494847c20044e073e0d6979;p=thirdparty%2Fsystemd.git id128: honor json output for single ids JSON mode was accepted by single-ID verbs but still printed bare IDs. Print JSON objects for those verbs and reject ambiguous JSON combinations. Follow-up for: a50666e376057a24b67f80c9a8025096c750fb23. --- diff --git a/src/id128/id128.c b/src/id128/id128.c index e66acd33e5e..a4452f1cc50 100644 --- a/src/id128/id128.c +++ b/src/id128/id128.c @@ -2,6 +2,8 @@ #include +#include "sd-json.h" + #include "alloc-util.h" #include "build.h" #include "format-table.h" @@ -24,9 +26,37 @@ static PagerFlags arg_pager_flags = 0; static bool arg_legend = true; static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; +static int print_id(sd_id128_t id) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL; + int r; + + if (!sd_json_format_enabled(arg_json_format_flags)) + return id128_pretty_print(id, arg_mode); + + if (arg_mode == ID128_PRINT_ID128) + r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_ID128("id", id)); + else + r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_UUID("id", id)); + if (r < 0) + return log_error_errno(r, "Failed to create JSON: %m"); + + r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL); + if (r < 0) + return log_error_errno(r, "Failed to print JSON: %m"); + + return 0; +} + VERB_NOARG(verb_new, "new", "Generate a new ID"); static int verb_new(int argc, char *argv[], uintptr_t _data, void *userdata) { - return id128_print_new(arg_mode); + sd_id128_t id; + int r; + + r = sd_id128_randomize(&id); + if (r < 0) + return log_error_errno(r, "Failed to generate ID: %m"); + + return print_id(id); } VERB_NOARG(verb_machine_id, "machine-id", "Print the ID of current machine"); @@ -42,7 +72,7 @@ static int verb_machine_id(int argc, char *argv[], uintptr_t _data, void *userda return log_error_errno(r, "Failed to get %smachine-ID: %m", sd_id128_is_null(arg_app) ? "" : "app-specific "); - return id128_pretty_print(id, arg_mode); + return print_id(id); } VERB_NOARG(verb_boot_id, "boot-id", "Print the ID of current boot"); @@ -58,7 +88,7 @@ static int verb_boot_id(int argc, char *argv[], uintptr_t _data, void *userdata) return log_error_errno(r, "Failed to get %sboot-ID: %m", sd_id128_is_null(arg_app) ? "" : "app-specific "); - return id128_pretty_print(id, arg_mode); + return print_id(id); } VERB_NOARG(verb_invocation_id, "invocation-id", "Print the ID of current invocation"); @@ -74,7 +104,7 @@ static int verb_invocation_id(int argc, char *argv[], uintptr_t _data, void *use if (r < 0) return log_error_errno(r, "Failed to get invocation-ID: %m"); - return id128_pretty_print(id, arg_mode); + return print_id(id); } VERB_NOARG(verb_var_uuid, "var-partition-uuid", "Print the UUID for the /var/ partition"); @@ -91,7 +121,7 @@ static int verb_var_uuid(int argc, char *argv[], uintptr_t _data, void *userdata if (r < 0) return log_error_errno(r, "Failed to generate machine-specific /var/ UUID: %m"); - return id128_pretty_print(id, arg_mode); + return print_id(id); } static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first) { @@ -120,7 +150,7 @@ static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first } if (arg_value) - return id128_pretty_print(uuid, arg_mode); + return print_id(uuid); if (!*table) { *table = table_new("name", "id"); @@ -141,6 +171,10 @@ static int verb_show(int argc, char *argv[], uintptr_t _data, void *userdata) { int r; argv = strv_skip(argv, 1); + if (arg_value && sd_json_format_enabled(arg_json_format_flags) && strv_length(argv) != 1) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "'show --value --json=' requires exactly one argument."); + if (argv) STRV_FOREACH(p, argv) { sd_id128_t uuid; @@ -290,6 +324,9 @@ static int parse_argv(int argc, char *argv[], char ***ret_args) { break; } + if (arg_mode == ID128_PRINT_PRETTY && sd_json_format_enabled(arg_json_format_flags)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--pretty cannot be combined with --json=."); + *ret_args = option_parser_get_args(&opts); return 1; } diff --git a/test/units/TEST-74-AUX-UTILS.id128.sh b/test/units/TEST-74-AUX-UTILS.id128.sh index b9db1f14542..5da2323461b 100755 --- a/test/units/TEST-74-AUX-UTILS.id128.sh +++ b/test/units/TEST-74-AUX-UTILS.id128.sh @@ -28,21 +28,25 @@ systemd-id128 show --json=short systemd-id128 show --no-legend systemd-id128 show --no-pager --no-legend systemd-id128 show root -P -u +systemd-id128 show root --value --json=short | jq -e '.id | type == "string"' [[ -n "$(systemd-id128 var-partition-uuid)" ]] [[ "$(systemd-id128 var-partition-uuid)" != "4d21b016b53445c2a9fb5c16e091fd2d" ]] [[ "$(systemd-id128 new | wc -c)" -eq 33 ]] +systemd-id128 --json=short new | jq -e '.id | type == "string"' systemd-id128 new -p systemd-id128 new -u systemd-id128 new -a 4f68bce3e8cd4db196e7fbcaf984b709 systemd-id128 machine-id +assert_eq "$(systemd-id128 --json=short machine-id | jq --raw-output .id)" "$(