From: Michael Tremer Date: Sun, 15 Oct 2023 12:39:17 +0000 (+0000) Subject: cli: repo: Upgrade to the new parser X-Git-Tag: 0.9.30~1509 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c5ad245b734a4d2206d16d489ab6b091666b655d;p=pakfire.git cli: repo: Upgrade to the new parser Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/repo.c b/src/cli/lib/repo.c index 6d4fa4a9f..71cb1b839 100644 --- a/src/cli/lib/repo.c +++ b/src/cli/lib/repo.c @@ -18,23 +18,15 @@ # # #############################################################################*/ -#include - #include "command.h" #include "repo.h" #include "repo_compose.h" -int cli_repo(struct pakfire* pakfire, int argc, char* argv[]) { +int cli_repo(void* data, int argc, char* argv[]) { static const struct command commands[] = { -#if 0 - { "compose", 0, cli_repo_compose }, -#endif + { "compose", cli_repo_compose, 1, -1, 0 }, { NULL }, }; - return 0; - -#if 0 - return command_dispatch(pakfire, commands, 1, argc, argv); -#endif + return cli_parse(NULL, commands, NULL, NULL, NULL, argc, argv, data); } diff --git a/src/cli/lib/repo.h b/src/cli/lib/repo.h index 8a1ef94ce..b97cab8c5 100644 --- a/src/cli/lib/repo.h +++ b/src/cli/lib/repo.h @@ -21,8 +21,6 @@ #ifndef PAKFIRE_CLI_REPO_H #define PAKFIRE_CLI_REPO_H -#include - -int cli_repo(struct pakfire* pakfire, int argc, char* argv[]); +int cli_repo(void* data, int argc, char* argv[]); #endif /* PAKFIRE_CLI_REPO_H */ diff --git a/src/cli/lib/repo_compose.c b/src/cli/lib/repo_compose.c index a824c30ba..cccee6666 100644 --- a/src/cli/lib/repo_compose.c +++ b/src/cli/lib/repo_compose.c @@ -18,30 +18,62 @@ # # #############################################################################*/ -#include -#include -#include +#include +#include #include +#include "command.h" #include "repo_compose.h" +#include "pakfire.h" + +static const char* args_doc = "[OPTIONS...] PATH PACKAGES..."; + +static const char* doc = "Create a new repository"; + +#define MAX_PACKAGES 1024 struct config { const char* path; - char** files; - struct pakfire_key* key; + const char* key; + + char* packages[MAX_PACKAGES]; + unsigned int num_packages; +}; + +enum { + OPT_KEY = 1, +}; + +static struct argp_option options[] = { + { "key", OPT_KEY, "KEY", 0, "Use this key to sign the repository", 0 }, + { NULL }, }; -static void help(void) { - printf( - "%s [OPTIONS...] repo compose [OPTIONS...] PACKAGE...\n\n" - "Options:\n" - " --key PATH The key to sign the repository with\n" - " -h --help Show help\n", - program_invocation_short_name - ); +static error_t parse(int key, char* arg, void* data) { + struct config* config = data; + + switch (key) { + case OPT_KEY: + config->key = arg; + break; - exit(0); + case ARGP_KEY_ARG: + if (!config->path) + config->path = arg; + + else if (config->num_packages >= MAX_PACKAGES) + return -ENOBUFS; + + else + config->packages[config->num_packages++] = arg; + break; + + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static int cli_import_key(struct pakfire_key** key, @@ -69,64 +101,43 @@ static int cli_import_key(struct pakfire_key** key, return r; } -static int parse_argv(struct pakfire* pakfire, struct config* config, - int argc, char* argv[]) { - enum { - ARG_KEY, - }; +int cli_repo_compose(void* data, int argc, char* argv[]) { + struct pakfire* pakfire = NULL; + struct pakfire_key* key = NULL; int r; - static const struct option options[] = { - { "key", required_argument, NULL, ARG_KEY }, - { "help", no_argument, NULL, 'h' }, - { NULL }, - }; - int c; + struct cli_config* cli_config = data; - for (;;) { - c = getopt_long(argc, argv, "h", options, NULL); - if (c < 0) - break; - - switch (c) { - case 'h': - help(); - - case ARG_KEY: - r = cli_import_key(&config->key, pakfire, optarg); - if (r) - return r; - break; - - case '?': - return -EINVAL; - - default: - break; - } - } - - return 0; -} - - -int cli_repo_compose(struct pakfire* pakfire, int argc, char* argv[]) { struct config config = { .path = NULL, .key = NULL, }; - int r; - // Parse commandline - r = parse_argv(pakfire, &config, argc, argv); + // Parse the command line + r = cli_parse(options, NULL, args_doc, doc, parse, argc, argv, &config); if (r) - return r; + goto ERROR; - // XXX check if we have enough arguments + // Setup pakfire + r = cli_setup_pakfire(&pakfire, cli_config); + if (r) + goto ERROR; - // Set path - config.path = argv[optind++]; argc--; - config.files = argv + optind; + // Read the key (if any) + if (config.key) { + r = cli_import_key(&key, pakfire, config.key); + if (r) + goto ERROR; + } + + // Write the repository + r = pakfire_repo_compose(pakfire, config.path, key, (const char**)config.packages); - return pakfire_repo_compose(pakfire, config.path, config.key, (const char**)config.files); +ERROR: + if (key) + pakfire_key_unref(key); + if (pakfire) + pakfire_unref(pakfire); + + return r; } diff --git a/src/cli/lib/repo_compose.h b/src/cli/lib/repo_compose.h index ff6b61c41..8e3ae8286 100644 --- a/src/cli/lib/repo_compose.h +++ b/src/cli/lib/repo_compose.h @@ -19,10 +19,8 @@ #############################################################################*/ #ifndef PAKFIRE_CLI_REPO_COMPOSE_H -#define PAKFIRE_CLI_REPO_COMPOSE_H +#define PAKFIRE_CLI_REPO_COM -#include - -int cli_repo_compose(struct pakfire* pakfire, int argc, char* argv[]); +int cli_repo_compose(void* data, int argc, char* argv[]); #endif /* PAKFIRE_CLI_REPO_COMPOSE_H */ diff --git a/src/cli/pakfire-builder.c b/src/cli/pakfire-builder.c index 7ada350f8..28e32bf25 100644 --- a/src/cli/pakfire-builder.c +++ b/src/cli/pakfire-builder.c @@ -70,10 +70,10 @@ static const struct command commands[] = { { "image", cli_image, -1, -1, 0 }, { "info", cli_info, 1, -1, 0 }, { "provides", cli_provides, 1, -1, 0 }, + { "repo", cli_repo, -1, -1, 0 }, { "requires", cli_requires, 1, -1, 0 }, { "search", cli_search, 1, -1, 0 }, #if 0 - { "repo", 0, cli_repo }, { "repolist", 0, cli_repolist }, { "shell", 0, cli_shell }, #endif