bin/summary: Add options to show metadata
authorDan Nicholson <dbn@endlessos.org>
Mon, 30 Jan 2023 05:45:29 +0000 (22:45 -0700)
committerDan Nicholson <dbn@endlessos.org>
Wed, 8 Feb 2023 05:59:30 +0000 (22:59 -0700)
bash/ostree
src/ostree/ot-builtin-summary.c
tests/test-summary-update.sh

index 76a62c594a677a69f770189ef923188e55e06b69..0415e225ae9d8302f5653104cfc7f431052b11bd 100644 (file)
@@ -1816,6 +1816,7 @@ _ostree_static_delta() {
 _ostree_summary() {
     local boolean_options="
         $main_boolean_options
+        --list-metadata-keys
         --raw
         --update -u
         --view -v
@@ -1825,6 +1826,7 @@ _ostree_summary() {
         --add-metadata -m
         --gpg-homedir
         --gpg-sign
+        --print-metadata-key
         --repo
     "
 
index 2d6306a4a8f27cd44e2e7a6d35b20f63cf20b54b..cdfc11e69950ae71c5026fddfe8f441225e3f588 100644 (file)
@@ -28,6 +28,8 @@
 #include "ostree-sign.h"
 
 static gboolean opt_update, opt_view, opt_raw;
+static gboolean opt_list_metadata_keys;
+static char *opt_print_metadata_key;
 static char **opt_gpg_key_ids;
 static char *opt_gpg_homedir;
 static char **opt_key_ids;
@@ -43,6 +45,8 @@ static GOptionEntry options[] = {
   { "update", 'u', 0, G_OPTION_ARG_NONE, &opt_update, "Update the summary", NULL },
   { "view", 'v', 0, G_OPTION_ARG_NONE, &opt_view, "View the local summary file", NULL },
   { "raw", 0, 0, G_OPTION_ARG_NONE, &opt_raw, "View the raw bytes of the summary file", NULL },
+  { "list-metadata-keys", 0, 0, G_OPTION_ARG_NONE, &opt_list_metadata_keys, "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" },
   { "gpg-sign", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_gpg_key_ids, "GPG Key ID to sign the summary with", "KEY-ID"},
   { "gpg-homedir", 0, 0, G_OPTION_ARG_FILENAME, &opt_gpg_homedir, "GPG Homedir to use when looking for keyrings", "HOMEDIR"},
   { "sign", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_key_ids, "Key ID to sign the summary with", "KEY-ID"},
@@ -85,6 +89,26 @@ build_additional_metadata (const char * const  *args,
   return g_variant_ref_sink (g_variant_builder_end (builder));
 }
 
+static gboolean
+get_summary_data (OstreeRepo  *repo,
+                  GBytes     **out_summary_data,
+                  GError     **error)
+{
+  g_assert (out_summary_data != NULL);
+
+  g_autoptr(GBytes) summary_data = NULL;
+  glnx_autofd int fd = -1;
+  if (!glnx_openat_rdonly (repo->repo_dir_fd, "summary", TRUE, &fd, error))
+    return FALSE;
+  summary_data = ot_fd_readall_or_mmap (fd, 0, error);
+  if (!summary_data)
+    return FALSE;
+
+  *out_summary_data = g_steal_pointer (&summary_data);
+
+  return TRUE;
+}
+
 gboolean
 ostree_builtin_summary (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
 {
@@ -275,15 +299,30 @@ ostree_builtin_summary (int argc, char **argv, OstreeCommandInvocation *invocati
       if (opt_raw)
         flags |= OSTREE_DUMP_RAW;
 
-      glnx_autofd int fd = -1;
-      if (!glnx_openat_rdonly (repo->repo_dir_fd, "summary", TRUE, &fd, error))
-        return FALSE;
-      summary_data = ot_fd_readall_or_mmap (fd, 0, error);
-      if (!summary_data)
+      if (!get_summary_data (repo, &summary_data, error))
         return FALSE;
 
       ot_dump_summary_bytes (summary_data, flags);
     }
+  else if (opt_list_metadata_keys)
+    {
+      g_autoptr(GBytes) summary_data = NULL;
+
+      if (!get_summary_data (repo, &summary_data, error))
+        return FALSE;
+
+      ot_dump_summary_metadata_keys (summary_data);
+    }
+  else if (opt_print_metadata_key)
+    {
+      g_autoptr(GBytes) summary_data = NULL;
+
+      if (!get_summary_data (repo, &summary_data, error))
+        return FALSE;
+
+      if (!ot_dump_summary_metadata_key (summary_data, opt_print_metadata_key, error))
+        return FALSE;
+    }
   else
     {
       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
index 74c42d10d141555f6bdae74ba136f5e778629b30..d85e9c4b3926f92e78634b21a8ef94ac73da1c84 100755 (executable)
@@ -59,6 +59,10 @@ ${CMD_PREFIX} ostree --repo=repo summary --update --add-metadata=map='@a{sv} {}'
 # Check the additional metadata turns up in the output.
 ${CMD_PREFIX} ostree --repo=repo summary --view > summary
 assert_file_has_content summary "^map: {}$"
+${CMD_PREFIX} ostree --repo=repo summary --list-metadata-keys > metadata
+assert_file_has_content metadata "^map$"
+${CMD_PREFIX} ostree --repo=repo summary --print-metadata-key=map > metadata
+assert_file_has_content metadata "^@a{sv} {}$"
 
 echo "ok 1 update summary"