]> git.ipfire.org Git - pakfire.git/commitdiff
cli: install: Migrate the command to the new system
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 31 Oct 2023 17:56:18 +0000 (17:56 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 31 Oct 2023 17:56:18 +0000 (17:56 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/install.c
src/cli/lib/install.h
src/cli/pakfire.c

index e00457125536be95be11af56e8627c4f47ca660b..930b3efdb16321815492fcfcc52533f07f4e621d 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <argp.h>
 #include <errno.h>
-#include <getopt.h>
 #include <stdlib.h>
 
 #include <pakfire/pakfire.h>
 #include <pakfire/transaction.h>
 
+#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;
 }
index 1c681988c08d55e660ea525361d51365d4b5087d..18894d8c20548563a230e9987d9466062eb5b21c 100644 (file)
@@ -21,8 +21,6 @@
 #ifndef PAKFIRE_CLI_INSTALL_H
 #define PAKFIRE_CLI_INSTALL_H
 
-#include <pakfire/pakfire.h>
-
-int cli_install(struct pakfire* pakfire, int argc, char* argv[]);
+int cli_install(void* data, int argc, char* argv[]);
 
 #endif /* PAKFIRE_CLI_INSTALL_H */
index c1124cae9f9e9a94e4bd1a769780024fe13b9644..54535efe16dd62e462327245dab8b839c909f654 100644 (file)
@@ -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 },