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