]>
Commit | Line | Data |
---|---|---|
ff7dad48 ZJS |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
3 | #include <stdio.h> | |
4 | ||
5 | #include "sd-id128.h" | |
6 | ||
87d57be0 | 7 | #include "alloc-util.h" |
ff7dad48 ZJS |
8 | #include "id128-print.h" |
9 | #include "log.h" | |
294bf0c3 | 10 | #include "pretty-print.h" |
87d57be0 | 11 | #include "terminal-util.h" |
ff7dad48 | 12 | |
0d1d512f | 13 | int id128_pretty_print(sd_id128_t id, bool pretty) { |
ff7dad48 | 14 | unsigned i; |
87d57be0 ZJS |
15 | _cleanup_free_ char *man_link = NULL, *mod_link = NULL; |
16 | const char *on, *off; | |
ff7dad48 | 17 | |
0d1d512f ZJS |
18 | if (!pretty) { |
19 | printf(SD_ID128_FORMAT_STR "\n", | |
20 | SD_ID128_FORMAT_VAL(id)); | |
21 | return 0; | |
22 | } | |
ff7dad48 | 23 | |
87d57be0 ZJS |
24 | on = ansi_highlight(); |
25 | off = ansi_normal(); | |
26 | ||
27 | if (terminal_urlify("man:systemd-id128(1)", "systemd-id128(1)", &man_link) < 0) | |
28 | return log_oom(); | |
29 | ||
30 | if (terminal_urlify("https://docs.python.org/3/library/uuid.html", "uuid", &mod_link) < 0) | |
31 | return log_oom(); | |
32 | ||
ff7dad48 | 33 | printf("As string:\n" |
87d57be0 | 34 | "%s" SD_ID128_FORMAT_STR "%s\n\n" |
ff7dad48 | 35 | "As UUID:\n" |
87d57be0 ZJS |
36 | "%s%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%s\n\n" |
37 | "As %s macro:\n" | |
38 | "%s#define MESSAGE_XYZ SD_ID128_MAKE(", | |
39 | on, SD_ID128_FORMAT_VAL(id), off, | |
40 | on, SD_ID128_FORMAT_VAL(id), off, | |
41 | man_link, | |
42 | on); | |
ff7dad48 ZJS |
43 | for (i = 0; i < 16; i++) |
44 | printf("%02x%s", id.bytes[i], i != 15 ? "," : ""); | |
87d57be0 | 45 | printf(")%s\n\n", off); |
ff7dad48 ZJS |
46 | |
47 | printf("As Python constant:\n" | |
87d57be0 ZJS |
48 | ">>> import %s\n" |
49 | ">>> %sMESSAGE_XYZ = uuid.UUID('" SD_ID128_FORMAT_STR "')%s\n", | |
50 | mod_link, | |
51 | on, SD_ID128_FORMAT_VAL(id), off); | |
ff7dad48 ZJS |
52 | |
53 | return 0; | |
54 | } | |
0d1d512f ZJS |
55 | |
56 | int id128_print_new(bool pretty) { | |
57 | sd_id128_t id; | |
58 | int r; | |
59 | ||
60 | r = sd_id128_randomize(&id); | |
61 | if (r < 0) | |
62 | return log_error_errno(r, "Failed to generate ID: %m"); | |
63 | ||
64 | return id128_pretty_print(id, pretty); | |
65 | } |