From e7246e6d6406da86498480b73363a1bfdcd9f8af Mon Sep 17 00:00:00 2001 From: Matthew Barnes Date: Wed, 29 Apr 2015 11:09:40 -0400 Subject: [PATCH] ostree: Split up "remote" subcommands To make room for "remote gpg-import", which will be non-trivial. ot-builtin-remote.c was already a little too crowded anyway. Also while we're at it, port this bit of code away from libgsystem. --- Makefile-ostree.am | 9 + src/ostree/ot-builtin-remote.c | 247 +----------------------- src/ostree/ot-remote-builtin-add.c | 117 +++++++++++ src/ostree/ot-remote-builtin-delete.c | 71 +++++++ src/ostree/ot-remote-builtin-list.c | 81 ++++++++ src/ostree/ot-remote-builtin-show-url.c | 65 +++++++ src/ostree/ot-remote-builtins.h | 32 +++ 7 files changed, 382 insertions(+), 240 deletions(-) create mode 100644 src/ostree/ot-remote-builtin-add.c create mode 100644 src/ostree/ot-remote-builtin-delete.c create mode 100644 src/ostree/ot-remote-builtin-list.c create mode 100644 src/ostree/ot-remote-builtin-show-url.c create mode 100644 src/ostree/ot-remote-builtins.h diff --git a/Makefile-ostree.am b/Makefile-ostree.am index 0061de41..f420e28c 100644 --- a/Makefile-ostree.am +++ b/Makefile-ostree.am @@ -72,6 +72,15 @@ ostree_SOURCES += \ src/ostree/ot-admin-functions.c \ $(NULL) +# Remote subcommand +ostree_SOURCES += \ + src/ostree/ot-remote-builtins.h \ + src/ostree/ot-remote-builtin-add.c \ + src/ostree/ot-remote-builtin-delete.c \ + src/ostree/ot-remote-builtin-list.c \ + src/ostree/ot-remote-builtin-show-url.c \ + $(NULL) + ostree_bin_shared_cflags = $(AM_CFLAGS) -I$(srcdir)/src/libotutil -I$(srcdir)/src/libostree -I$(srcdir)/src/ostree \ $(NULL) ostree_bin_shared_ldadd = libglnx.la libbsdiff.la libotutil.la libostree-kernel-args.la libostree-1.la diff --git a/src/ostree/ot-builtin-remote.c b/src/ostree/ot-builtin-remote.c index 6db92260..608b9ef1 100644 --- a/src/ostree/ot-builtin-remote.c +++ b/src/ostree/ot-builtin-remote.c @@ -24,240 +24,7 @@ #include "ot-main.h" #include "ot-builtins.h" -#include "ostree.h" -#include "ot-tool-util.h" -#include "otutil.h" - -static void -usage_error (GOptionContext *context, const char *message, GError **error) -{ - gs_free gchar *help = g_option_context_get_help (context, TRUE, NULL); - g_printerr ("%s", help); - g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, message); -} - -static char **opt_set; -static gboolean opt_no_gpg_verify; -static gboolean opt_if_not_exists; - -static GOptionEntry add_option_entries[] = { - { "set", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_set, "Set config option KEY=VALUE for remote", "KEY=VALUE" }, - { "no-gpg-verify", 0, 0, G_OPTION_ARG_NONE, &opt_no_gpg_verify, "Disable GPG verification", NULL }, - { "if-not-exists", 0, 0, G_OPTION_ARG_NONE, &opt_if_not_exists, "Do nothing if the provided remote exists", NULL }, - { NULL } -}; - -static gboolean -ostree_remote_builtin_add (int argc, char **argv, GCancellable *cancellable, GError **error) -{ - GOptionContext *context; - gs_unref_object OstreeRepo *repo = NULL; - const char *remote_name; - const char *remote_url; - char **iter; - gs_free char *target_name = NULL; - gs_unref_object GFile *target_conf = NULL; - gs_unref_variant_builder GVariantBuilder *optbuilder = NULL; - gboolean ret = FALSE; - - context = g_option_context_new ("NAME URL [BRANCH...] - Add a remote repository"); - - if (!ostree_option_context_parse (context, add_option_entries, &argc, &argv, - OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error)) - goto out; - - if (argc < 3) - { - usage_error (context, "NAME and URL must be specified", error); - goto out; - } - - remote_name = argv[1]; - remote_url = argv[2]; - - optbuilder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}")); - - if (argc > 3) - { - gs_unref_ptrarray GPtrArray *branchesp = g_ptr_array_new (); - int i; - - for (i = 3; i < argc; i++) - g_ptr_array_add (branchesp, argv[i]); - g_ptr_array_add (branchesp, NULL); - - g_variant_builder_add (optbuilder, "{s@v}", - "branches", - g_variant_new_variant (g_variant_new_strv ((const char*const*)branchesp->pdata, -1))); - } - - for (iter = opt_set; iter && *iter; iter++) - { - const char *keyvalue = *iter; - gs_free char *subkey = NULL; - gs_free char *subvalue = NULL; - - if (!ot_parse_keyvalue (keyvalue, &subkey, &subvalue, error)) - goto out; - - g_variant_builder_add (optbuilder, "{s@v}", - subkey, g_variant_new_variant (g_variant_new_string (subvalue))); - } - - if (opt_no_gpg_verify) - g_variant_builder_add (optbuilder, "{s@v}", - "gpg-verify", - g_variant_new_variant (g_variant_new_boolean (FALSE))); - - if (!ostree_repo_remote_change (repo, NULL, - opt_if_not_exists ? OSTREE_REPO_REMOTE_CHANGE_ADD_IF_NOT_EXISTS : - OSTREE_REPO_REMOTE_CHANGE_ADD, - remote_name, remote_url, - g_variant_builder_end (optbuilder), - cancellable, error)) - goto out; - - ret = TRUE; - out: - g_option_context_free (context); - - return ret; -} - -gboolean opt_if_exists = FALSE; - -static GOptionEntry delete_option_entries[] = { - { "if-exists", 0, 0, G_OPTION_ARG_NONE, &opt_if_exists, "Do nothing if the provided remote does not exist", NULL }, - { NULL } -}; - -static gboolean -ostree_remote_builtin_delete (int argc, char **argv, GCancellable *cancellable, GError **error) -{ - GOptionContext *context; - gs_unref_object OstreeRepo *repo = NULL; - const char *remote_name; - gboolean ret = FALSE; - - context = g_option_context_new ("NAME - Delete a remote repository"); - - if (!ostree_option_context_parse (context, delete_option_entries, &argc, &argv, - OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error)) - goto out; - - if (argc < 2) - { - usage_error (context, "NAME must be specified", error); - goto out; - } - - remote_name = argv[1]; - - if (!ostree_repo_remote_change (repo, NULL, - opt_if_exists ? OSTREE_REPO_REMOTE_CHANGE_DELETE_IF_EXISTS : - OSTREE_REPO_REMOTE_CHANGE_DELETE, - remote_name, NULL, NULL, - cancellable, error)) - goto out; - - ret = TRUE; - out: - g_option_context_free (context); - - return ret; -} - -static GOptionEntry show_url_option_entries[] = { - { NULL } -}; - -static gboolean -ostree_remote_builtin_show_url (int argc, char **argv, GCancellable *cancellable, GError **error) -{ - GOptionContext *context; - gs_unref_object OstreeRepo *repo = NULL; - const char *remote_name; - gs_free char *remote_url = NULL; - gboolean ret = FALSE; - - context = g_option_context_new ("NAME - Show remote repository URL"); - - if (!ostree_option_context_parse (context, show_url_option_entries, &argc, &argv, - OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error)) - goto out; - - if (argc < 2) - { - usage_error (context, "NAME must be specified", error); - goto out; - } - - remote_name = argv[1]; - - if (ostree_repo_remote_get_url (repo, remote_name, &remote_url, error)) - { - g_print ("%s\n", remote_url); - ret = TRUE; - } - - out: - return ret; -} - -static gboolean opt_show_urls; - -static GOptionEntry list_option_entries[] = { - { "show-urls", 'u', 0, G_OPTION_ARG_NONE, &opt_show_urls, "Show remote URLs in list", NULL }, - { NULL } -}; - -static gboolean -ostree_remote_builtin_list (int argc, char **argv, GCancellable *cancellable, GError **error) -{ - GOptionContext *context; - gs_unref_object OstreeRepo *repo = NULL; - gs_strfreev char **remotes = NULL; - guint ii, n_remotes = 0; - gboolean ret = FALSE; - - context = g_option_context_new ("- List remote repository names"); - - if (!ostree_option_context_parse (context, list_option_entries, &argc, &argv, - OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error)) - goto out; - - remotes = ostree_repo_remote_list (repo, &n_remotes); - - if (opt_show_urls) - { - int max_length = 0; - - for (ii = 0; ii < n_remotes; ii++) - max_length = MAX (max_length, strlen (remotes[ii])); - - for (ii = 0; ii < n_remotes; ii++) - { - gs_free char *remote_url = NULL; - - if (!ostree_repo_remote_get_url (repo, remotes[ii], &remote_url, error)) - goto out; - - g_print ("%-*s %s\n", max_length, remotes[ii], remote_url); - } - } - else - { - for (ii = 0; ii < n_remotes; ii++) - g_print ("%s\n", remotes[ii]); - } - - ret = TRUE; - - out: - g_option_context_free (context); - - return ret; -} +#include "ot-remote-builtins.h" typedef struct { const char *name; @@ -265,10 +32,10 @@ typedef struct { } OstreeRemoteCommand; static OstreeRemoteCommand remote_subcommands[] = { - { "add", ostree_remote_builtin_add }, - { "delete", ostree_remote_builtin_delete }, - { "show-url", ostree_remote_builtin_show_url }, - { "list", ostree_remote_builtin_list }, + { "add", ot_remote_builtin_add }, + { "delete", ot_remote_builtin_delete }, + { "show-url", ot_remote_builtin_show_url }, + { "list", ot_remote_builtin_list }, { NULL, NULL } }; @@ -301,7 +68,7 @@ ostree_builtin_remote (int argc, char **argv, GCancellable *cancellable, GError { OstreeRemoteCommand *subcommand; const char *subcommand_name = NULL; - gs_free char *prgname = NULL; + g_autofree char *prgname = NULL; gboolean ret = FALSE; int in, out; @@ -339,7 +106,7 @@ ostree_builtin_remote (int argc, char **argv, GCancellable *cancellable, GError if (!subcommand->name) { GOptionContext *context; - gs_free char *help; + g_autofree char *help; context = remote_option_context_new_with_commands (); diff --git a/src/ostree/ot-remote-builtin-add.c b/src/ostree/ot-remote-builtin-add.c new file mode 100644 index 00000000..99e47e7c --- /dev/null +++ b/src/ostree/ot-remote-builtin-add.c @@ -0,0 +1,117 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 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., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include + +#include "otutil.h" +#include "ot-tool-util.h" + +#include "ot-main.h" +#include "ot-remote-builtins.h" + +static char **opt_set; +static gboolean opt_no_gpg_verify; +static gboolean opt_if_not_exists; + +static GOptionEntry option_entries[] = { + { "set", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_set, "Set config option KEY=VALUE for remote", "KEY=VALUE" }, + { "no-gpg-verify", 0, 0, G_OPTION_ARG_NONE, &opt_no_gpg_verify, "Disable GPG verification", NULL }, + { "if-not-exists", 0, 0, G_OPTION_ARG_NONE, &opt_if_not_exists, "Do nothing if the provided remote exists", NULL }, + { NULL } +}; + +gboolean +ot_remote_builtin_add (int argc, char **argv, GCancellable *cancellable, GError **error) +{ + GOptionContext *context; + glnx_unref_object OstreeRepo *repo = NULL; + const char *remote_name; + const char *remote_url; + char **iter; + g_autofree char *target_name = NULL; + glnx_unref_object GFile *target_conf = NULL; + g_autoptr(GVariantBuilder) optbuilder = NULL; + gboolean ret = FALSE; + + context = g_option_context_new ("NAME URL [BRANCH...] - Add a remote repository"); + + if (!ostree_option_context_parse (context, option_entries, &argc, &argv, + OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error)) + goto out; + + if (argc < 3) + { + ot_util_usage_error (context, "NAME and URL must be specified", error); + goto out; + } + + remote_name = argv[1]; + remote_url = argv[2]; + + optbuilder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}")); + + if (argc > 3) + { + g_autoptr(GPtrArray) branchesp = g_ptr_array_new (); + int i; + + for (i = 3; i < argc; i++) + g_ptr_array_add (branchesp, argv[i]); + g_ptr_array_add (branchesp, NULL); + + g_variant_builder_add (optbuilder, "{s@v}", + "branches", + g_variant_new_variant (g_variant_new_strv ((const char*const*)branchesp->pdata, -1))); + } + + for (iter = opt_set; iter && *iter; iter++) + { + const char *keyvalue = *iter; + g_autofree char *subkey = NULL; + g_autofree char *subvalue = NULL; + + if (!ot_parse_keyvalue (keyvalue, &subkey, &subvalue, error)) + goto out; + + g_variant_builder_add (optbuilder, "{s@v}", + subkey, g_variant_new_variant (g_variant_new_string (subvalue))); + } + + if (opt_no_gpg_verify) + g_variant_builder_add (optbuilder, "{s@v}", + "gpg-verify", + g_variant_new_variant (g_variant_new_boolean (FALSE))); + + if (!ostree_repo_remote_change (repo, NULL, + opt_if_not_exists ? OSTREE_REPO_REMOTE_CHANGE_ADD_IF_NOT_EXISTS : + OSTREE_REPO_REMOTE_CHANGE_ADD, + remote_name, remote_url, + g_variant_builder_end (optbuilder), + cancellable, error)) + goto out; + + ret = TRUE; + out: + g_option_context_free (context); + + return ret; +} diff --git a/src/ostree/ot-remote-builtin-delete.c b/src/ostree/ot-remote-builtin-delete.c new file mode 100644 index 00000000..d8d16db8 --- /dev/null +++ b/src/ostree/ot-remote-builtin-delete.c @@ -0,0 +1,71 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 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., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include + +#include "otutil.h" + +#include "ot-main.h" +#include "ot-remote-builtins.h" + +gboolean opt_if_exists = FALSE; + +static GOptionEntry option_entries[] = { + { "if-exists", 0, 0, G_OPTION_ARG_NONE, &opt_if_exists, "Do nothing if the provided remote does not exist", NULL }, + { NULL } +}; + +gboolean +ot_remote_builtin_delete (int argc, char **argv, GCancellable *cancellable, GError **error) +{ + GOptionContext *context; + glnx_unref_object OstreeRepo *repo = NULL; + const char *remote_name; + gboolean ret = FALSE; + + context = g_option_context_new ("NAME - Delete a remote repository"); + + if (!ostree_option_context_parse (context, option_entries, &argc, &argv, + OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error)) + goto out; + + if (argc < 2) + { + ot_util_usage_error (context, "NAME must be specified", error); + goto out; + } + + remote_name = argv[1]; + + if (!ostree_repo_remote_change (repo, NULL, + opt_if_exists ? OSTREE_REPO_REMOTE_CHANGE_DELETE_IF_EXISTS : + OSTREE_REPO_REMOTE_CHANGE_DELETE, + remote_name, NULL, NULL, + cancellable, error)) + goto out; + + ret = TRUE; + out: + g_option_context_free (context); + + return ret; +} diff --git a/src/ostree/ot-remote-builtin-list.c b/src/ostree/ot-remote-builtin-list.c new file mode 100644 index 00000000..8e4c3d1d --- /dev/null +++ b/src/ostree/ot-remote-builtin-list.c @@ -0,0 +1,81 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 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., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include + +#include "ot-main.h" +#include "ot-remote-builtins.h" + +static gboolean opt_show_urls; + +static GOptionEntry option_entries[] = { + { "show-urls", 'u', 0, G_OPTION_ARG_NONE, &opt_show_urls, "Show remote URLs in list", NULL }, + { NULL } +}; + +gboolean +ot_remote_builtin_list (int argc, char **argv, GCancellable *cancellable, GError **error) +{ + GOptionContext *context; + glnx_unref_object OstreeRepo *repo = NULL; + g_auto(GStrv) remotes = NULL; + guint ii, n_remotes = 0; + gboolean ret = FALSE; + + context = g_option_context_new ("- List remote repository names"); + + if (!ostree_option_context_parse (context, option_entries, &argc, &argv, + OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error)) + goto out; + + remotes = ostree_repo_remote_list (repo, &n_remotes); + + if (opt_show_urls) + { + int max_length = 0; + + for (ii = 0; ii < n_remotes; ii++) + max_length = MAX (max_length, strlen (remotes[ii])); + + for (ii = 0; ii < n_remotes; ii++) + { + g_autofree char *remote_url = NULL; + + if (!ostree_repo_remote_get_url (repo, remotes[ii], &remote_url, error)) + goto out; + + g_print ("%-*s %s\n", max_length, remotes[ii], remote_url); + } + } + else + { + for (ii = 0; ii < n_remotes; ii++) + g_print ("%s\n", remotes[ii]); + } + + ret = TRUE; + + out: + g_option_context_free (context); + + return ret; +} diff --git a/src/ostree/ot-remote-builtin-show-url.c b/src/ostree/ot-remote-builtin-show-url.c new file mode 100644 index 00000000..dea8efc3 --- /dev/null +++ b/src/ostree/ot-remote-builtin-show-url.c @@ -0,0 +1,65 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 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., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include + +#include "otutil.h" + +#include "ot-main.h" +#include "ot-remote-builtins.h" + +static GOptionEntry option_entries[] = { + { NULL } +}; + +gboolean +ot_remote_builtin_show_url (int argc, char **argv, GCancellable *cancellable, GError **error) +{ + GOptionContext *context; + glnx_unref_object OstreeRepo *repo = NULL; + const char *remote_name; + g_autofree char *remote_url = NULL; + gboolean ret = FALSE; + + context = g_option_context_new ("NAME - Show remote repository URL"); + + if (!ostree_option_context_parse (context, option_entries, &argc, &argv, + OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error)) + goto out; + + if (argc < 2) + { + ot_util_usage_error (context, "NAME must be specified", error); + goto out; + } + + remote_name = argv[1]; + + if (ostree_repo_remote_get_url (repo, remote_name, &remote_url, error)) + { + g_print ("%s\n", remote_url); + ret = TRUE; + } + + out: + return ret; +} diff --git a/src/ostree/ot-remote-builtins.h b/src/ostree/ot-remote-builtins.h new file mode 100644 index 00000000..833b6149 --- /dev/null +++ b/src/ostree/ot-remote-builtins.h @@ -0,0 +1,32 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 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., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#pragma once + +#include + +G_BEGIN_DECLS + +gboolean ot_remote_builtin_add (int argc, char **argv, GCancellable *cancellable, GError **error); +gboolean ot_remote_builtin_delete (int argc, char **argv, GCancellable *cancellable, GError **error); +gboolean ot_remote_builtin_list (int argc, char **argv, GCancellable *cancellable, GError **error); +gboolean ot_remote_builtin_show_url (int argc, char **argv, GCancellable *cancellable, GError **error); + +G_END_DECLS