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

index e4ade4cfde9b0ef975b695d250d1310b99afdea1..ffb1ff5ccf74994ecb4c2900c2737db6a237274b 100644 (file)
 #                                                                             #
 #############################################################################*/
 
-#include <errno.h>
-#include <getopt.h>
-#include <limits.h>
-#include <stdlib.h>
+#include <argp.h>
 
 #include "command.h"
 #include "dist.h"
+#include "pakfire.h"
 
 #include <pakfire/dist.h>
 #include <pakfire/pakfire.h>
 
-#define MAX_MAKEFILES 128
+static const char* args_doc = "dist MAKEFILES...";
+
+static const char* doc = "Creates source packages from makefiles";
+
+#define MAX_MAKEFILES 32
 
 struct config {
        const char* resultdir;
-};
 
-static void help(void) {
-       printf(
-               "%s [OPTIONS...] dist MAKEFILES...\n\n"
-               "Options:\n"
-               "  -h --help                 Show help\n",
-               program_invocation_short_name
-       );
+       // Makefiles
+       char* makefiles[MAX_MAKEFILES];
+       unsigned int num_makefiles;
+};
 
-       exit(0);
-}
+static error_t parse(int key, char* arg, void* data) {
+       struct config* config = data;
 
-static int parse_argv(int argc, char* argv[]) {
-       static const struct option options[] = {
-               { "help", no_argument, NULL, 'h' },
-               { NULL },
-       };
-       int c;
+       switch (key) {
+               case ARGP_KEY_ARG:
+                       if (config->num_makefiles >= MAX_MAKEFILES)
+                               return -ENOBUFS;
 
-       for (;;) {
-               c = getopt_long(argc, argv, "h", options, NULL);
-               if (c < 0)
+                       config->makefiles[config->num_makefiles++] = arg;
                        break;
 
-               switch (c) {
-                       case 'h':
-                               help();
-
-                       case '?':
-                               return -EINVAL;
-
-                       default:
-                               break;
-               }
+               default:
+                       return ARGP_ERR_UNKNOWN;
        }
 
        return 0;
 }
 
-int cli_dist(struct pakfire* pakfire, int argc, char* argv[]) {
+int cli_dist(void* data, int argc, char* argv[]) {
+       struct pakfire* pakfire = NULL;
        struct config config = {
                .resultdir = NULL,
        };
        char cwd[PATH_MAX];
        int r;
 
-       // Parse commandline
-       r = parse_argv(argc, argv);
+       struct cli_config* cli_config = data;
+
+       // Parse the command line
+       r = cli_parse(NULL, NULL, args_doc, doc, parse, argc, argv, &config);
        if (r)
-               return r;
+               goto ERROR;
 
        // Set result directory to PWD
        if (!config.resultdir)
                config.resultdir = getcwd(cwd, sizeof(cwd));
 
-       // Run for all arguments
-       for (int i = 0; i < argc; i++) {
+       // Setup pakfire
+       r = cli_setup_pakfire(&pakfire, cli_config);
+       if (r)
+               goto ERROR;
+
+       // Process all packages
+       for (unsigned int i = 0; i < config.num_makefiles; i++) {
                r = pakfire_dist(pakfire, argv[i], config.resultdir, NULL);
-               if (r)
-                       break;
+               if (r) {
+                       fprintf(stderr, "Could not dist %s\n", config.makefiles[i]);
+                       goto ERROR;
+               }
        }
 
+ERROR:
+       if (pakfire)
+               pakfire_unref(pakfire);
+
        return r;
 }
index 97f5583cbc4e95038df854e1af4a4d4fe06b7d01..ef940137f783a73cb14bc03e7d7cceacbe8aedde 100644 (file)
@@ -21,8 +21,6 @@
 #ifndef PAKFIRE_CLI_DIST_H
 #define PAKFIRE_CLI_DIST_H
 
-#include <pakfire/pakfire.h>
-
-int cli_dist(struct pakfire* pakfire, int argc, char* argv[]);
+int cli_dist(void* data, int argc, char* argv[]);
 
 #endif /* PAKFIRE_CLI_DIST_H */
index 9c9350f8955b80d4ee313b277a5c6305887d8f30..11dbecf10cf70f5326797548a82710913c309a4a 100644 (file)
@@ -66,8 +66,8 @@ static struct argp_option options[] = {
 static const struct command commands[] = {
        { "build",    cli_build, 1, -1, 0 },
        { "clean",    cli_clean, 0,  0, 0 },
+       { "dist",     cli_dist,  1, -1, 0 },
 #if 0
-       { "dist",     0, cli_dist },
        { "image",    0, cli_image },
        { "info",     0, cli_info },
        { "provides", 0, cli_provides },