#include <stdio.h>
+#include "sd-json.h"
+
#include "alloc-util.h"
#include "build.h"
#include "format-table.h"
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");
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");
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");
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");
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) {
}
if (arg_value)
- return id128_pretty_print(uuid, arg_mode);
+ return print_id(uuid);
if (!*table) {
*table = table_new("name", "id");
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;
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;
}
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)" "$(</etc/machine-id)"
systemd-id128 machine-id --pretty
systemd-id128 machine-id --uuid
systemd-id128 machine-id --app-specific=4f68bce3e8cd4db196e7fbcaf984b709
assert_eq "$(systemd-id128 machine-id)" "$(</etc/machine-id)"
systemd-id128 boot-id
+assert_eq "$(systemd-id128 --json=short boot-id --uuid | jq --raw-output .id)" "$(</proc/sys/kernel/random/boot_id)"
systemd-id128 boot-id --pretty
systemd-id128 boot-id --uuid
systemd-id128 boot-id --app-specific=4f68bce3e8cd4db196e7fbcaf984b709
(! systemd-id128 invocation-id -a 4f68bce3e8cd4db196e7fbcaf984b709)
(! systemd-id128 show '')
(! systemd-id128 show "$(set +x; printf '%0.s0' {0..64})")
+(! systemd-id128 show root --pretty --json=short)
+(! systemd-id128 show root usr --value --json=short)