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

index 930b3efdb16321815492fcfcc52533f07f4e621d..8f33695670d01cd5f221918fe6b2662965481c3f 100644 (file)
 #############################################################################*/
 
 #include <argp.h>
-#include <errno.h>
-#include <stdlib.h>
 
 #include <pakfire/pakfire.h>
-#include <pakfire/transaction.h>
 
 #include "command.h"
 #include "install.h"
index 4b08ca13ff44bea6f5f40235414406e49bea5b77..bf4ef056c34d6bacda6d4f221fd040cd8b39417b 100644 (file)
 #                                                                             #
 #############################################################################*/
 
-#include <errno.h>
-#include <getopt.h>
-#include <stdlib.h>
+#include <argp.h>
 
 #include <pakfire/pakfire.h>
-#include <pakfire/transaction.h>
 
+#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;
 }
index f9ed7be6cc1ce5123638e0ecad1eb3749325f601..1e0674a850e8fe7a2b32b9216abdb07618c0d834 100644 (file)
@@ -21,8 +21,6 @@
 #ifndef PAKFIRE_CLI_REMOVE_H
 #define PAKFIRE_CLI_REMOVE_H
 
-#include <pakfire/pakfire.h>
-
-int cli_remove(struct pakfire* pakfire, int argc, char* argv[]);
+int cli_remove(void* data, int argc, char* argv[]);
 
 #endif /* PAKFIRE_CLI_REMOVE_H */
index 54535efe16dd62e462327245dab8b839c909f654..9816b0b113954d1270c435c2df7665fa904e07f4 100644 (file)
@@ -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 },