From: Michael Tremer Date: Sun, 15 Oct 2023 11:55:50 +0000 (+0000) Subject: cli: info: Migrate command to the new parser X-Git-Tag: 0.9.30~1513 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fb15efc39074d2ea52ed7b67d47b40981fb3a1b3;p=pakfire.git cli: info: Migrate command to the new parser Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/dist.c b/src/cli/lib/dist.c index ffb1ff5cc..e4bf955c0 100644 --- a/src/cli/lib/dist.c +++ b/src/cli/lib/dist.c @@ -85,7 +85,7 @@ int cli_dist(void* data, int argc, char* argv[]) { // Process all packages for (unsigned int i = 0; i < config.num_makefiles; i++) { - r = pakfire_dist(pakfire, argv[i], config.resultdir, NULL); + r = pakfire_dist(pakfire, config.makefiles[i], config.resultdir, NULL); if (r) { fprintf(stderr, "Could not dist %s\n", config.makefiles[i]); goto ERROR; diff --git a/src/cli/lib/info.c b/src/cli/lib/info.c index 41c183aef..75316f385 100644 --- a/src/cli/lib/info.c +++ b/src/cli/lib/info.c @@ -18,88 +18,87 @@ # # #############################################################################*/ -#include -#include -#include -#include +#include #include #include +#include "command.h" #include "dump.h" #include "info.h" +#include "pakfire.h" #include "transaction.h" +static const char* args_doc = "info [OPTIONS...] PACKAGES..."; + +static const char* doc = "Shows package information"; + +#define MAX_PACKAGES 128 + struct config { int dump_flags; + + // Packages + char* packages[MAX_PACKAGES]; + unsigned int num_packages; }; -static void help(void) { - printf( - "%s [OPTIONS...] info [OPTIONS...]\n\n" - "Options:\n" - " --devel Show more development information\n" - " --filelist Show the filelist\n" - " -h --help Show help\n", - program_invocation_short_name - ); - - exit(0); -} +enum { + OPT_DEVEL = 1, + OPT_FILELIST = 2, +}; -static int parse_argv(struct config* config, int argc, char* argv[]) { - enum { - ARG_DEVEL, - ARG_FILELIST, - }; +static struct argp_option options[] = { + { "devel", OPT_DEVEL, NULL, 0, "Show development information", 0 }, + { "filelist", OPT_FILELIST, NULL, 0, "Show the filelist", 0 }, + { NULL }, +}; - static const struct option options[] = { - { "help", no_argument, NULL, 'h' }, - { "devel", no_argument, NULL, ARG_DEVEL }, - { "filelist", no_argument, NULL, ARG_FILELIST }, - { NULL }, - }; - int c; +static error_t parse(int key, char* arg, void* data) { + struct config* config = data; - for (;;) { - c = getopt_long(argc, argv, "h", options, NULL); - if (c < 0) + switch (key) { + case OPT_DEVEL: + config->dump_flags |= PAKFIRE_PKG_DUMP_LONG; break; - switch (c) { - case 'h': - help(); - - case ARG_DEVEL: - config->dump_flags |= PAKFIRE_PKG_DUMP_LONG; - break; + case OPT_FILELIST: + config->dump_flags |= PAKFIRE_PKG_DUMP_FILELIST; + break; - case ARG_FILELIST: - config->dump_flags |= PAKFIRE_PKG_DUMP_FILELIST; - break; + case ARGP_KEY_ARG: + if (config->num_packages >= MAX_PACKAGES) + return -ENOBUFS; - case '?': - return -EINVAL; + config->packages[config->num_packages++] = arg; + break; - default: - break; - } + default: + return ARGP_ERR_UNKNOWN; } return 0; } -int cli_info(struct pakfire* pakfire, int argc, char* argv[]) { +int cli_info(void* data, int argc, char* argv[]) { + struct pakfire* pakfire = NULL; struct pakfire_packagelist* list = NULL; struct config config = { .dump_flags = 0, }; int r; - // Parse CLI options - r = parse_argv(&config, argc, argv); + struct cli_config* cli_config = data; + + // Parse the command line + r = cli_parse(options, NULL, args_doc, doc, parse, argc, argv, &config); if (r) - return r; + goto ERROR; + + // Setup Pakfire + r = cli_setup_pakfire(&pakfire, cli_config); + if (r) + goto ERROR; // Allocate a new packagelist r = pakfire_packagelist_create(&list, pakfire); @@ -107,8 +106,8 @@ int cli_info(struct pakfire* pakfire, int argc, char* argv[]) { goto ERROR; // Perform search - for (int i = 0; i < argc; i++) { - r = pakfire_search(pakfire, argv[i], PAKFIRE_SEARCH_NAME_ONLY, list); + for (unsigned int i = 0; i < config.num_packages; i++) { + r = pakfire_search(pakfire, config.packages[i], PAKFIRE_SEARCH_NAME_ONLY, list); if (r) goto ERROR; } @@ -119,6 +118,8 @@ int cli_info(struct pakfire* pakfire, int argc, char* argv[]) { ERROR: if (list) pakfire_packagelist_unref(list); + if (pakfire) + pakfire_unref(pakfire); return r; } diff --git a/src/cli/lib/info.h b/src/cli/lib/info.h index 87415689e..290ba2dc0 100644 --- a/src/cli/lib/info.h +++ b/src/cli/lib/info.h @@ -21,8 +21,6 @@ #ifndef PAKFIRE_CLI_INFO_H #define PAKFIRE_CLI_INFO_H -#include - -int cli_info(struct pakfire* pakfire, int argc, char* argv[]); +int cli_info(void* data, int argc, char* argv[]); #endif /* PAKFIRE_CLI_INFO_H */ diff --git a/src/cli/pakfire-builder.c b/src/cli/pakfire-builder.c index 98fae7d2a..51277fd1c 100644 --- a/src/cli/pakfire-builder.c +++ b/src/cli/pakfire-builder.c @@ -68,8 +68,8 @@ static const struct command commands[] = { { "clean", cli_clean, 0, 0, 0 }, { "dist", cli_dist, 1, -1, 0 }, { "image", cli_image, -1, -1, 0 }, + { "info", cli_info, 1, -1, 0 }, #if 0 - { "info", 0, cli_info }, { "provides", 0, cli_provides }, { "repo", 0, cli_repo }, { "repolist", 0, cli_repolist },