From: Michael Tremer Date: Sun, 15 Oct 2023 11:42:19 +0000 (+0000) Subject: cli: image: Upgrade to the new parser X-Git-Tag: 0.9.30~1514 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ab91ce3f337ca65232cee9fdbf4a69e760c509eb;p=pakfire.git cli: image: Upgrade to the new parser Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/image.c b/src/cli/lib/image.c index 1d60e20f3..60678bd5f 100644 --- a/src/cli/lib/image.c +++ b/src/cli/lib/image.c @@ -18,23 +18,15 @@ # # #############################################################################*/ -#include - #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); } diff --git a/src/cli/lib/image.h b/src/cli/lib/image.h index 7bd5fb5db..4e9495495 100644 --- a/src/cli/lib/image.h +++ b/src/cli/lib/image.h @@ -21,8 +21,6 @@ #ifndef PAKFIRE_CLI_IMAGE_H #define PAKFIRE_CLI_IMAGE_H -#include - -int cli_image(struct pakfire* pakfire, int argc, char* argv[]); +int cli_image(void* data, int argc, char* argv[]); #endif /* PAKFIRE_CLI_IMAGE_H */ diff --git a/src/cli/lib/image_create.c b/src/cli/lib/image_create.c index 205255bdd..595848b18 100644 --- a/src/cli/lib/image_create.c +++ b/src/cli/lib/image_create.c @@ -18,85 +18,89 @@ # # #############################################################################*/ -#include -#include -#include +#include #include #include +#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); diff --git a/src/cli/lib/image_create.h b/src/cli/lib/image_create.h index ec139f4ba..a4f4b1bd4 100644 --- a/src/cli/lib/image_create.h +++ b/src/cli/lib/image_create.h @@ -21,8 +21,6 @@ #ifndef PAKFIRE_CLI_IMAGE_CREATE_H #define PAKFIRE_CLI_IMAGE_CREATE_H -#include - -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 */ diff --git a/src/cli/pakfire-builder.c b/src/cli/pakfire-builder.c index 11dbecf10..98fae7d2a 100644 --- a/src/cli/pakfire-builder.c +++ b/src/cli/pakfire-builder.c @@ -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 },