mirror of
https://github.com/systemd/systemd.git
synced 2026-08-08 17:10:55 +00:00
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:
@@ -7,14 +7,15 @@
|
|||||||
#include "id128-print.h"
|
#include "id128-print.h"
|
||||||
#include "main-func.h"
|
#include "main-func.h"
|
||||||
#include "pretty-print.h"
|
#include "pretty-print.h"
|
||||||
|
#include "terminal-util.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "verbs.h"
|
#include "verbs.h"
|
||||||
|
|
||||||
static bool arg_pretty = false;
|
static Id128PrettyPrintMode arg_mode = ID128_PRINT_ID128;
|
||||||
static sd_id128_t arg_app = {};
|
static sd_id128_t arg_app = SD_ID128_NULL;
|
||||||
|
|
||||||
static int verb_new(int argc, char **argv, void *userdata) {
|
static int verb_new(int argc, char **argv, void *userdata) {
|
||||||
return id128_print_new(arg_pretty);
|
return id128_print_new(arg_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int verb_machine_id(int argc, char **argv, void *userdata) {
|
static int verb_machine_id(int argc, char **argv, void *userdata) {
|
||||||
@@ -29,7 +30,7 @@ static int verb_machine_id(int argc, char **argv, void *userdata) {
|
|||||||
return log_error_errno(r, "Failed to get %smachine-ID: %m",
|
return log_error_errno(r, "Failed to get %smachine-ID: %m",
|
||||||
sd_id128_is_null(arg_app) ? "" : "app-specific ");
|
sd_id128_is_null(arg_app) ? "" : "app-specific ");
|
||||||
|
|
||||||
return id128_pretty_print(id, arg_pretty);
|
return id128_pretty_print(id, arg_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int verb_boot_id(int argc, char **argv, void *userdata) {
|
static int verb_boot_id(int argc, char **argv, void *userdata) {
|
||||||
@@ -44,7 +45,7 @@ static int verb_boot_id(int argc, char **argv, void *userdata) {
|
|||||||
return log_error_errno(r, "Failed to get %sboot-ID: %m",
|
return log_error_errno(r, "Failed to get %sboot-ID: %m",
|
||||||
sd_id128_is_null(arg_app) ? "" : "app-specific ");
|
sd_id128_is_null(arg_app) ? "" : "app-specific ");
|
||||||
|
|
||||||
return id128_pretty_print(id, arg_pretty);
|
return id128_pretty_print(id, arg_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int verb_invocation_id(int argc, char **argv, void *userdata) {
|
static int verb_invocation_id(int argc, char **argv, void *userdata) {
|
||||||
@@ -59,7 +60,7 @@ static int verb_invocation_id(int argc, char **argv, void *userdata) {
|
|||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_error_errno(r, "Failed to get invocation-ID: %m");
|
return log_error_errno(r, "Failed to get invocation-ID: %m");
|
||||||
|
|
||||||
return id128_pretty_print(id, arg_pretty);
|
return id128_pretty_print(id, arg_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int help(void) {
|
static int help(void) {
|
||||||
@@ -70,19 +71,22 @@ static int help(void) {
|
|||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_oom();
|
return log_oom();
|
||||||
|
|
||||||
printf("%s [OPTIONS...] {COMMAND}\n\n"
|
printf("%s [OPTIONS...] COMMAND\n\n"
|
||||||
"Generate and print id128 strings.\n\n"
|
"%sGenerate and print 128bit identifiers.%s\n"
|
||||||
" -h --help Show this help\n"
|
|
||||||
" -p --pretty Generate samples of program code\n"
|
|
||||||
" -a --app-specific=ID Generate app-specific IDs\n"
|
|
||||||
"\nCommands:\n"
|
"\nCommands:\n"
|
||||||
" new Generate a new id128 string\n"
|
" new Generate a new id128 string\n"
|
||||||
" machine-id Print the ID of current machine\n"
|
" machine-id Print the ID of current machine\n"
|
||||||
" boot-id Print the ID of current boot\n"
|
" boot-id Print the ID of current boot\n"
|
||||||
" invocation-id Print the ID of current invocation\n"
|
" invocation-id Print the ID of current invocation\n"
|
||||||
" help Show this help\n"
|
" help Show this help\n"
|
||||||
|
"\nOptions:\n"
|
||||||
|
" -h --help Show this help\n"
|
||||||
|
" -p --pretty Generate samples of program code\n"
|
||||||
|
" -a --app-specific=ID Generate app-specific IDs\n"
|
||||||
|
" -u --uuid Output in UUID format\n"
|
||||||
"\nSee the %s for details.\n"
|
"\nSee the %s for details.\n"
|
||||||
, program_invocation_short_name
|
, program_invocation_short_name
|
||||||
|
, ansi_highlight(), ansi_normal()
|
||||||
, link
|
, link
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -103,6 +107,7 @@ static int parse_argv(int argc, char *argv[]) {
|
|||||||
{ "version", no_argument, NULL, ARG_VERSION },
|
{ "version", no_argument, NULL, ARG_VERSION },
|
||||||
{ "pretty", no_argument, NULL, 'p' },
|
{ "pretty", no_argument, NULL, 'p' },
|
||||||
{ "app-specific", required_argument, NULL, 'a' },
|
{ "app-specific", required_argument, NULL, 'a' },
|
||||||
|
{ "uuid", no_argument, NULL, 'u' },
|
||||||
{},
|
{},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -111,7 +116,7 @@ static int parse_argv(int argc, char *argv[]) {
|
|||||||
assert(argc >= 0);
|
assert(argc >= 0);
|
||||||
assert(argv);
|
assert(argv);
|
||||||
|
|
||||||
while ((c = getopt_long(argc, argv, "hpa:", options, NULL)) >= 0)
|
while ((c = getopt_long(argc, argv, "hpa:u", options, NULL)) >= 0)
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
|
||||||
case 'h':
|
case 'h':
|
||||||
@@ -121,7 +126,7 @@ static int parse_argv(int argc, char *argv[]) {
|
|||||||
return version();
|
return version();
|
||||||
|
|
||||||
case 'p':
|
case 'p':
|
||||||
arg_pretty = true;
|
arg_mode = ID128_PRINT_PRETTY;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'a':
|
case 'a':
|
||||||
@@ -130,6 +135,10 @@ static int parse_argv(int argc, char *argv[]) {
|
|||||||
return log_error_errno(r, "Failed to parse \"%s\" as application-ID: %m", optarg);
|
return log_error_errno(r, "Failed to parse \"%s\" as application-ID: %m", optarg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'u':
|
||||||
|
arg_mode = ID128_PRINT_UUID;
|
||||||
|
break;
|
||||||
|
|
||||||
case '?':
|
case '?':
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
|
|||||||
@@ -2047,7 +2047,7 @@ int main(int argc, char *argv[]) {
|
|||||||
switch (arg_action) {
|
switch (arg_action) {
|
||||||
|
|
||||||
case ACTION_NEW_ID128:
|
case ACTION_NEW_ID128:
|
||||||
r = id128_print_new(true);
|
r = id128_print_new(ID128_PRINT_PRETTY);
|
||||||
goto finish;
|
goto finish;
|
||||||
|
|
||||||
case ACTION_SETUP_KEYS:
|
case ACTION_SETUP_KEYS:
|
||||||
|
|||||||
@@ -10,15 +10,22 @@
|
|||||||
#include "pretty-print.h"
|
#include "pretty-print.h"
|
||||||
#include "terminal-util.h"
|
#include "terminal-util.h"
|
||||||
|
|
||||||
int id128_pretty_print(sd_id128_t id, bool pretty) {
|
int id128_pretty_print(sd_id128_t id, Id128PrettyPrintMode mode) {
|
||||||
unsigned i;
|
|
||||||
_cleanup_free_ char *man_link = NULL, *mod_link = NULL;
|
_cleanup_free_ char *man_link = NULL, *mod_link = NULL;
|
||||||
const char *on, *off;
|
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",
|
printf(SD_ID128_FORMAT_STR "\n",
|
||||||
SD_ID128_FORMAT_VAL(id));
|
SD_ID128_FORMAT_VAL(id));
|
||||||
return 0;
|
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();
|
on = ansi_highlight();
|
||||||
@@ -53,7 +60,7 @@ int id128_pretty_print(sd_id128_t id, bool pretty) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int id128_print_new(bool pretty) {
|
int id128_print_new(Id128PrettyPrintMode mode) {
|
||||||
sd_id128_t id;
|
sd_id128_t id;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
@@ -61,5 +68,5 @@ int id128_print_new(bool pretty) {
|
|||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_error_errno(r, "Failed to generate ID: %m");
|
return log_error_errno(r, "Failed to generate ID: %m");
|
||||||
|
|
||||||
return id128_pretty_print(id, pretty);
|
return id128_pretty_print(id, mode);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,5 +6,13 @@
|
|||||||
|
|
||||||
#include "sd-id128.h"
|
#include "sd-id128.h"
|
||||||
|
|
||||||
int id128_pretty_print(sd_id128_t id, bool pretty);
|
typedef enum Id128PrettyPrintMode {
|
||||||
int id128_print_new(bool pretty);
|
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user