From 95555a85a1b39b65cc6b01658b88f609889c6ef1 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Tue, 19 Nov 2024 11:41:47 +0100 Subject: [PATCH] pkg: add a repositories command This command allows to list repositories it takes 3 arguemnts: -l: only list the repository names -e: show only enabled ones (by default all repositories are shown) -d: show only disabled ones (by default all repositories are shown) if -d and -e are provided all the repositories are shown Fixes: #2134 --- src/Makefile.autosetup | 1 + src/main.c | 82 +++----------------------------------- src/pkgcli.h | 29 ++++---------- src/repositories.c | 90 ++++++++++++++++++++++++++++++++++++++++++ src/utils.c | 81 +++++++++++++++++++++++++++---------- 5 files changed, 164 insertions(+), 119 deletions(-) create mode 100644 src/repositories.c diff --git a/src/Makefile.autosetup b/src/Makefile.autosetup index ce19451f4..fc02d835f 100644 --- a/src/Makefile.autosetup +++ b/src/Makefile.autosetup @@ -22,6 +22,7 @@ SRCS= add.c \ query.c \ register.c \ repo.c \ + repositories.c \ rquery.c \ search.c \ set.c \ diff --git a/src/main.c b/src/main.c index 39665ea58..0abc070c8 100644 --- a/src/main.c +++ b/src/main.c @@ -1,32 +1,12 @@ /*- - * Copyright (c) 2011-2013 Baptiste Daroussin + * Copyright (c) 2011-2024 Baptiste Daroussin * Copyright (c) 2011-2012 Julien Laffaye * Copyright (c) 2011 Will Andrews * Copyright (c) 2011-2012 Marin Atanasov Nikolov * Copyright (c) 2014-2015 Matthew Seaman * Copyright (c) 2014 Vsevolod Stakhov - * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer - * in this position and unchanged. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-2-Clause */ #ifdef HAVE_CONFIG_H @@ -100,6 +80,7 @@ static struct commands { { "register", "Registers a package into the local database", exec_register, usage_register}, { "remove", "Deletes packages from the database and the system", exec_delete, usage_delete}, { "repo", "Creates a package repository catalogue", exec_repo, usage_repo}, + { "repositories", "Show repositories informations", exec_repositories, usage_repositories}, { "rquery", "Queries information in repository catalogues", exec_rquery, usage_rquery}, { "search", "Performs a search of package repository catalogues", exec_search, usage_search}, { "set", "Modifies information about packages in the local database", exec_set, usage_set}, @@ -302,64 +283,11 @@ show_plugin_info(void) static void show_repository_info(void) { - const char *mirror, *sig; struct pkg_repo *repo = NULL; printf("\nRepositories:\n"); - while (pkg_repos(&repo) == EPKG_OK) { - switch (pkg_repo_mirror_type(repo)) { - case SRV: - mirror = "SRV"; - break; - case HTTP: - mirror = "HTTP"; - break; - case NOMIRROR: - mirror = "NONE"; - break; - default: - mirror = "-unknown-"; - break; - } - switch (pkg_repo_signature_type(repo)) { - case SIG_PUBKEY: - sig = "PUBKEY"; - break; - case SIG_FINGERPRINT: - sig = "FINGERPRINTS"; - break; - case SIG_NONE: - sig = "NONE"; - break; - default: - sig = "-unknown-"; - break; - } - - printf(" %s: { \n %-16s: \"%s\",\n %-16s: %s,\n" - " %-16s: %u", - pkg_repo_name(repo), - "url", pkg_repo_url(repo), - "enabled", pkg_repo_enabled(repo) ? "yes" : "no", - "priority", pkg_repo_priority(repo)); - - if (pkg_repo_mirror_type(repo) != NOMIRROR) - printf(",\n %-16s: \"%s\"", - "mirror_type", mirror); - if (pkg_repo_signature_type(repo) != SIG_NONE) - printf(",\n %-16s: \"%s\"", - "signature_type", sig); - if (pkg_repo_fingerprints(repo) != NULL) - printf(",\n %-16s: \"%s\"", - "fingerprints", pkg_repo_fingerprints(repo)); - if (pkg_repo_key(repo) != NULL) - printf(",\n %-16s: \"%s\"", - "pubkey", pkg_repo_key(repo)); - if (pkg_repo_ip_version(repo) != 0) - printf(",\n %-16s: %u", - "ip_version", pkg_repo_ip_version(repo)); - printf("\n }\n"); - } + while (pkg_repos(&repo) == EPKG_OK) + print_repository(repo, true); } static void diff --git a/src/pkgcli.h b/src/pkgcli.h index 1a8668bf9..c78043724 100644 --- a/src/pkgcli.h +++ b/src/pkgcli.h @@ -1,28 +1,8 @@ /*- - * Copyright (c) 2011-2012 Baptiste Daroussin + * Copyright (c) 2011-2024 Baptiste Daroussin * Copyright (c) 2013 Matthew Seaman - * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer - * in this position and unchanged. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-2-Clause */ #ifndef _PKGCLI_H @@ -115,6 +95,10 @@ int exec_register(int argc, char **argv); int exec_repo(int, char **); void usage_repo(void); +/* pkg repo */ +int exec_repositories(int, char **); +void usage_repositories(void); + /* pkg rquery */ int exec_rquery(int, char **); void usage_rquery(void); @@ -278,6 +262,7 @@ void job_status_end(xstring *); int event_callback(void *data, struct pkg_event *ev); int print_pkg(struct pkg *p, void *ctx); +void print_repository(struct pkg_repo *repo, bool pad); void progressbar_start(const char *pmsg); void progressbar_tick(int64_t current, int64_t total); void progressbar_stop(void); diff --git a/src/repositories.c b/src/repositories.c new file mode 100644 index 000000000..c54b128b6 --- /dev/null +++ b/src/repositories.c @@ -0,0 +1,90 @@ +/*- + * Copyright(c) 2024 Baptiste Daroussin + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +#include + +#include "pkgcli.h" + +void +usage_repositories(void) +{ + fprintf(stderr, "Usage: pkg repositories [-edl] [repository]\n\n"); +} + + +typedef enum { + REPO_SHOW_ALL = 0, + REPO_SHOW_ENABLED = 1U << 0, + REPO_SHOW_DISABLED = 1U << 1, +} repo_show_t; + +int +exec_repositories(int argc, char **argv) +{ + const char *r = NULL; + struct pkg_repo *repo = NULL; + bool list_only = false; + repo_show_t rs = REPO_SHOW_ALL; + int ch; + + struct option longopts[] = { + { "list", no_argument, NULL, 'l' }, + { "enabled", no_argument, NULL, 'e' }, + { "disabled", no_argument, NULL, 'd' }, + { NULL, 0, NULL, 0 }, + }; + + while ((ch = getopt_long(argc, argv, "+led", longopts, NULL)) != -1) { + switch (ch) { + case 'l': + list_only = true; + break; + case 'e': + rs |= REPO_SHOW_ENABLED; + break; + case 'd': + rs |= REPO_SHOW_DISABLED; + break; + default: + usage_repositories(); + return (EXIT_FAILURE); + } + } + + if (rs == REPO_SHOW_ALL) + rs |= REPO_SHOW_DISABLED|REPO_SHOW_ENABLED; + + argc -= optind; + argv += optind; + + if (argc == 1) + r = argv[0]; + + while (pkg_repos(&repo) == EPKG_OK) { + if (r && !STREQ(r, pkg_repo_name(repo))) + continue; + if (pkg_repo_enabled(repo)) { + if ((rs & REPO_SHOW_ENABLED) == 0) + continue; + } else { + if ((rs & REPO_SHOW_DISABLED) == 0) + continue; + } + if (list_only) { + printf("%s\n", pkg_repo_name(repo)); + continue; + } + print_repository(repo, false); + } + + + return (EXIT_SUCCESS); +} diff --git a/src/utils.c b/src/utils.c index 7db18d516..dc0800b8f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -5,26 +5,7 @@ * Copyright (c) 2012-2015 Matthew Seaman * Copyright (c) 2013-2016 Vsevolod Stakhov * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer - * in this position and unchanged. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-2-Clause */ #ifdef HAVE_CONFIG_H @@ -1105,3 +1086,63 @@ print_pkg(struct pkg *p, void *ctx) return 0; } + +void +print_repository(struct pkg_repo *repo, bool pad) +{ + const char *mirror, *sig; + + switch (pkg_repo_mirror_type(repo)) { + case SRV: + mirror = "SRV"; + break; + case HTTP: + mirror = "HTTP"; + break; + case NOMIRROR: + mirror = "NONE"; + break; + default: + mirror = "-unknown-"; + break; + } + switch (pkg_repo_signature_type(repo)) { + case SIG_PUBKEY: + sig = "PUBKEY"; + break; + case SIG_FINGERPRINT: + sig = "FINGERPRINTS"; + break; + case SIG_NONE: + sig = "NONE"; + break; + default: + sig = "-unknown-"; + break; + } + + printf("%s%s: { \n %-16s: \"%s\",\n %-16s: %s,\n" + " %-16s: %u", + pad ? " " : "", + pkg_repo_name(repo), + "url", pkg_repo_url(repo), + "enabled", pkg_repo_enabled(repo) ? "yes" : "no", + "priority", pkg_repo_priority(repo)); + + if (pkg_repo_mirror_type(repo) != NOMIRROR) + printf(",\n %-16s: \"%s\"", + "mirror_type", mirror); + if (pkg_repo_signature_type(repo) != SIG_NONE) + printf(",\n %-16s: \"%s\"", + "signature_type", sig); + if (pkg_repo_fingerprints(repo) != NULL) + printf(",\n %-16s: \"%s\"", + "fingerprints", pkg_repo_fingerprints(repo)); + if (pkg_repo_key(repo) != NULL) + printf(",\n %-16s: \"%s\"", + "pubkey", pkg_repo_key(repo)); + if (pkg_repo_ip_version(repo) != 0) + printf(",\n %-16s: %u", + "ip_version", pkg_repo_ip_version(repo)); + printf("\n }\n"); +}