1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
7 #include "id128-print.h"
9 #include "pretty-print.h"
10 #include "terminal-util.h"
14 static Id128PrettyPrintMode arg_mode
= ID128_PRINT_ID128
;
15 static sd_id128_t arg_app
= {};
17 static int verb_new(int argc
, char **argv
, void *userdata
) {
18 return id128_print_new(arg_mode
);
21 static int verb_machine_id(int argc
, char **argv
, void *userdata
) {
25 if (sd_id128_is_null(arg_app
))
26 r
= sd_id128_get_machine(&id
);
28 r
= sd_id128_get_machine_app_specific(arg_app
, &id
);
30 return log_error_errno(r
, "Failed to get %smachine-ID: %m",
31 sd_id128_is_null(arg_app
) ? "" : "app-specific ");
33 return id128_pretty_print(id
, arg_mode
);
36 static int verb_boot_id(int argc
, char **argv
, void *userdata
) {
40 if (sd_id128_is_null(arg_app
))
41 r
= sd_id128_get_boot(&id
);
43 r
= sd_id128_get_boot_app_specific(arg_app
, &id
);
45 return log_error_errno(r
, "Failed to get %sboot-ID: %m",
46 sd_id128_is_null(arg_app
) ? "" : "app-specific ");
48 return id128_pretty_print(id
, arg_mode
);
51 static int verb_invocation_id(int argc
, char **argv
, void *userdata
) {
55 if (!sd_id128_is_null(arg_app
))
56 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
57 "Verb \"invocation-id\" cannot be combined with --app-specific=.");
59 r
= sd_id128_get_invocation(&id
);
61 return log_error_errno(r
, "Failed to get invocation-ID: %m");
63 return id128_pretty_print(id
, arg_mode
);
66 static int help(void) {
67 _cleanup_free_
char *link
= NULL
;
70 r
= terminal_urlify_man("systemd-id128", "1", &link
);
74 printf("%s [OPTIONS...] COMMAND\n\n"
75 "%sGenerate and print 128bit identifiers.%s\n"
77 " new Generate a new id128 string\n"
78 " machine-id Print the ID of current machine\n"
79 " boot-id Print the ID of current boot\n"
80 " invocation-id Print the ID of current invocation\n"
81 " help Show this help\n"
83 " -h --help Show this help\n"
84 " -p --pretty Generate samples of program code\n"
85 " -a --app-specific=ID Generate app-specific IDs\n"
86 " -u --uuid Output in UUID format\n"
87 "\nSee the %s for details.\n"
88 , program_invocation_short_name
89 , ansi_highlight(), ansi_normal()
96 static int verb_help(int argc
, char **argv
, void *userdata
) {
100 static int parse_argv(int argc
, char *argv
[]) {
105 static const struct option options
[] = {
106 { "help", no_argument
, NULL
, 'h' },
107 { "version", no_argument
, NULL
, ARG_VERSION
},
108 { "pretty", no_argument
, NULL
, 'p' },
109 { "app-specific", required_argument
, NULL
, 'a' },
110 { "uuid", no_argument
, NULL
, 'u' },
119 while ((c
= getopt_long(argc
, argv
, "hpa:u", options
, NULL
)) >= 0)
129 arg_mode
= ID128_PRINT_PRETTY
;
133 r
= sd_id128_from_string(optarg
, &arg_app
);
135 return log_error_errno(r
, "Failed to parse \"%s\" as application-ID: %m", optarg
);
139 arg_mode
= ID128_PRINT_UUID
;
146 assert_not_reached("Unhandled option");
152 static int id128_main(int argc
, char *argv
[]) {
153 static const Verb verbs
[] = {
154 { "new", VERB_ANY
, 1, 0, verb_new
},
155 { "machine-id", VERB_ANY
, 1, 0, verb_machine_id
},
156 { "boot-id", VERB_ANY
, 1, 0, verb_boot_id
},
157 { "invocation-id", VERB_ANY
, 1, 0, verb_invocation_id
},
158 { "help", VERB_ANY
, VERB_ANY
, 0, verb_help
},
162 return dispatch_verb(argc
, argv
, verbs
, NULL
);
165 static int run(int argc
, char *argv
[]) {
168 log_show_color(true);
169 log_parse_environment();
172 r
= parse_argv(argc
, argv
);
176 return id128_main(argc
, argv
);
179 DEFINE_MAIN_FUNCTION(run
);