From 4cec1e393495d4d31de3f5816f7deb2ea395fd8b Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 15 Jan 2015 23:25:03 -0500 Subject: [PATCH] Add a repo-contents command This can show available runtimes and applications. --- Makefile.am | 1 + xdg-app-builtins-repo-contents.c | 95 ++++++++++++++++++++++++++++++++ xdg-app-builtins.h | 1 + xdg-app-main.c | 1 + 4 files changed, 98 insertions(+) create mode 100644 xdg-app-builtins-repo-contents.c diff --git a/Makefile.am b/Makefile.am index e8b9c74a..835f2c81 100644 --- a/Makefile.am +++ b/Makefile.am @@ -30,6 +30,7 @@ xdg_app_SOURCES = \ xdg-app-builtins-add-repo.c \ xdg-app-builtins-delete-repo.c \ xdg-app-builtins-list-repos.c \ + xdg-app-builtins-repo-contents.c \ xdg-app-builtins-install.c \ xdg-app-builtins-update.c \ xdg-app-builtins-list.c \ diff --git a/xdg-app-builtins-repo-contents.c b/xdg-app-builtins-repo-contents.c new file mode 100644 index 00000000..e27ef400 --- /dev/null +++ b/xdg-app-builtins-repo-contents.c @@ -0,0 +1,95 @@ +#include "config.h" + +#include +#include +#include +#include + +#include "libgsystem.h" + +#include "xdg-app-builtins.h" +#include "xdg-app-utils.h" + +static gboolean opt_show_details; +static gboolean opt_only_runtimes; +static gboolean opt_only_apps; + +static GOptionEntry options[] = { + { "show-details", 0, 0, G_OPTION_ARG_NONE, &opt_show_details, "Show arches and branches", NULL }, + { "only-runtimes", 0, 0, G_OPTION_ARG_NONE, &opt_only_runtimes, "Show only runtimes", NULL }, + { "only-apps", 0, 0, G_OPTION_ARG_NONE, &opt_only_apps, "Show only apps", NULL }, + { NULL } +}; + +gboolean +xdg_app_builtin_repo_contents (int argc, char **argv, GCancellable *cancellable, GError **error) +{ + gboolean ret = FALSE; + GOptionContext *context; + gs_unref_object XdgAppDir *dir = NULL; + gs_unref_object OstreeRepo *repo = NULL; + gs_unref_hashtable GHashTable *refs = NULL; + GHashTableIter iter; + gpointer key; + gs_unref_hashtable GHashTable *seen = NULL; + + context = g_option_context_new (" - Show available runtimes and applications"); + + if (!xdg_app_option_context_parse (context, options, &argc, &argv, 0, &dir, cancellable, error)) + goto out; + + repo = xdg_app_dir_get_repo (dir); + if (!ostree_repo_list_refs (repo, NULL, &refs, cancellable, error)) + goto out; + + seen = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); + + g_hash_table_iter_init (&iter, refs); + while (g_hash_table_iter_next (&iter, &key, NULL)) + { + const char *refspec = key; + gs_free char *remote = NULL; + gs_free char *ref = NULL; + char *name = NULL; + char *p; + + if (!ostree_parse_refspec (refspec, &remote, &ref, error)) + goto out; + + if (g_str_has_prefix (ref, "runtime/") && !opt_only_apps) + { + name = g_strdup (ref + strlen ("runtime/")); + if (!opt_show_details) + { + p = strchr (name, '/'); + if (p) + *p = 0; + } + } + + if (g_str_has_prefix (ref, "app/") && !opt_only_runtimes) + { + name = g_strdup (ref + strlen ("app/")); + if (!opt_show_details) + { + p = strchr (name, '/'); + if (p) + *p = 0; + } + } + + if (name && !g_hash_table_contains (seen, name)) + { + g_hash_table_add (seen, name); + g_print ("%s\n", name); + } + } + + ret = TRUE; + + out: + if (context) + g_option_context_free (context); + + return ret; +} diff --git a/xdg-app-builtins.h b/xdg-app-builtins.h index c53d98c8..81d9d427 100644 --- a/xdg-app-builtins.h +++ b/xdg-app-builtins.h @@ -31,6 +31,7 @@ void usage_error (GOptionContext *context, BUILTINPROTO(add_repo); BUILTINPROTO(delete_repo); BUILTINPROTO(list_repos); +BUILTINPROTO(repo_contents); BUILTINPROTO(install_runtime); BUILTINPROTO(update_runtime); BUILTINPROTO(list_runtimes); diff --git a/xdg-app-main.c b/xdg-app-main.c index 4228fa98..22d48bdb 100644 --- a/xdg-app-main.c +++ b/xdg-app-main.c @@ -22,6 +22,7 @@ static XdgAppCommand commands[] = { { "add-repo", xdg_app_builtin_add_repo }, { "delete-repo", xdg_app_builtin_delete_repo }, { "list-repos", xdg_app_builtin_list_repos }, + { "repo-contents", xdg_app_builtin_repo_contents }, { "install-runtime", xdg_app_builtin_install_runtime }, { "update-runtime", xdg_app_builtin_update_runtime }, { "list-runtimes", xdg_app_builtin_list_runtimes },