Blame SOURCES/0007-Add-repolist-command-RhBug1584952.patch

ace92b
From 83f282853e22fe3146b86b33bdaebb7a22b0a580 Mon Sep 17 00:00:00 2001
ace92b
From: Jaroslav Rohel <jrohel@redhat.com>
ace92b
Date: Thu, 7 Nov 2019 21:34:38 +0100
ace92b
Subject: [PATCH] Add repolist command (RhBug:1584952)
ace92b
ace92b
The command lists repositories.
ace92b
ace92b
Command options:
ace92b
  --all        show all repositories
ace92b
  --disabled   show disabled repositories
ace92b
  --enabled    show enabled repositories (default)
ace92b
ace92b
Signed-off-by: Jaroslav Rohel <jrohel@redhat.com>
ace92b
---
ace92b
 dnf/CMakeLists.txt                                      |   6 ++++++
ace92b
 dnf/meson.build                                         |   9 +++++++++
ace92b
 dnf/plugins/repolist/dnf-command-repolist.c             | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ace92b
 dnf/plugins/repolist/dnf-command-repolist.gresource.xml |   6 ++++++
ace92b
 dnf/plugins/repolist/dnf-command-repolist.h             |  33 +++++++++++++++++++++++++++++++++
ace92b
 dnf/plugins/repolist/repolist.plugin                    |   9 +++++++++
ace92b
 6 files changed, 230 insertions(+)
ace92b
 create mode 100644 dnf/plugins/repolist/dnf-command-repolist.c
ace92b
 create mode 100644 dnf/plugins/repolist/dnf-command-repolist.gresource.xml
ace92b
 create mode 100644 dnf/plugins/repolist/dnf-command-repolist.h
ace92b
 create mode 100644 dnf/plugins/repolist/repolist.plugin
ace92b
ace92b
diff --git a/dnf/CMakeLists.txt b/dnf/CMakeLists.txt
ace92b
index 04d5b17..eb73c11 100644
ace92b
--- a/dnf/CMakeLists.txt
ace92b
+++ b/dnf/CMakeLists.txt
ace92b
@@ -15,6 +15,11 @@ glib_compile_resources (DNF_COMMAND_UPDATE plugins/update/dnf-command-update.gre
ace92b
                         INTERNAL)
ace92b
 list (APPEND DNF_COMMAND_UPDATE "plugins/update/dnf-command-update.c")
ace92b
 
ace92b
+glib_compile_resources (DNF_COMMAND_REPOLIST plugins/repolist/dnf-command-repolist.gresource.xml
ace92b
+                        C_PREFIX dnf_command_repolist
ace92b
+                        INTERNAL)
ace92b
+list (APPEND DNF_COMMAND_REPOLIST "plugins/repolist/dnf-command-repolist.c")
ace92b
+
ace92b
 glib_compile_resources (DNF_COMMAND_CLEAN plugins/clean/dnf-command-clean.gresource.xml
ace92b
                         C_PREFIX dnf_command_clean
ace92b
                         INTERNAL)
ace92b
@@ -25,6 +30,7 @@ add_executable (microdnf dnf-main.c ${DNF_SRCS}
ace92b
                 ${DNF_COMMAND_INSTALL}
ace92b
                 ${DNF_COMMAND_REMOVE}
ace92b
                 ${DNF_COMMAND_UPDATE}
ace92b
+                ${DNF_COMMAND_REPOLIST}
ace92b
                 ${DNF_COMMAND_CLEAN})
ace92b
 
ace92b
 target_link_libraries (microdnf
ace92b
diff --git a/dnf/meson.build b/dnf/meson.build
ace92b
index f8f1bf3..d368180 100644
ace92b
--- a/dnf/meson.build
ace92b
+++ b/dnf/meson.build
ace92b
@@ -30,6 +30,15 @@ microdnf_srcs = [
ace92b
   ),
ace92b
   'plugins/update/dnf-command-update.c',
ace92b
 
ace92b
+  # repolist
ace92b
+  gnome.compile_resources(
ace92b
+    'dnf-repolist',
ace92b
+    'plugins/repolist/dnf-command-repolist.gresource.xml',
ace92b
+    c_name : 'dnf_command_repolist',
ace92b
+    source_dir : 'plugins/repolist',
ace92b
+  ),
ace92b
+  'plugins/repolist/dnf-command-repolist.c',
ace92b
+
ace92b
   # clean
ace92b
   gnome.compile_resources(
ace92b
     'dnf-clean',
ace92b
diff --git a/dnf/plugins/repolist/dnf-command-repolist.c b/dnf/plugins/repolist/dnf-command-repolist.c
ace92b
new file mode 100644
ace92b
index 0000000..f69917e
ace92b
--- /dev/null
ace92b
+++ b/dnf/plugins/repolist/dnf-command-repolist.c
ace92b
@@ -0,0 +1,167 @@
ace92b
+/*
ace92b
+ * Copyright (C) 2019 Red Hat, Inc.
ace92b
+ *
ace92b
+ * Licensed under the GNU Lesser General Public License Version 2.1
ace92b
+ *
ace92b
+ * This library is free software; you can redistribute it and/or
ace92b
+ * modify it under the terms of the GNU Lesser General Public
ace92b
+ * License as published by the Free Software Foundation; either
ace92b
+ * version 2.1 of the License, or (at your option) any later version.
ace92b
+ *
ace92b
+ * This library is distributed in the hope that it will be useful,
ace92b
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
ace92b
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ace92b
+ * Lesser General Public License for more details.
ace92b
+ *
ace92b
+ * You should have received a copy of the GNU Lesser General Public
ace92b
+ * License along with this library; if not, write to the Free Software
ace92b
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
ace92b
+ */
ace92b
+
ace92b
+#include "dnf-command-repolist.h"
ace92b
+
ace92b
+#include <libsmartcols.h>
ace92b
+#include <unistd.h>
ace92b
+
ace92b
+struct _DnfCommandRepolist
ace92b
+{
ace92b
+  PeasExtensionBase parent_instance;
ace92b
+};
ace92b
+
ace92b
+static void dnf_command_repolist_iface_init (DnfCommandInterface *iface);
ace92b
+
ace92b
+G_DEFINE_DYNAMIC_TYPE_EXTENDED (DnfCommandRepolist,
ace92b
+                                dnf_command_repolist,
ace92b
+                                PEAS_TYPE_EXTENSION_BASE,
ace92b
+                                0,
ace92b
+                                G_IMPLEMENT_INTERFACE (DNF_TYPE_COMMAND,
ace92b
+                                                       dnf_command_repolist_iface_init))
ace92b
+
ace92b
+static void
ace92b
+dnf_command_repolist_init (DnfCommandRepolist *self)
ace92b
+{
ace92b
+}
ace92b
+
ace92b
+// repository list table columns
ace92b
+enum { COL_REPO_ID, COL_REPO_NAME, COL_REPO_STATUS };
ace92b
+
ace92b
+static struct libscols_table *
ace92b
+create_repolist_table (gboolean with_status)
ace92b
+{
ace92b
+  struct libscols_table *table = scols_new_table ();
ace92b
+  if (isatty (1))
ace92b
+    scols_table_enable_colors (table, 1);
ace92b
+  scols_table_enable_maxout (table, 1);
ace92b
+  struct libscols_column *cl = scols_table_new_column (table, "repo id", 0.4, 0);
ace92b
+  scols_column_set_cmpfunc(cl, scols_cmpstr_cells, NULL);
ace92b
+  scols_table_new_column (table, "repo name", 0.5, SCOLS_FL_TRUNC);
ace92b
+  if (with_status)
ace92b
+    scols_table_new_column (table, "status", 0.1, SCOLS_FL_RIGHT);
ace92b
+  return table;
ace92b
+}
ace92b
+
ace92b
+static void
ace92b
+add_line_into_table (struct libscols_table *table,
ace92b
+                     gboolean               with_status,
ace92b
+                     const char            *id,
ace92b
+                     const char            *descr,
ace92b
+                     gboolean               enabled)
ace92b
+{
ace92b
+  struct libscols_line *ln = scols_table_new_line (table, NULL);
ace92b
+  scols_line_set_data (ln, COL_REPO_ID, id);
ace92b
+  scols_line_set_data (ln, COL_REPO_NAME, descr);
ace92b
+  if (with_status)
ace92b
+    {
ace92b
+      scols_line_set_data (ln, COL_REPO_STATUS, enabled ? "enabled" : "disabled");
ace92b
+      struct libscols_cell * cl = scols_line_get_cell (ln, COL_REPO_STATUS);
ace92b
+      scols_cell_set_color (cl, enabled ? "green" : "red");
ace92b
+    }
ace92b
+}
ace92b
+
ace92b
+static gboolean
ace92b
+dnf_command_repolist_run (DnfCommand      *cmd,
ace92b
+                          int              argc,
ace92b
+                          char            *argv[],
ace92b
+                          GOptionContext  *opt_ctx,
ace92b
+                          DnfContext      *ctx,
ace92b
+                          GError         **error)
ace92b
+{
ace92b
+  gboolean opt_all = FALSE;
ace92b
+  gboolean opt_enabled = FALSE;
ace92b
+  gboolean opt_disabled = FALSE;
ace92b
+  g_auto(GStrv) opt_repos = NULL;
ace92b
+  const GOptionEntry opts[] = {
ace92b
+    { "all", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opt_all, "show all repos", NULL },
ace92b
+    { "disabled", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opt_disabled, "show disabled repos", NULL },
ace92b
+    { "enabled", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opt_enabled, "show enabled repos (default)", NULL },
ace92b
+    { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &opt_repos, NULL, NULL },
ace92b
+    { NULL }
ace92b
+  };
ace92b
+  g_option_context_add_main_entries (opt_ctx, opts, NULL);
ace92b
+
ace92b
+  if (!g_option_context_parse (opt_ctx, &argc, &argv, error))
ace92b
+    return FALSE;
ace92b
+
ace92b
+  if (opt_repos && opt_repos[0])
ace92b
+    {
ace92b
+      g_set_error (error,
ace92b
+                   G_OPTION_ERROR,
ace92b
+                   G_OPTION_ERROR_UNKNOWN_OPTION,
ace92b
+                   "Unknown argument %s", opt_repos[0]);
ace92b
+      return FALSE;
ace92b
+    }
ace92b
+
ace92b
+  if (!opt_disabled)
ace92b
+    opt_enabled = TRUE;
ace92b
+
ace92b
+  if (opt_enabled && opt_disabled)
ace92b
+    opt_all = TRUE;
ace92b
+
ace92b
+  struct libscols_table *table = create_repolist_table (opt_all);
ace92b
+
ace92b
+  GPtrArray *repos = dnf_context_get_repos (ctx);
ace92b
+  for (guint i = 0; i < repos->len; ++i)
ace92b
+    {
ace92b
+      DnfRepo * repo = g_ptr_array_index (repos, i);
ace92b
+      gboolean enabled = dnf_repo_get_enabled (repo) & DNF_REPO_ENABLED_PACKAGES;
ace92b
+      if (opt_all || (opt_enabled && enabled) || (opt_disabled && !enabled))
ace92b
+        {
ace92b
+          const gchar * id = dnf_repo_get_id (repo);
ace92b
+          g_autofree gchar * descr = dnf_repo_get_description (repo);
ace92b
+          add_line_into_table (table, opt_all, id, descr, enabled);
ace92b
+        }
ace92b
+    }
ace92b
+
ace92b
+  struct libscols_column *cl = scols_table_get_column (table, COL_REPO_ID);
ace92b
+  scols_sort_table (table, cl);
ace92b
+  scols_print_table (table);
ace92b
+  scols_unref_table (table);
ace92b
+
ace92b
+  return TRUE;
ace92b
+}
ace92b
+
ace92b
+static void
ace92b
+dnf_command_repolist_class_init (DnfCommandRepolistClass *klass)
ace92b
+{
ace92b
+}
ace92b
+
ace92b
+static void
ace92b
+dnf_command_repolist_iface_init (DnfCommandInterface *iface)
ace92b
+{
ace92b
+  iface->run = dnf_command_repolist_run;
ace92b
+}
ace92b
+
ace92b
+static void
ace92b
+dnf_command_repolist_class_finalize (DnfCommandRepolistClass *klass)
ace92b
+{
ace92b
+}
ace92b
+
ace92b
+G_MODULE_EXPORT void
ace92b
+dnf_command_repolist_register_types (PeasObjectModule *module)
ace92b
+{
ace92b
+  dnf_command_repolist_register_type (G_TYPE_MODULE (module));
ace92b
+
ace92b
+  peas_object_module_register_extension_type (module,
ace92b
+                                              DNF_TYPE_COMMAND,
ace92b
+                                              DNF_TYPE_COMMAND_REPOLIST);
ace92b
+}
ace92b
diff --git a/dnf/plugins/repolist/dnf-command-repolist.gresource.xml b/dnf/plugins/repolist/dnf-command-repolist.gresource.xml
ace92b
new file mode 100644
ace92b
index 0000000..0d12b8b
ace92b
--- /dev/null
ace92b
+++ b/dnf/plugins/repolist/dnf-command-repolist.gresource.xml
ace92b
@@ -0,0 +1,6 @@
ace92b
+
ace92b
+<gresources>
ace92b
+  <gresource prefix="/org/fedoraproject/dnf/plugins/repolist">
ace92b
+    <file>repolist.plugin</file>
ace92b
+  </gresource>
ace92b
+</gresources>
ace92b
diff --git a/dnf/plugins/repolist/dnf-command-repolist.h b/dnf/plugins/repolist/dnf-command-repolist.h
ace92b
new file mode 100644
ace92b
index 0000000..5edaf1c
ace92b
--- /dev/null
ace92b
+++ b/dnf/plugins/repolist/dnf-command-repolist.h
ace92b
@@ -0,0 +1,33 @@
ace92b
+/*
ace92b
+ * Copyright (C) 2019 Red Hat, Inc.
ace92b
+ *
ace92b
+ * Licensed under the GNU Lesser General Public License Version 2.1
ace92b
+ *
ace92b
+ * This library is free software; you can redistribute it and/or
ace92b
+ * modify it under the terms of the GNU Lesser General Public
ace92b
+ * License as published by the Free Software Foundation; either
ace92b
+ * version 2.1 of the License, or (at your option) any later version.
ace92b
+ *
ace92b
+ * This library is distributed in the hope that it will be useful,
ace92b
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
ace92b
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ace92b
+ * Lesser General Public License for more details.
ace92b
+ *
ace92b
+ * You should have received a copy of the GNU Lesser General Public
ace92b
+ * License along with this library; if not, write to the Free Software
ace92b
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
ace92b
+ */
ace92b
+
ace92b
+#pragma once
ace92b
+
ace92b
+#include "dnf-command.h"
ace92b
+#include <libpeas/peas.h>
ace92b
+
ace92b
+G_BEGIN_DECLS
ace92b
+
ace92b
+#define DNF_TYPE_COMMAND_REPOLIST dnf_command_repolist_get_type ()
ace92b
+G_DECLARE_FINAL_TYPE (DnfCommandRepolist, dnf_command_repolist, DNF, COMMAND_REPOLIST, PeasExtensionBase)
ace92b
+
ace92b
+G_MODULE_EXPORT void dnf_command_repolist_register_types (PeasObjectModule *module);
ace92b
+
ace92b
+G_END_DECLS
ace92b
diff --git a/dnf/plugins/repolist/repolist.plugin b/dnf/plugins/repolist/repolist.plugin
ace92b
new file mode 100644
ace92b
index 0000000..c659a1e
ace92b
--- /dev/null
ace92b
+++ b/dnf/plugins/repolist/repolist.plugin
ace92b
@@ -0,0 +1,9 @@
ace92b
+[Plugin]
ace92b
+Module = command_repolist
ace92b
+Embedded = dnf_command_repolist_register_types
ace92b
+Name = repolist
ace92b
+Description = List repositories
ace92b
+Authors = Jaroslav Rohel <jrohel@redhat.com>
ace92b
+License = GPL-3.0+
ace92b
+Copyright = Copyright (C) 2019 Red Hat, Inc.
ace92b
+X-Command-Syntax = repolist [--all] [--disabled] [--enabled]
ace92b
--
ace92b
libgit2 0.28.2
ace92b