From: Michael Tremer Date: Tue, 31 Oct 2023 17:56:18 +0000 (+0000) Subject: cli: install: Migrate the command to the new system X-Git-Tag: 0.9.30~1365 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8b881a9b4fdf42a9aec51ed935f8386f6e106a2e;p=pakfire.git cli: install: Migrate the command to the new system Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/install.c b/src/cli/lib/install.c index e00457125..930b3efdb 100644 --- a/src/cli/lib/install.c +++ b/src/cli/lib/install.c @@ -18,84 +18,77 @@ # # #############################################################################*/ +#include #include -#include #include #include #include +#include "command.h" #include "install.h" +#include "pakfire.h" #include "transaction.h" +static const char* args_doc = "install [OPTIONS...] PACKAGES..."; + +static const char* doc = "Install packages"; + +#define MAX_PACKAGES 128 + struct config { int transaction_flags; int job_flags; + + // Packages + char* packages[MAX_PACKAGES]; + unsigned int num_packages; }; -static void help(void) { - printf( - "%s [OPTIONS...] install [OPTIONS...] PACKAGE...\n\n" - "Options:\n" - " --allow-downgrade Allow downgrading packages\n" - " --allow-uninstall Allow uninstalling packages\n" - " --without-recommended Do not install recommended packages\n" - " -h --help Show help\n", - program_invocation_short_name - ); - - exit(0); -} +enum { + OPT_BEST = 1, + OPT_ALLOW_DOWNGRADE = 2, + OPT_ALLOW_UNINSTALL = 3, + OPT_WITHOUT_RECOMMENDED = 4, +}; -static int parse_argv(struct config* config, int argc, char* argv[]) { - enum { - ARG_BEST, - ARG_ALLOW_DOWNGRADE, - ARG_ALLOW_UNINSTALL, - ARG_WITHOUT_RECOMMENDED, - }; - - static const struct option options[] = { - { "help", no_argument, NULL, 'h' }, - { "allow-downgrade", no_argument, NULL, ARG_ALLOW_DOWNGRADE }, - { "allow-uninstall", no_argument, NULL, ARG_ALLOW_UNINSTALL }, - { "best", no_argument, NULL, ARG_BEST }, - { "without-recommended", no_argument, NULL, ARG_WITHOUT_RECOMMENDED }, - { NULL }, - }; - int c; - - for (;;) { - c = getopt_long(argc, argv, "h", 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 }, + { "best", OPT_BEST, NULL, 0, "Only install the best package(s)", 0 }, + { "without-recommended", OPT_ALLOW_DOWNGRADE, NULL, 0, "Do not install recommended packages", 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; + + switch (key) { + case OPT_BEST: + config->job_flags |= PAKFIRE_JOB_BEST; + break; - case ARG_BEST: - config->job_flags |= PAKFIRE_JOB_BEST; - break; + case OPT_ALLOW_DOWNGRADE: + config->transaction_flags |= PAKFIRE_TRANSACTION_ALLOW_DOWNGRADE; + break; - case ARG_ALLOW_DOWNGRADE: - config->transaction_flags |= PAKFIRE_TRANSACTION_ALLOW_DOWNGRADE; - break; + case OPT_ALLOW_UNINSTALL: + config->transaction_flags |= PAKFIRE_TRANSACTION_ALLOW_UNINSTALL; + break; - case ARG_ALLOW_UNINSTALL: - config->transaction_flags |= PAKFIRE_TRANSACTION_ALLOW_UNINSTALL; - break; + case OPT_WITHOUT_RECOMMENDED: + config->transaction_flags |= PAKFIRE_TRANSACTION_WITHOUT_RECOMMENDED; + break; - case ARG_WITHOUT_RECOMMENDED: - config->transaction_flags |= PAKFIRE_TRANSACTION_WITHOUT_RECOMMENDED; - 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; @@ -106,9 +99,9 @@ static int __cli_install(struct pakfire_transaction* transaction, int argc, char 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_INSTALL, argv[i], config->job_flags); + PAKFIRE_JOB_INSTALL, config->packages[i], config->job_flags); if (r) { fprintf(stderr, "Could not find '%s': %m\n", argv[i]); return r; @@ -118,17 +111,28 @@ static int __cli_install(struct pakfire_transaction* transaction, int argc, char return 0; } -int cli_install(struct pakfire* pakfire, int argc, char* argv[]) { - struct config config = { - .transaction_flags = 0, - .job_flags = 0, - }; +int cli_install(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_install, &config); + +ERROR: + if (pakfire) + pakfire_unref(pakfire); - return cli_transaction(pakfire, argc, argv, config.transaction_flags, __cli_install, &config); + return r; } diff --git a/src/cli/lib/install.h b/src/cli/lib/install.h index 1c681988c..18894d8c2 100644 --- a/src/cli/lib/install.h +++ b/src/cli/lib/install.h @@ -21,8 +21,6 @@ #ifndef PAKFIRE_CLI_INSTALL_H #define PAKFIRE_CLI_INSTALL_H -#include - -int cli_install(struct pakfire* pakfire, int argc, char* argv[]); +int cli_install(void* data, int argc, char* argv[]); #endif /* PAKFIRE_CLI_INSTALL_H */ diff --git a/src/cli/pakfire.c b/src/cli/pakfire.c index c1124cae9..54535efe1 100644 --- a/src/cli/pakfire.c +++ b/src/cli/pakfire.c @@ -77,7 +77,7 @@ static struct argp_option options[] = { static const struct command commands[] = { { "clean", cli_clean, 0, 0, 0 }, { "info", cli_info, 1, -1, 0 }, - //{ "install", cli_install, 1, -1, 0 }, + { "install", cli_install, 1, -1, 0 }, { "provides", cli_provides, 1, -1, 0 }, //{ "remove", cli_remove, 1, -1, 0 }, { "repolist", cli_repolist, 0, 0, 0 },