From: Michael Tremer Date: Tue, 31 Oct 2023 18:29:00 +0000 (+0000) Subject: cli: remove: Migrate command to the new parser X-Git-Tag: 0.9.30~1364 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=38d531d305c64c131d721b8b48e6f5f9090ec9e1;p=pakfire.git cli: remove: Migrate command to the new parser Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/install.c b/src/cli/lib/install.c index 930b3efdb..8f3369567 100644 --- a/src/cli/lib/install.c +++ b/src/cli/lib/install.c @@ -19,11 +19,8 @@ #############################################################################*/ #include -#include -#include #include -#include #include "command.h" #include "install.h" diff --git a/src/cli/lib/remove.c b/src/cli/lib/remove.c index 4b08ca13f..bf4ef056c 100644 --- a/src/cli/lib/remove.c +++ b/src/cli/lib/remove.c @@ -18,63 +18,55 @@ # # #############################################################################*/ -#include -#include -#include +#include #include -#include +#include "command.h" +#include "pakfire.h" #include "remove.h" #include "transaction.h" +static const char* args_doc = "remove [OPTIONS...] PACKAGES..."; + +static const char* doc = "Remove packages"; + +#define MAX_PACKAGES 128 + struct config { int job_flags; + + // Packages + char* packages[MAX_PACKAGES]; + unsigned int num_packages; }; -static void help(void) { - printf( - "%s [OPTIONS...] remove [OPTIONS...] PACKAGE...\n\n" - "Options:\n" - " --keep-dependencies Do not uninstall any dependencies\n" - " -h --help Show help\n", - program_invocation_short_name - ); +enum { + OPT_KEEP_DEPENDENCIES = 1, +}; - exit(0); -} +static struct argp_option options[] = { + { "keep-dependencies", OPT_KEEP_DEPENDENCIES, NULL, 0, "Keep previously installed dependencies", 0 }, + { NULL }, +}; -static int parse_argv(struct config* config, int argc, char* argv[]) { - enum { - ARG_KEEP_DEPS, - }; - - static const struct option options[] = { - { "help", no_argument, NULL, 'h' }, - { "keep-dependencies", no_argument, NULL, ARG_KEEP_DEPS }, - { NULL }, - }; - int c; - - for (;;) { - c = getopt_long(argc, argv, "h", options, NULL); - if (c < 0) - break; +static error_t parse(int key, char* arg, struct argp_state* state, void* data) { + struct config* config = data; - switch (c) { - case 'h': - help(); + switch (key) { + case OPT_KEEP_DEPENDENCIES: + config->job_flags |= PAKFIRE_JOB_KEEP_DEPS; + break; - case ARG_KEEP_DEPS: - config->job_flags |= PAKFIRE_JOB_KEEP_DEPS; - 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; @@ -84,10 +76,9 @@ static int __cli_remove(struct pakfire_transaction* transaction, int argc, char* struct config* config = (struct config*)data; int r; - // Add the remaining command line options as packages - for (int i = 0; i < argc; i++) { + for (unsigned int i = 0; i < config->num_packages; i++) { r = pakfire_transaction_request(transaction, - PAKFIRE_JOB_ERASE, argv[i], config->job_flags); + PAKFIRE_JOB_ERASE, config->packages[i], config->job_flags); if (r) { fprintf(stderr, "Could not find '%s': %m\n", argv[i]); return r; @@ -97,16 +88,28 @@ static int __cli_remove(struct pakfire_transaction* transaction, int argc, char* return 0; } -int cli_remove(struct pakfire* pakfire, int argc, char* argv[]) { - struct config config = { - .job_flags = 0, - }; +int cli_remove(void* data, int argc, char* argv[]) { + struct pakfire* pakfire = NULL; + struct config config = {}; 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; + + r = cli_transaction(pakfire, argc, argv, 0, __cli_remove, &config); + +ERROR: + if (pakfire) + pakfire_unref(pakfire); - return cli_transaction(pakfire, argc, argv, 0, __cli_remove, &config); + return r; } diff --git a/src/cli/lib/remove.h b/src/cli/lib/remove.h index f9ed7be6c..1e0674a85 100644 --- a/src/cli/lib/remove.h +++ b/src/cli/lib/remove.h @@ -21,8 +21,6 @@ #ifndef PAKFIRE_CLI_REMOVE_H #define PAKFIRE_CLI_REMOVE_H -#include - -int cli_remove(struct pakfire* pakfire, int argc, char* argv[]); +int cli_remove(void* data, int argc, char* argv[]); #endif /* PAKFIRE_CLI_REMOVE_H */ diff --git a/src/cli/pakfire.c b/src/cli/pakfire.c index 54535efe1..9816b0b11 100644 --- a/src/cli/pakfire.c +++ b/src/cli/pakfire.c @@ -79,7 +79,7 @@ static const struct command commands[] = { { "info", cli_info, 1, -1, 0 }, { "install", cli_install, 1, -1, 0 }, { "provides", cli_provides, 1, -1, 0 }, - //{ "remove", cli_remove, 1, -1, 0 }, + { "remove", cli_remove, 1, -1, 0 }, { "repolist", cli_repolist, 0, 0, 0 }, { "requires", cli_requires, 1, -1, 0 }, { "search", cli_search, 1, -1, 0 },