From: Michael Tremer Date: Tue, 31 Oct 2023 18:49:34 +0000 (+0000) Subject: cli: update: Migrate to the new parser X-Git-Tag: 0.9.30~1362 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bbbc51c66759bff899e5c9ffb1118a9d611b4f5c;p=pakfire.git cli: update: Migrate to the new parser Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/update.c b/src/cli/lib/update.c index b4a8dbb85..9f374c920 100644 --- a/src/cli/lib/update.c +++ b/src/cli/lib/update.c @@ -18,85 +18,74 @@ # # #############################################################################*/ -#include -#include -#include -#include +#include #include -#include -#include "update.h" +#include "command.h" +#include "pakfire.h" #include "transaction.h" +#include "update.h" + +static const char* args_doc = "update [OPTIONS...] PACKAGES..."; -#define MAX_EXCLUDE 64 +static const char* doc = "Update packages"; + +#define MAX_EXCLUDES 128 +#define MAX_PACKAGES 128 struct config { int transaction_flags; - char* exclude[MAX_EXCLUDE]; - unsigned int nexclude; + + // Excludes + char* excludes[MAX_EXCLUDES]; + unsigned int num_excludes; + + // Packages + char* packages[MAX_PACKAGES]; + unsigned int num_packages; }; -static void help(void) { - printf( - "%s [OPTIONS...] update [OPTIONS...]\n\n" - "Options:\n" - " --allow-downgrade Allows downgrading packages\n" - " --allow-uninstall Allows uninstalling packages\n" - " -x --exclude Excludes a package from being updated\n" - " -h --help Show help\n", - program_invocation_short_name - ); - - exit(0); -} +enum { + OPT_ALLOW_DOWNGRADE = 1, + OPT_ALLOW_UNINSTALL = 2, +}; -static int parse_argv(struct config* config, int argc, char* argv[]) { - enum { - ARG_ALLOW_DOWNGRADE, - ARG_ALLOW_UNINSTALL, - }; - - static const struct option options[] = { - { "allow-downgrade", no_argument, NULL, ARG_ALLOW_DOWNGRADE }, - { "allow-uninstall", no_argument, NULL, ARG_ALLOW_UNINSTALL }, - { "help", no_argument, NULL, 'h' }, - { "exclude", required_argument, NULL, 'x' }, - { NULL }, - }; - int c; - - for (;;) { - c = getopt_long(argc, argv, "hx", options, NULL); - if (c < 0) - break; +static struct argp_option options[] = { + { "allow-downgrade", OPT_ALLOW_DOWNGRADE, NULL, 0, "Allow downgrading packages", 0 }, + { "allow-uninstall", OPT_ALLOW_UNINSTALL, NULL, 0, "Allow uninstalling packages", 0 }, + { "exclude", 'x', "PACKAGE", 0, "Exclude a package from being updated", 0 }, + { NULL }, +}; - switch (c) { - case 'h': - help(); +static error_t parse(int key, char* arg, struct argp_state* state, void* data) { + struct config* config = data; - case ARG_ALLOW_DOWNGRADE: - config->transaction_flags |= PAKFIRE_TRANSACTION_ALLOW_DOWNGRADE; - break; + switch (key) { + case OPT_ALLOW_DOWNGRADE: + config->transaction_flags |= PAKFIRE_TRANSACTION_ALLOW_DOWNGRADE; + break; - case ARG_ALLOW_UNINSTALL: - config->transaction_flags |= PAKFIRE_TRANSACTION_ALLOW_UNINSTALL; - break; + case OPT_ALLOW_UNINSTALL: + config->transaction_flags |= PAKFIRE_TRANSACTION_ALLOW_UNINSTALL; + break; - case 'x': - // Check if there is space - if (config->nexclude >= MAX_EXCLUDE) - return -ENOBUFS; + case 'x': + if (config->num_excludes >= MAX_EXCLUDES) + return -ENOBUFS; - config->exclude[config->nexclude++] = optarg; - break; + config->excludes[config->num_excludes++] = arg; + break; - case '?': - return -EINVAL; + case ARGP_KEY_ARG: + if (config->num_packages >= MAX_PACKAGES) + return -ENOBUFS; - default: - break; - } + config->packages[config->num_packages++] = arg; + break; + + default: + return ARGP_ERR_UNKNOWN; } return 0; @@ -107,8 +96,8 @@ static int __cli_update(struct pakfire_transaction* transaction, int argc, char* int r; // Exclude any packages - for (unsigned int i = 0; i < config->nexclude; i++) { - r = pakfire_transaction_request(transaction, PAKFIRE_JOB_LOCK, config->exclude[i], 0); + for (unsigned int i = 0; i < config->num_excludes; i++) { + r = pakfire_transaction_request(transaction, PAKFIRE_JOB_LOCK, config->excludes[i], 0); if (r) return r; } @@ -116,8 +105,9 @@ static int __cli_update(struct pakfire_transaction* transaction, int argc, char* // Did the user pass any packages? if (argc) { // Add the remaining command line options as packages - for (int i = 0; i < argc; i++) { - r = pakfire_transaction_request(transaction, PAKFIRE_JOB_UPDATE, argv[i], 0); + for (unsigned int i = 0; i < config->num_packages; i++) { + r = pakfire_transaction_request(transaction, + PAKFIRE_JOB_UPDATE, config->packages[i], 0); if (r) { fprintf(stderr, "Could not find '%s': %m\n", argv[i]); return r; @@ -134,17 +124,28 @@ static int __cli_update(struct pakfire_transaction* transaction, int argc, char* return 0; } -int cli_update(struct pakfire* pakfire, int argc, char* argv[]) { - struct config config = { - .transaction_flags = 0, - .nexclude = 0, - }; +int cli_update(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) + goto ERROR; + + // Setup Pakfire + r = cli_setup_pakfire(&pakfire, cli_config); if (r) - return r; + goto ERROR; + + r = cli_transaction(pakfire, argc, argv, config.transaction_flags, __cli_update, &config); + +ERROR: + if (pakfire) + pakfire_unref(pakfire); - return cli_transaction(pakfire, argc, argv, config.transaction_flags, __cli_update, &config); + return r; } diff --git a/src/cli/lib/update.h b/src/cli/lib/update.h index 4fc42ccce..7a8b8f356 100644 --- a/src/cli/lib/update.h +++ b/src/cli/lib/update.h @@ -21,8 +21,6 @@ #ifndef PAKFIRE_CLI_UPDATE_H #define PAKFIRE_CLI_UPDATE_H -#include - -int cli_update(struct pakfire* pakfire, int argc, char* argv[]); +int cli_update(void* data, int argc, char* argv[]); #endif /* PAKFIRE_CLI_UPDATE_H */ diff --git a/src/cli/pakfire.c b/src/cli/pakfire.c index 03ffb5ca6..b60cd25d9 100644 --- a/src/cli/pakfire.c +++ b/src/cli/pakfire.c @@ -84,7 +84,7 @@ static const struct command commands[] = { { "requires", cli_requires, 1, -1, 0 }, { "search", cli_search, 1, -1, 0 }, { "sync", cli_sync, 0, 0, 0 }, - //{ "update", cli_update, 0, -1, 0 }, + { "update", cli_update, 0, -1, 0 }, { NULL }, };