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

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