1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
8 #include "format-table.h"
10 #include "id128-print.h"
11 #include "main-func.h"
12 #include "parse-argument.h"
13 #include "pretty-print.h"
15 #include "terminal-util.h"
18 static Id128PrettyPrintMode arg_mode
= ID128_PRINT_ID128
;
19 static sd_id128_t arg_app
= {};
20 static bool arg_value
= false;
21 static PagerFlags arg_pager_flags
= 0;
22 static bool arg_legend
= true;
23 static JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
25 static int verb_new(int argc
, char **argv
, void *userdata
) {
26 return id128_print_new(arg_mode
);
29 static int verb_machine_id(int argc
, char **argv
, void *userdata
) {
33 if (sd_id128_is_null(arg_app
))
34 r
= sd_id128_get_machine(&id
);
36 r
= sd_id128_get_machine_app_specific(arg_app
, &id
);
38 return log_error_errno(r
, "Failed to get %smachine-ID: %m",
39 sd_id128_is_null(arg_app
) ? "" : "app-specific ");
41 return id128_pretty_print(id
, arg_mode
);
44 static int verb_boot_id(int argc
, char **argv
, void *userdata
) {
48 if (sd_id128_is_null(arg_app
))
49 r
= sd_id128_get_boot(&id
);
51 r
= sd_id128_get_boot_app_specific(arg_app
, &id
);
53 return log_error_errno(r
, "Failed to get %sboot-ID: %m",
54 sd_id128_is_null(arg_app
) ? "" : "app-specific ");
56 return id128_pretty_print(id
, arg_mode
);
59 static int verb_invocation_id(int argc
, char **argv
, void *userdata
) {
63 if (!sd_id128_is_null(arg_app
))
64 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
65 "Verb \"invocation-id\" cannot be combined with --app-specific=.");
67 r
= sd_id128_get_invocation(&id
);
69 return log_error_errno(r
, "Failed to get invocation-ID: %m");
71 return id128_pretty_print(id
, arg_mode
);
74 static int show_one(Table
**table
, const char *name
, sd_id128_t uuid
, bool first
) {
80 if (sd_id128_is_null(arg_app
))
83 assert_se(sd_id128_get_app_specific(uuid
, arg_app
, &u
) == 0);
85 if (arg_mode
== ID128_PRINT_PRETTY
) {
86 _cleanup_free_
char *id
= NULL
;
88 id
= strreplace(name
, "-", "_");
94 r
= id128_pretty_print_sample(id
, u
);
103 return id128_pretty_print(u
, arg_mode
);
106 *table
= table_new("name", "id");
109 table_set_width(*table
, 0);
112 return table_add_many(*table
,
114 arg_mode
== ID128_PRINT_ID128
? TABLE_ID128
: TABLE_UUID
,
118 static int verb_show(int argc
, char **argv
, void *userdata
) {
119 _cleanup_(table_unrefp
) Table
*table
= NULL
;
122 argv
= strv_skip(argv
, 1);
123 if (strv_isempty(argv
))
124 for (const GptPartitionType
*e
= gpt_partition_type_table
; e
->name
; e
++) {
125 r
= show_one(&table
, e
->name
, e
->uuid
, e
== gpt_partition_type_table
);
130 STRV_FOREACH(p
, argv
) {
135 /* Check if the argument is an actual UUID first */
136 have_uuid
= sd_id128_from_string(*p
, &uuid
) >= 0;
139 id
= gpt_partition_type_uuid_to_string(uuid
) ?: "XYZ";
141 GptPartitionType type
;
143 r
= gpt_partition_type_from_string(*p
, &type
);
145 return log_error_errno(r
, "Unknown identifier \"%s\".", *p
);
151 r
= show_one(&table
, id
, uuid
, p
== argv
);
157 r
= table_print_with_pager(table
, arg_json_format_flags
, arg_pager_flags
, arg_legend
);
165 static int help(void) {
166 _cleanup_free_
char *link
= NULL
;
169 r
= terminal_urlify_man("systemd-id128", "1", &link
);
173 printf("%s [OPTIONS...] COMMAND\n\n"
174 "%sGenerate and print 128-bit identifiers.%s\n"
176 " new Generate a new ID\n"
177 " machine-id Print the ID of current machine\n"
178 " boot-id Print the ID of current boot\n"
179 " invocation-id Print the ID of current invocation\n"
180 " show [NAME|UUID] Print one or more UUIDs\n"
181 " help Show this help\n"
183 " -h --help Show this help\n"
184 " --no-pager Do not pipe output into a pager\n"
185 " --no-legend Do not show the headers and footers\n"
186 " --json=FORMAT Output inspection data in JSON (takes one of\n"
187 " pretty, short, off)\n"
188 " -j Equivalent to --json=pretty (on TTY) or\n"
189 " --json=short (otherwise)\n"
190 " -p --pretty Generate samples of program code\n"
191 " -P --value Only print the value\n"
192 " -a --app-specific=ID Generate app-specific IDs\n"
193 " -u --uuid Output in UUID format\n"
194 "\nSee the %s for details.\n",
195 program_invocation_short_name
,
203 static int verb_help(int argc
, char **argv
, void *userdata
) {
207 static int parse_argv(int argc
, char *argv
[]) {
215 static const struct option options
[] = {
216 { "help", no_argument
, NULL
, 'h' },
217 { "version", no_argument
, NULL
, ARG_VERSION
},
218 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
219 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
220 { "json", required_argument
, NULL
, ARG_JSON
},
221 { "pretty", no_argument
, NULL
, 'p' },
222 { "value", no_argument
, NULL
, 'P' },
223 { "app-specific", required_argument
, NULL
, 'a' },
224 { "uuid", no_argument
, NULL
, 'u' },
233 while ((c
= getopt_long(argc
, argv
, "hpa:uPj", options
, NULL
)) >= 0)
243 arg_pager_flags
|= PAGER_DISABLE
;
251 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
255 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
261 arg_mode
= ID128_PRINT_PRETTY
;
267 if (arg_mode
== ID128_PRINT_PRETTY
)
268 arg_mode
= ID128_PRINT_ID128
;
272 r
= id128_from_string_nonzero(optarg
, &arg_app
);
274 return log_error_errno(r
, "Application ID cannot be all zeros.");
276 return log_error_errno(r
, "Failed to parse \"%s\" as application-ID: %m", optarg
);
280 arg_mode
= ID128_PRINT_UUID
;
287 assert_not_reached();
293 static int id128_main(int argc
, char *argv
[]) {
294 static const Verb verbs
[] = {
295 { "new", VERB_ANY
, 1, 0, verb_new
},
296 { "machine-id", VERB_ANY
, 1, 0, verb_machine_id
},
297 { "boot-id", VERB_ANY
, 1, 0, verb_boot_id
},
298 { "invocation-id", VERB_ANY
, 1, 0, verb_invocation_id
},
299 { "show", VERB_ANY
, VERB_ANY
, 0, verb_show
},
300 { "help", VERB_ANY
, VERB_ANY
, 0, verb_help
},
304 return dispatch_verb(argc
, argv
, verbs
, NULL
);
307 static int run(int argc
, char *argv
[]) {
312 r
= parse_argv(argc
, argv
);
316 return id128_main(argc
, argv
);
319 DEFINE_MAIN_FUNCTION(run
);