From 7bf535ad25e616e6b2a20a0787e4d78962d2cb2e Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Thu, 17 Sep 2015 11:37:29 +0200 Subject: [PATCH] add modify-remote command --- app/xdg-app-builtins-add-remote.c | 94 ++++++++++++++++++++++++++----- app/xdg-app-builtins.h | 1 + app/xdg-app-main.c | 1 + 3 files changed, 81 insertions(+), 15 deletions(-) diff --git a/app/xdg-app-builtins-add-remote.c b/app/xdg-app-builtins-add-remote.c index e8d619a3..2d264123 100644 --- a/app/xdg-app-builtins-add-remote.c +++ b/app/xdg-app-builtins-add-remote.c @@ -32,12 +32,24 @@ #include "xdg-app-utils.h" static gboolean opt_no_gpg_verify; +static gboolean opt_do_gpg_verify; static gboolean opt_if_not_exists; static char *opt_title; +static char *opt_url; -static GOptionEntry options[] = { - { "no-gpg-verify", 0, 0, G_OPTION_ARG_NONE, &opt_no_gpg_verify, "Disable GPG verification", NULL }, +static GOptionEntry add_options[] = { { "if-not-exists", 0, 0, G_OPTION_ARG_NONE, &opt_if_not_exists, "Do nothing if the provided remote exists", NULL }, + { NULL } +}; + +static GOptionEntry modify_options[] = { + { "gpg-verify", 0, 0, G_OPTION_ARG_NONE, &opt_do_gpg_verify, "Enable GPG verification", NULL }, + { "url", 0, 0, G_OPTION_ARG_STRING, &opt_url, "Set a new url", NULL }, + { NULL } +}; + +static GOptionEntry common_options[] = { + { "no-gpg-verify", 0, 0, G_OPTION_ARG_NONE, &opt_no_gpg_verify, "Disable GPG verification", NULL }, { "title", 0, 0, G_OPTION_ARG_STRING, &opt_title, "A nice name to use for this remote", "TITLE" }, { NULL } }; @@ -45,8 +57,7 @@ static GOptionEntry options[] = { gboolean xdg_app_builtin_add_remote (int argc, char **argv, GCancellable *cancellable, GError **error) { - GOptionContext *context; - gboolean ret = FALSE; + g_autoptr(GOptionContext) context = NULL; g_autoptr(XdgAppDir) dir = NULL; g_autoptr(GVariantBuilder) optbuilder = NULL; g_autoptr(GHashTable) refs = NULL; @@ -56,13 +67,15 @@ xdg_app_builtin_add_remote (int argc, char **argv, GCancellable *cancellable, GE context = g_option_context_new ("NAME URL - Add a remote repository"); - if (!xdg_app_option_context_parse (context, options, &argc, &argv, 0, &dir, cancellable, error)) - goto out; + g_option_context_add_main_entries (context, common_options, NULL); + + if (!xdg_app_option_context_parse (context, add_options, &argc, &argv, 0, &dir, cancellable, error)) + return FALSE; if (argc < 3) { usage_error (context, "NAME and URL must be specified", error); - goto out; + return FALSE; } remote_name = argv[1]; @@ -71,7 +84,7 @@ xdg_app_builtin_add_remote (int argc, char **argv, GCancellable *cancellable, GE optbuilder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}")); if (!ostree_repo_load_summary (remote_url, &refs, &title, cancellable, error)) - goto out; + return FALSE; if (opt_no_gpg_verify) g_variant_builder_add (optbuilder, "{s@v}", @@ -95,12 +108,63 @@ xdg_app_builtin_add_remote (int argc, char **argv, GCancellable *cancellable, GE remote_name, remote_url, g_variant_builder_end (optbuilder), cancellable, error)) - goto out; + return FALSE; - ret = TRUE; - - out: - if (context) - g_option_context_free (context); - return ret; + return TRUE; +} + +gboolean +xdg_app_builtin_modify_remote (int argc, char **argv, GCancellable *cancellable, GError **error) +{ + g_autoptr(GOptionContext) context = NULL; + g_autoptr(XdgAppDir) dir = NULL; + g_autoptr(GVariantBuilder) optbuilder = NULL; + g_autoptr(GHashTable) refs = NULL; + g_autoptr(GKeyFile) config = NULL; + const char *remote_name; + g_autofree char *group = NULL; + + context = g_option_context_new ("NAME - Modify a remote repository"); + + g_option_context_add_main_entries (context, common_options, NULL); + + if (!xdg_app_option_context_parse (context, modify_options, &argc, &argv, 0, &dir, cancellable, error)) + return FALSE; + + if (argc < 2) + { + usage_error (context, "remote NAME must be specified", error); + return FALSE; + } + + remote_name = argv[1]; + + optbuilder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}")); + + group = g_strdup_printf ("remote \"%s\"", remote_name); + + if (!ostree_repo_remote_get_url (xdg_app_dir_get_repo (dir), remote_name, NULL, NULL)) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "No remote %s", remote_name); + return FALSE; + } + + config = ostree_repo_copy_config (xdg_app_dir_get_repo (dir)); + + if (opt_no_gpg_verify) + g_key_file_set_boolean (config, group, "gpg-verify", FALSE); + + if (opt_do_gpg_verify) + g_key_file_set_boolean (config, group, "gpg-verify", TRUE); + + if (opt_url) + g_key_file_set_string (config, group, "url", opt_url); + + if (opt_title) + g_key_file_set_string (config, group, "xa.title", opt_title); + + if (!ostree_repo_write_config (xdg_app_dir_get_repo (dir), config, error)) + return FALSE; + + return TRUE; } diff --git a/app/xdg-app-builtins.h b/app/xdg-app-builtins.h index 90949450..fe8c0351 100644 --- a/app/xdg-app-builtins.h +++ b/app/xdg-app-builtins.h @@ -49,6 +49,7 @@ void usage_error (GOptionContext *context, #define BUILTINPROTO(name) gboolean xdg_app_builtin_ ## name (int argc, char **argv, GCancellable *cancellable, GError **error) BUILTINPROTO(add_remote); +BUILTINPROTO(modify_remote); BUILTINPROTO(delete_remote); BUILTINPROTO(list_remotes); BUILTINPROTO(repo_contents); diff --git a/app/xdg-app-main.c b/app/xdg-app-main.c index f40da309..829e0f54 100644 --- a/app/xdg-app-main.c +++ b/app/xdg-app-main.c @@ -43,6 +43,7 @@ typedef struct { static XdgAppCommand commands[] = { { "add-remote", xdg_app_builtin_add_remote }, { "delete-remote", xdg_app_builtin_delete_remote }, + { "modify-remote", xdg_app_builtin_modify_remote }, { "list-remotes", xdg_app_builtin_list_remotes }, { "repo-contents", xdg_app_builtin_repo_contents }, { "install-runtime", xdg_app_builtin_install_runtime },