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

index 1d60e20f35c980982d58e093617190a678e4cb2a..60678bd5f247cb2b1d4a2c6d01904adf4b4abf8e 100644 (file)
 #                                                                             #
 #############################################################################*/
 
-#include <pakfire/pakfire.h>
-
 #include "command.h"
 #include "image.h"
 #include "image_create.h"
 
-int cli_image(struct pakfire* pakfire, int argc, char* argv[]) {
+int cli_image(void* data, int argc, char* argv[]) {
        static const struct command commands[] = {
-#if 0
-               { "create",  0, cli_image_create },
-#endif
+               { "create", cli_image_create, 2, 2, 0 },
                { NULL },
        };
 
-       return 0;
-
-#if 0
-       return command_dispatch(pakfire, commands, 1, argc, argv);
-#endif
+       return cli_parse(NULL, commands, NULL, NULL, NULL, argc, argv, data);
 }
index 7bd5fb5db1c765b08a93e27dd970b89e2437512d..4e9495495b413ff4c96c7ce6b56bf84f7782740a 100644 (file)
@@ -21,8 +21,6 @@
 #ifndef PAKFIRE_CLI_IMAGE_H
 #define PAKFIRE_CLI_IMAGE_H
 
-#include <pakfire/pakfire.h>
-
-int cli_image(struct pakfire* pakfire, int argc, char* argv[]);
+int cli_image(void* data, int argc, char* argv[]);
 
 #endif /* PAKFIRE_CLI_IMAGE_H */
index 205255bdd30ca78f758c831864859e228b8d94e9..595848b18b4d9a035f9367178ed276ce67b5f909 100644 (file)
 #                                                                             #
 #############################################################################*/
 
-#include <errno.h>
-#include <getopt.h>
-#include <stdlib.h>
+#include <argp.h>
 
 #include <pakfire/build.h>
 #include <pakfire/pakfire.h>
 
+#include "command.h"
 #include "image_create.h"
+#include "pakfire.h"
 
-static void help(void) {
-       printf(
-               "%s [OPTIONS...] image create TYPE PATH...\n\n"
-               "Options:\n"
-               "  -h --help                 Show help\n",
-               program_invocation_short_name
-       );
+struct config {
+       const char* type;
+       const char* path;
+};
 
-       exit(0);
-}
+static const char* args_doc = "image create TYPE PATH";
 
-static int parse_argv(struct pakfire* pakfire, int argc, char* argv[]) {
-       static const struct option options[] = {
-               { "help", no_argument,       NULL, 'h' },
-               { NULL },
-       };
-       int c;
+static const char* doc = "Creates images";
 
-       for (;;) {
-               c = getopt_long(argc, argv, "h", options, NULL);
-               if (c < 0)
-                       break;
+static error_t parse(int key, char* arg, void* data) {
+       struct config* config = data;
 
-               switch (c) {
-                       case 'h':
-                               help();
+       switch (key) {
+               case ARGP_KEY_ARG:
+                       if (!config->type)
+                               config->type = arg;
 
-                       case '?':
-                               return -EINVAL;
+                       else if (!config->path)
+                               config->path = arg;
 
-                       default:
-                               break;
-               }
+                       break;
+
+               default:
+                       return ARGP_ERR_UNKNOWN;
        }
 
        return 0;
 }
 
-int cli_image_create(struct pakfire* pakfire, int argc, char* argv[]) {
+int cli_image_create(void* data, int argc, char* argv[]) {
+       struct pakfire* pakfire = NULL;
        struct pakfire_build* build = NULL;
        FILE* f = NULL;
        int r;
 
-       // Parse commandline
-       r = parse_argv(pakfire, argc, argv);
-       if (r)
-               return r;
+       struct cli_config* cli_config = data;
 
-       // XXX check arguments
+       struct config config = {
+               .type = NULL,
+               .path = NULL,
+       };
 
-       f = fopen(argv[2], "w");
+       // Parse the command line
+       r = cli_parse(NULL, NULL, args_doc, doc, parse, argc, argv, &config);
+       if (r)
+               goto ERROR;
+
+       f = fopen(config.path, "w");
        if (!f) {
-               fprintf(stderr, "Could not open %s: %m\n", argv[2]);
+               fprintf(stderr, "Could not open %s: %m\n", config.path);
                r = -errno;
                goto ERROR;
        }
 
+       // Setup pakfire
+       r = cli_setup_pakfire(&pakfire, cli_config);
+       if (r)
+               goto ERROR;
+
        // Setup the build environment
        r = pakfire_build_create(&build, pakfire, NULL, 0);
        if (r)
                goto ERROR;
 
        // Create the image
-       r = pakfire_build_mkimage(build, argv[1], f);
+       r = pakfire_build_mkimage(build, config.type, f);
        if (r)
                goto ERROR;
 
 ERROR:
        if (build)
                pakfire_build_unref(build);
+       if (pakfire)
+               pakfire_unref(pakfire);
        if (f)
                fclose(f);
 
index ec139f4bab409ca8d99a32ef3d3ee602ae5dc53d..a4f4b1bd4369295e403dabaca538af27123d1fdc 100644 (file)
@@ -21,8 +21,6 @@
 #ifndef PAKFIRE_CLI_IMAGE_CREATE_H
 #define PAKFIRE_CLI_IMAGE_CREATE_H
 
-#include <pakfire/pakfire.h>
-
-int cli_image_create(struct pakfire* pakfire, int argc, char* argv[]);
+int cli_image_create(void* data, int argc, char* argv[]);
 
 #endif /* PAKFIRE_CLI_IMAGE_CREATE_H */
index 11dbecf10cf70f5326797548a82710913c309a4a..98fae7d2ad958b7d8cb136b40a7d2921f0f3ab33 100644 (file)
@@ -64,11 +64,11 @@ 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 },
+       { "build",    cli_build,  1, -1, 0 },
+       { "clean",    cli_clean,  0,  0, 0 },
+       { "dist",     cli_dist,   1, -1, 0 },
+       { "image",    cli_image, -1, -1, 0 },
 #if 0
-       { "image",    0, cli_image },
        { "info",     0, cli_info },
        { "provides", 0, cli_provides },
        { "repo",     0, cli_repo },