2018-08-21 16:38:04 +03:00
/* SPDX-License-Identifier: LGPL-2.1+ */
# include <stdio.h>
# include "sd-id128.h"
2018-10-02 16:02:04 +03:00
# include "alloc-util.h"
2018-08-21 16:38:04 +03:00
# include "id128-print.h"
# include "log.h"
2018-11-20 17:42:57 +03:00
# include "pretty-print.h"
2018-10-02 16:02:04 +03:00
# include "terminal-util.h"
2018-08-21 16:38:04 +03:00
2018-08-21 17:08:48 +03:00
int id128_pretty_print ( sd_id128_t id , bool pretty ) {
2018-08-21 16:38:04 +03:00
unsigned i ;
2018-10-02 16:02:04 +03:00
_cleanup_free_ char * man_link = NULL , * mod_link = NULL ;
const char * on , * off ;
2018-08-21 16:38:04 +03:00
2018-08-21 17:08:48 +03:00
if ( ! pretty ) {
printf ( SD_ID128_FORMAT_STR " \n " ,
SD_ID128_FORMAT_VAL ( id ) ) ;
return 0 ;
}
2018-08-21 16:38:04 +03:00
2018-10-02 16:02:04 +03:00
on = ansi_highlight ( ) ;
off = ansi_normal ( ) ;
if ( terminal_urlify ( " man:systemd-id128(1) " , " systemd-id128(1) " , & man_link ) < 0 )
return log_oom ( ) ;
if ( terminal_urlify ( " https://docs.python.org/3/library/uuid.html " , " uuid " , & mod_link ) < 0 )
return log_oom ( ) ;
2018-08-21 16:38:04 +03:00
printf ( " As string: \n "
2018-10-02 16:02:04 +03:00
" %s " SD_ID128_FORMAT_STR " %s \n \n "
2018-08-21 16:38:04 +03:00
" As UUID: \n "
2018-10-02 16:02:04 +03:00
" %s%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%s \n \n "
" As %s macro: \n "
" %s#define MESSAGE_XYZ SD_ID128_MAKE( " ,
on , SD_ID128_FORMAT_VAL ( id ) , off ,
on , SD_ID128_FORMAT_VAL ( id ) , off ,
man_link ,
on ) ;
2018-08-21 16:38:04 +03:00
for ( i = 0 ; i < 16 ; i + + )
printf ( " %02x%s " , id . bytes [ i ] , i ! = 15 ? " , " : " " ) ;
2018-10-02 16:02:04 +03:00
printf ( " )%s \n \n " , off ) ;
2018-08-21 16:38:04 +03:00
printf ( " As Python constant: \n "
2018-10-02 16:02:04 +03:00
" >>> import %s \n "
" >>> %sMESSAGE_XYZ = uuid.UUID(' " SD_ID128_FORMAT_STR " ')%s \n " ,
mod_link ,
on , SD_ID128_FORMAT_VAL ( id ) , off ) ;
2018-08-21 16:38:04 +03:00
return 0 ;
}
2018-08-21 17:08:48 +03:00
int id128_print_new ( bool pretty ) {
sd_id128_t id ;
int r ;
r = sd_id128_randomize ( & id ) ;
if ( r < 0 )
return log_error_errno ( r , " Failed to generate ID: %m " ) ;
return id128_pretty_print ( id , pretty ) ;
}