Blob Blame History Raw
From 83f282853e22fe3146b86b33bdaebb7a22b0a580 Mon Sep 17 00:00:00 2001
From: Jaroslav Rohel <jrohel@redhat.com>
Date: Thu, 7 Nov 2019 21:34:38 +0100
Subject: [PATCH] Add repolist command (RhBug:1584952)

The command lists repositories.

Command options:
  --all        show all repositories
  --disabled   show disabled repositories
  --enabled    show enabled repositories (default)

Signed-off-by: Jaroslav Rohel <jrohel@redhat.com>
---
 dnf/CMakeLists.txt                                      |   6 ++++++
 dnf/meson.build                                         |   9 +++++++++
 dnf/plugins/repolist/dnf-command-repolist.c             | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 dnf/plugins/repolist/dnf-command-repolist.gresource.xml |   6 ++++++
 dnf/plugins/repolist/dnf-command-repolist.h             |  33 +++++++++++++++++++++++++++++++++
 dnf/plugins/repolist/repolist.plugin                    |   9 +++++++++
 6 files changed, 230 insertions(+)
 create mode 100644 dnf/plugins/repolist/dnf-command-repolist.c
 create mode 100644 dnf/plugins/repolist/dnf-command-repolist.gresource.xml
 create mode 100644 dnf/plugins/repolist/dnf-command-repolist.h
 create mode 100644 dnf/plugins/repolist/repolist.plugin

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