]> git.ipfire.org Git - pakfire.git/commitdiff
cli: info: Migrate command to the new parser
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Oct 2023 11:55:50 +0000 (11:55 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Oct 2023 11:55:50 +0000 (11:55 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/dist.c
src/cli/lib/info.c
src/cli/lib/info.h
src/cli/pakfire-builder.c

index ffb1ff5ccf74994ecb4c2900c2737db6a237274b..e4bf955c0e6b1a544f52e6741b2f996bb0a6bd54 100644 (file)
@@ -85,7 +85,7 @@ int cli_dist(void* data, int argc, char* argv[]) {
 
        // Process all packages
        for (unsigned int i = 0; i < config.num_makefiles; i++) {
-               r = pakfire_dist(pakfire, argv[i], config.resultdir, NULL);
+               r = pakfire_dist(pakfire, config.makefiles[i], config.resultdir, NULL);
                if (r) {
                        fprintf(stderr, "Could not dist %s\n", config.makefiles[i]);
                        goto ERROR;
index 41c183aef875e8c560b2946189cc400bc8a5997b..75316f38585b04fdc7298318d4bf023f140499cf 100644 (file)
 #                                                                             #
 #############################################################################*/
 
-#include <errno.h>
-#include <getopt.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include <argp.h>
 
 #include <pakfire/packagelist.h>
 #include <pakfire/pakfire.h>
 
+#include "command.h"
 #include "dump.h"
 #include "info.h"
+#include "pakfire.h"
 #include "transaction.h"
 
+static const char* args_doc = "info [OPTIONS...] PACKAGES...";
+
+static const char* doc = "Shows package information";
+
+#define MAX_PACKAGES 128
+
 struct config {
        int dump_flags;
+
+       // Packages
+       char* packages[MAX_PACKAGES];
+       unsigned int num_packages;
 };
 
-static void help(void) {
-       printf(
-               "%s [OPTIONS...] info [OPTIONS...]\n\n"
-               "Options:\n"
-               "     --devel                Show more development information\n"
-               "     --filelist             Show the filelist\n"
-               "  -h --help                 Show help\n",
-               program_invocation_short_name
-       );
-
-       exit(0);
-}
+enum {
+       OPT_DEVEL    = 1,
+       OPT_FILELIST = 2,
+};
 
-static int parse_argv(struct config* config, int argc, char* argv[]) {
-       enum {
-               ARG_DEVEL,
-               ARG_FILELIST,
-       };
+static struct argp_option options[] = {
+       { "devel",    OPT_DEVEL,    NULL, 0, "Show development information", 0 },
+       { "filelist", OPT_FILELIST, NULL, 0, "Show the filelist",            0 },
+       { NULL },
+};
 
-       static const struct option options[] = {
-               { "help",     no_argument, NULL, 'h' },
-               { "devel",    no_argument, NULL, ARG_DEVEL },
-               { "filelist", no_argument, NULL, ARG_FILELIST },
-               { NULL },
-       };
-       int c;
+static error_t parse(int key, char* arg, void* data) {
+       struct config* config = data;
 
-       for (;;) {
-               c = getopt_long(argc, argv, "h", options, NULL);
-               if (c < 0)
+       switch (key) {
+               case OPT_DEVEL:
+                       config->dump_flags |= PAKFIRE_PKG_DUMP_LONG;
                        break;
 
-               switch (c) {
-                       case 'h':
-                               help();
-
-                       case ARG_DEVEL:
-                               config->dump_flags |= PAKFIRE_PKG_DUMP_LONG;
-                               break;
+               case OPT_FILELIST:
+                       config->dump_flags |= PAKFIRE_PKG_DUMP_FILELIST;
+                       break;
 
-                       case ARG_FILELIST:
-                               config->dump_flags |= PAKFIRE_PKG_DUMP_FILELIST;
-                               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;
 }
 
-int cli_info(struct pakfire* pakfire, int argc, char* argv[]) {
+int cli_info(void* data, int argc, char* argv[]) {
+       struct pakfire* pakfire = NULL;
        struct pakfire_packagelist* list = NULL;
        struct config config = {
                .dump_flags = 0,
        };
        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;
 
        // Allocate a new packagelist
        r = pakfire_packagelist_create(&list, pakfire);
@@ -107,8 +106,8 @@ int cli_info(struct pakfire* pakfire, int argc, char* argv[]) {
                goto ERROR;
 
        // Perform search
-       for (int i = 0; i < argc; i++) {
-               r = pakfire_search(pakfire, argv[i], PAKFIRE_SEARCH_NAME_ONLY, list);
+       for (unsigned int i = 0; i < config.num_packages; i++) {
+               r = pakfire_search(pakfire, config.packages[i], PAKFIRE_SEARCH_NAME_ONLY, list);
                if (r)
                        goto ERROR;
        }
@@ -119,6 +118,8 @@ int cli_info(struct pakfire* pakfire, int argc, char* argv[]) {
 ERROR:
        if (list)
                pakfire_packagelist_unref(list);
+       if (pakfire)
+               pakfire_unref(pakfire);
 
        return r;
 }
index 87415689ebfa729c2886e168eee23b4aec4b945b..290ba2dc0de4d84c631acb158b6de4405c250f99 100644 (file)
@@ -21,8 +21,6 @@
 #ifndef PAKFIRE_CLI_INFO_H
 #define PAKFIRE_CLI_INFO_H
 
-#include <pakfire/pakfire.h>
-
-int cli_info(struct pakfire* pakfire, int argc, char* argv[]);
+int cli_info(void* data, int argc, char* argv[]);
 
 #endif /* PAKFIRE_CLI_INFO_H */
index 98fae7d2ad958b7d8cb136b40a7d2921f0f3ab33..51277fd1c7b269ebc1d6d6191d79362349c7d2a3 100644 (file)
@@ -68,8 +68,8 @@ static const struct command commands[] = {
        { "clean",    cli_clean,  0,  0, 0 },
        { "dist",     cli_dist,   1, -1, 0 },
        { "image",    cli_image, -1, -1, 0 },
+       { "info",     cli_info,   1, -1, 0 },
 #if 0
-       { "info",     0, cli_info },
        { "provides", 0, cli_provides },
        { "repo",     0, cli_repo },
        { "repolist", 0, cli_repolist },