show: Add --print-hex
authorColin Walters <walters@verbum.org>
Fri, 7 Jul 2023 13:00:31 +0000 (09:00 -0400)
committerColin Walters <walters@verbum.org>
Fri, 7 Jul 2023 13:22:30 +0000 (09:22 -0400)
The default GVariant output for byte arrays is illegible to humans,
and byte arrays are super common for us.

src/ostree/ot-builtin-show.c

index d59407f88a210437541e48fda1b49e8586f8b518..c1fc0fed086dbaa9c28d53a9a6d0b23cf7c87c7b 100644 (file)
@@ -35,6 +35,7 @@ static gboolean opt_list_metadata_keys;
 static gboolean opt_list_detached_metadata_keys;
 static gboolean opt_print_sizes;
 static gboolean opt_raw;
+static gboolean opt_print_hex;
 static gboolean opt_no_byteswap;
 static char *opt_gpg_homedir;
 static char *opt_gpg_verify_remote;
@@ -53,6 +54,15 @@ static GOptionEntry options[]
           "List the available metadata keys", NULL },
         { "print-metadata-key", 0, 0, G_OPTION_ARG_STRING, &opt_print_metadata_key,
           "Print string value of metadata key", "KEY" },
+        {
+            "print-hex",
+            0,
+            0,
+            G_OPTION_ARG_NONE,
+            &opt_print_hex,
+            "For byte array valued keys, output an unquoted hexadecimal string",
+            NULL,
+        },
         { "list-detached-metadata-keys", 0, 0, G_OPTION_ARG_NONE, &opt_list_detached_metadata_keys,
           "List the available detached metadata keys", NULL },
         { "print-detached-metadata-key", 0, 0, G_OPTION_ARG_STRING,
@@ -186,6 +196,14 @@ do_print_metadata_key (OstreeRepo *repo, const char *resolved_rev, gboolean deta
       return FALSE;
     }
 
+  if (opt_print_hex && g_variant_is_of_type (value, (GVariantType *)"ay"))
+    {
+      g_autofree char *buf = g_malloc (g_variant_get_size (value) * 2 + 1);
+      ot_bin2hex (buf, g_variant_get_data (value), g_variant_get_size (value));
+      g_print ("%s\n", buf);
+      return TRUE;
+    }
+
   if (opt_no_byteswap)
     {
       g_autofree char *formatted = g_variant_print (value, TRUE);