From: Michael Tremer Date: Fri, 20 Oct 2023 13:58:23 +0000 (+0000) Subject: buildservice: Implement creating repositories X-Git-Tag: 0.9.30~1407 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=110ab7da257409da88fc7208da3489fc4c070c6b;p=pakfire.git buildservice: Implement creating repositories Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index bc9dedf0e..172024648 100644 --- a/Makefile.am +++ b/Makefile.am @@ -497,6 +497,8 @@ libcli_la_SOURCES = \ src/cli/lib/repo.h \ src/cli/lib/repo_compose.c \ src/cli/lib/repo_compose.h \ + src/cli/lib/repo_create.c \ + src/cli/lib/repo_create.h \ src/cli/lib/repo_list.c \ src/cli/lib/repo_list.h \ src/cli/lib/repo_show.c \ diff --git a/src/cli/lib/repo.c b/src/cli/lib/repo.c index 0cbd111f1..3c38ae4ab 100644 --- a/src/cli/lib/repo.c +++ b/src/cli/lib/repo.c @@ -21,6 +21,7 @@ #include "command.h" #include "repo.h" #include "repo_compose.h" +#include "repo_create.h" #include "repo_list.h" #include "repo_show.h" @@ -35,8 +36,9 @@ int cli_repo(void* data, int argc, char* argv[]) { int cli_repo_client(void* data, int argc, char* argv[]) { static const struct command commands[] = { - { "list", cli_repo_list, 1, 1, 0 }, - { "show", cli_repo_show, 2, 2, 0 }, + { "create", cli_repo_create, 2, -1, 0 }, + { "list", cli_repo_list, 1, 1, 0 }, + { "show", cli_repo_show, 2, 2, 0 }, { NULL }, }; diff --git a/src/cli/lib/repo_create.c b/src/cli/lib/repo_create.c new file mode 100644 index 000000000..e09b96d52 --- /dev/null +++ b/src/cli/lib/repo_create.c @@ -0,0 +1,112 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2023 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program 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 General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include + +#include + +#include + +#include "command.h" +#include "dump.h" +#include "pakfire.h" +#include "repo_create.h" + +static const char* args_doc = "DISTRO NAME [OPTIONS...]"; + +static const char* doc = "Create a new repository"; + +struct config { + const char* distro; + const char* name; + const char* description; +}; + +enum { + OPT_DESCRIPTION = 1, +}; +static struct argp_option options[] = { + { "description", OPT_DESCRIPTION, "DESCRIPTION", 0, "A description for the repository", 0 }, + { NULL }, +}; + +static error_t parse(int key, char* arg, struct argp_state* state, void* data) { + struct config* config = data; + + switch (key) { + case OPT_DESCRIPTION: + config->description = arg; + break; + + case ARGP_KEY_ARG: + if (!config->distro) + config->distro = arg; + + else if (!config->name) + config->name = arg; + + else + argp_usage(state); + break; + + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; +} + +int cli_repo_create(void* data, int argc, char* argv[]) { + struct pakfire_buildservice* service = NULL; + struct json_object* repo = NULL; + struct config config = {}; + int r; + + struct cli_config* cli_config = data; + + // Parse the command line + r = cli_parse(options, NULL, args_doc, doc, parse, argc, argv, &config); + if (r) + goto ERROR; + + // Connect to the build service + r = pakfire_buildservice_create(&service, cli_config->ctx); + if (r) + goto ERROR; + + // List repos + r = pakfire_buildservice_create_repo(service, + config.distro, config.name, config.description, &repo); + if (r) + goto ERROR; + + // Dump everything + r = cli_dump_json(repo); + if (r) + goto ERROR; + +ERROR: + if (service) + pakfire_buildservice_unref(service); + if (repo) + json_object_put(repo); + + return r; +} diff --git a/src/cli/lib/repo_create.h b/src/cli/lib/repo_create.h new file mode 100644 index 000000000..c5459e3a4 --- /dev/null +++ b/src/cli/lib/repo_create.h @@ -0,0 +1,26 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2023 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program 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 General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef PAKFIRE_CLI_REPO_CREATE_H +#define PAKFIRE_CLI_REPO_CREATE_H + +int cli_repo_create(void* data, int argc, char* argv[]); + +#endif /* PAKFIRE_CLI_REPO_CREATE_H */ diff --git a/src/libpakfire/buildservice.c b/src/libpakfire/buildservice.c index bcaa0b063..b8a4b7000 100644 --- a/src/libpakfire/buildservice.c +++ b/src/libpakfire/buildservice.c @@ -838,3 +838,75 @@ ERROR: return r; } + +PAKFIRE_EXPORT int pakfire_buildservice_create_repo(struct pakfire_buildservice* service, + const char* distro, const char* name, const char* description, struct json_object** p) { + struct pakfire_transfer* transfer = NULL; + struct json_object* response = NULL; + char url[PATH_MAX]; + char* buffer = NULL; + size_t length = 0; + int r; + + // Check inputs + if (!distro) + return -EINVAL; + + // Compose path + r = pakfire_string_format(url, "/api/v1/repos/%s", distro); + if (r) + goto ERROR; + + // Create a new transfer + r = pakfire_buildservice_create_transfer(&transfer, service, url); + if (r) + goto ERROR; + + // Enable authentication + r = pakfire_downloader_transfer_auth(transfer); + if (r) + goto ERROR; + + // Set name + r = pakfire_downloader_transfer_add_param(transfer, "name", "%s", name); + if (r) + goto ERROR; + + // Set description + if (description) { + r = pakfire_downloader_transfer_add_param(transfer, "description", "%s", description); + if (r) + goto ERROR; + } + + // Write the response to the buffer + r = pakfire_downloader_transfer_set_output_buffer(transfer, &buffer, &length); + if (r) + goto ERROR; + + // Run the transfer + r = pakfire_downloader_transfer_run(transfer, PAKFIRE_TRANSFER_NO_PROGRESS); + if (r) + goto ERROR; + + // Parse the response + r = pakfire_buildservice_parse_response(service, transfer, buffer, length, &response); + if (r) { + CTX_ERROR(service->ctx, "Could not parse the response: %s\n", strerror(-r)); + goto ERROR; + } + + // Return the pointer + if (p) + *p = json_object_get(response); + +ERROR: + if (transfer) + pakfire_downloader_transfer_unref(transfer); + if (response) + json_object_put(response); + if (buffer) + free(buffer); + + return r; +} diff --git a/src/libpakfire/include/pakfire/buildservice.h b/src/libpakfire/include/pakfire/buildservice.h index 68f3069eb..245de5323 100644 --- a/src/libpakfire/include/pakfire/buildservice.h +++ b/src/libpakfire/include/pakfire/buildservice.h @@ -56,5 +56,7 @@ int pakfire_buildservice_list_repos(struct pakfire_buildservice* service, const char* distro, struct json_object** repos); int pakfire_buildservice_get_repo(struct pakfire_buildservice* service, const char* distro, const char* name, struct json_object** repo); +int pakfire_buildservice_create_repo(struct pakfire_buildservice* service, + const char* distro, const char* name, const char* description, struct json_object** repo); #endif /* PAKFIRE_BUILDSERVICE_H */