diff --git a/app/xdg-app-builtins-list-remotes.c b/app/xdg-app-builtins-list-remotes.c index 3c8aec20..633a9b90 100644 --- a/app/xdg-app-builtins-list-remotes.c +++ b/app/xdg-app-builtins-list-remotes.c @@ -30,18 +30,87 @@ #include "xdg-app-builtins.h" -static gboolean opt_show_urls; +static gboolean opt_show_details; static GOptionEntry options[] = { - { "show-urls", 0, 0, G_OPTION_ARG_NONE, &opt_show_urls, "Show remote URLs in list", NULL }, + { "details", 'd', 0, G_OPTION_ARG_NONE, &opt_show_details, "Show remote details", NULL }, { NULL } }; +typedef struct { + GPtrArray *rows; + GPtrArray *current; + int n_columns; +} TablePrinter; + +static TablePrinter * +table_printer_new (void) +{ + TablePrinter *printer = g_new0 (TablePrinter, 1); + printer->rows = g_ptr_array_new_with_free_func ((GDestroyNotify)g_strfreev); + printer->current = g_ptr_array_new_with_free_func (g_free); + + return printer; +} + +static void +table_printer_free (TablePrinter *printer) +{ + g_ptr_array_free (printer->rows, TRUE); + g_ptr_array_free (printer->current, TRUE); + g_free (printer); +} + +static void +table_printer_add_column (TablePrinter *printer, + const char *text) +{ + g_ptr_array_add (printer->current, text ? g_strdup (text) : g_strdup ("")); +} + +static void +table_printer_finish_row (TablePrinter *printer) +{ + printer->n_columns = MAX (printer->n_columns, printer->current->len); + g_ptr_array_add (printer->current, NULL); + g_ptr_array_add (printer->rows, + g_ptr_array_free (printer->current, FALSE)); + printer->current = g_ptr_array_new_with_free_func (g_free); +} + +static void +table_printer_print (TablePrinter *printer) +{ + g_autofree int *widths = NULL; + int i, j; + + if (printer->current->len != 0) + table_printer_finish_row (printer); + + widths = g_new0 (int, printer->n_columns); + + for (i = 0; i < printer->rows->len; i++) + { + char **row = g_ptr_array_index (printer->rows,i); + + for (j = 0; row[j] != NULL; j++) + widths[j] = MAX (widths[j], strlen (row[j])); + } + + for (i = 0; i < printer->rows->len; i++) + { + char **row = g_ptr_array_index (printer->rows,i); + + for (j = 0; row[j] != NULL; j++) + g_print ("%-*s%s", widths[j], row[j], j == printer->n_columns - 1 ? "" : " "); + g_print ("\n"); + } +} + gboolean xdg_app_builtin_list_remotes (int argc, char **argv, GCancellable *cancellable, GError **error) { - GOptionContext *context; - gboolean ret = FALSE; + g_autoptr(GOptionContext) context = NULL; g_autoptr(XdgAppDir) dir = NULL; g_auto(GStrv) remotes = NULL; guint ii, n_remotes = 0; @@ -49,26 +118,57 @@ xdg_app_builtin_list_remotes (int argc, char **argv, GCancellable *cancellable, context = g_option_context_new (" - List remote repositories"); if (!xdg_app_option_context_parse (context, options, &argc, &argv, 0, &dir, cancellable, error)) - goto out; + return FALSE; remotes = ostree_repo_remote_list (xdg_app_dir_get_repo (dir), &n_remotes); - if (opt_show_urls) + if (opt_show_details) { - int max_length = 0; - - for (ii = 0; ii < n_remotes; ii++) - max_length = MAX (max_length, strlen (remotes[ii])); + TablePrinter *printer = table_printer_new (); + GKeyFile *config = ostree_repo_get_config (xdg_app_dir_get_repo (dir));; + g_autoptr(GString) options = g_string_new (""); for (ii = 0; ii < n_remotes; ii++) { + char *remote_name = remotes[ii]; g_autofree char *remote_url = NULL; + g_autofree char *group = NULL; + g_autofree char *title = NULL; + gboolean gpg_verify = TRUE; - if (!ostree_repo_remote_get_url (xdg_app_dir_get_repo (dir), remotes[ii], &remote_url, error)) - goto out; + group = g_strdup_printf ("remote \"%s\"", remote_name); - g_print ("%-*s %s\n", max_length, remotes[ii], remote_url); + table_printer_add_column (printer, remote_name); + + title = g_key_file_get_string (config, group, "xa.title", NULL); + if (title) + table_printer_add_column (printer, title); + else + table_printer_add_column (printer, "-"); + + ostree_repo_remote_get_url (xdg_app_dir_get_repo (dir), remote_name, &remote_url, NULL); + + table_printer_add_column (printer, remote_url); + + ostree_repo_remote_get_gpg_verify (xdg_app_dir_get_repo (dir), remote_name, + &gpg_verify, NULL); + if (!gpg_verify) + { + if (options->len != 0) + g_string_append_c (options, ','); + g_string_append (options, "no-gpg-verify"); + } + + if (options->len != 0) + table_printer_add_column (printer, options->str); + + g_string_truncate (options, 0); + + table_printer_finish_row (printer); } + + table_printer_print (printer); + table_printer_free (printer); } else { @@ -76,10 +176,5 @@ xdg_app_builtin_list_remotes (int argc, char **argv, GCancellable *cancellable, g_print ("%s\n", remotes[ii]); } - ret = TRUE; - - out: - if (context) - g_option_context_free (context); - return ret; + return TRUE; }