id128: add new "-u" switch for outputting Ids in UUID format

For some unrelated stuff I wanted the machine ID in UUID format, and it
was annoying doing that manually. So let's add a switch for this, so
that this works:

    systemd-id128 machine-id -u
This commit is contained in:
Lennart Poettering
2019-11-15 19:07:05 +01:00
parent f0bfae7265
commit a19fdd66c2
4 changed files with 45 additions and 21 deletions
+12 -5
View File
@@ -10,15 +10,22 @@
#include "pretty-print.h"
#include "terminal-util.h"
int id128_pretty_print(sd_id128_t id, bool pretty) {
unsigned i;
int id128_pretty_print(sd_id128_t id, Id128PrettyPrintMode mode) {
_cleanup_free_ char *man_link = NULL, *mod_link = NULL;
const char *on, *off;
unsigned i;
if (!pretty) {
assert(mode >= 0);
assert(mode < _ID128_PRETTY_PRINT_MODE_MAX);
if (mode == ID128_PRINT_ID128) {
printf(SD_ID128_FORMAT_STR "\n",
SD_ID128_FORMAT_VAL(id));
return 0;
} else if (mode == ID128_PRINT_UUID) {
printf(SD_ID128_UUID_FORMAT_STR "\n",
SD_ID128_FORMAT_VAL(id));
return 0;
}
on = ansi_highlight();
@@ -53,7 +60,7 @@ int id128_pretty_print(sd_id128_t id, bool pretty) {
return 0;
}
int id128_print_new(bool pretty) {
int id128_print_new(Id128PrettyPrintMode mode) {
sd_id128_t id;
int r;
@@ -61,5 +68,5 @@ int id128_print_new(bool pretty) {
if (r < 0)
return log_error_errno(r, "Failed to generate ID: %m");
return id128_pretty_print(id, pretty);
return id128_pretty_print(id, mode);
}
+10 -2
View File
@@ -6,5 +6,13 @@
#include "sd-id128.h"
int id128_pretty_print(sd_id128_t id, bool pretty);
int id128_print_new(bool pretty);
typedef enum Id128PrettyPrintMode {
ID128_PRINT_ID128,
ID128_PRINT_UUID,
ID128_PRINT_PRETTY,
_ID128_PRETTY_PRINT_MODE_MAX,
_ID128_PRETTY_PRINT_MODE_INVALID = -1
} Id128PrettyPrintMode;
int id128_pretty_print(sd_id128_t id, Id128PrettyPrintMode mode);
int id128_print_new(Id128PrettyPrintMode mode);