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

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