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

index 6d4fa4a9fe5a12d604fd00c6e5c61781629b3a4e..71cb1b83992471c97a4fccda124bcfe211dc256b 100644 (file)
 #                                                                             #
 #############################################################################*/
 
-#include <pakfire/pakfire.h>
-
 #include "command.h"
 #include "repo.h"
 #include "repo_compose.h"
 
-int cli_repo(struct pakfire* pakfire, int argc, char* argv[]) {
+int cli_repo(void* data, int argc, char* argv[]) {
        static const struct command commands[] = {
-#if 0
-               { "compose",  0, cli_repo_compose },
-#endif
+               { "compose", cli_repo_compose, 1, -1, 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 8a1ef94ce2c8b0de0f920e63507b1c9883348479..b97cab8c5ebc9ff98fcd60f348d7e00f070d1711 100644 (file)
@@ -21,8 +21,6 @@
 #ifndef PAKFIRE_CLI_REPO_H
 #define PAKFIRE_CLI_REPO_H
 
-#include <pakfire/pakfire.h>
-
-int cli_repo(struct pakfire* pakfire, int argc, char* argv[]);
+int cli_repo(void* data, int argc, char* argv[]);
 
 #endif /* PAKFIRE_CLI_REPO_H */
index a824c30ba3abc9faa797be95ce7beecec8f1fe92..cccee66667af3feabc6816d2843aa4d693985594 100644 (file)
 #                                                                             #
 #############################################################################*/
 
-#include <errno.h>
-#include <getopt.h>
-#include <stdlib.h>
+#include <argp.h>
 
+#include <pakfire/key.h>
 #include <pakfire/pakfire.h>
 
+#include "command.h"
 #include "repo_compose.h"
+#include "pakfire.h"
+
+static const char* args_doc = "[OPTIONS...] PATH PACKAGES...";
+
+static const char* doc = "Create a new repository";
+
+#define MAX_PACKAGES 1024
 
 struct config {
        const char* path;
-       char** files;
-       struct pakfire_key* key;
+       const char* key;
+
+       char* packages[MAX_PACKAGES];
+       unsigned int num_packages;
+};
+
+enum {
+       OPT_KEY = 1,
+};
+
+static struct argp_option options[] = {
+       { "key", OPT_KEY, "KEY", 0, "Use this key to sign the repository", 0 },
+       { NULL },
 };
 
-static void help(void) {
-       printf(
-               "%s [OPTIONS...] repo compose [OPTIONS...] PACKAGE...\n\n"
-               "Options:\n"
-               "     --key PATH             The key to sign the repository with\n"
-               "  -h --help                 Show help\n",
-               program_invocation_short_name
-       );
+static error_t parse(int key, char* arg, void* data) {
+       struct config* config = data;
+
+       switch (key) {
+               case OPT_KEY:
+                       config->key = arg;
+                       break;
 
-       exit(0);
+               case ARGP_KEY_ARG:
+                       if (!config->path)
+                               config->path = arg;
+
+                       else if (config->num_packages >= MAX_PACKAGES)
+                               return -ENOBUFS;
+
+                       else
+                               config->packages[config->num_packages++] = arg;
+                       break;
+
+               default:
+                       return ARGP_ERR_UNKNOWN;
+       }
+
+       return 0;
 }
 
 static int cli_import_key(struct pakfire_key** key,
@@ -69,64 +101,43 @@ static int cli_import_key(struct pakfire_key** key,
        return r;
 }
 
-static int parse_argv(struct pakfire* pakfire, struct config* config,
-               int argc, char* argv[]) {
-       enum {
-               ARG_KEY,
-       };
+int cli_repo_compose(void* data, int argc, char* argv[]) {
+       struct pakfire* pakfire = NULL;
+       struct pakfire_key* key = NULL;
        int r;
 
-       static const struct option options[] = {
-               { "key",  required_argument, NULL, ARG_KEY },
-               { "help", no_argument,       NULL, 'h' },
-               { NULL },
-       };
-       int c;
+       struct cli_config* cli_config = data;
 
-       for (;;) {
-               c = getopt_long(argc, argv, "h", options, NULL);
-               if (c < 0)
-                       break;
-
-               switch (c) {
-                       case 'h':
-                               help();
-
-                       case ARG_KEY:
-                               r = cli_import_key(&config->key, pakfire, optarg);
-                               if (r)
-                                       return r;
-                               break;
-
-                       case '?':
-                               return -EINVAL;
-
-                       default:
-                               break;
-               }
-       }
-
-       return 0;
-}
-
-
-int cli_repo_compose(struct pakfire* pakfire, int argc, char* argv[]) {
        struct config config = {
                .path = NULL,
                .key  = NULL,
        };
-       int r;
 
-       // Parse commandline
-       r = parse_argv(pakfire, &config, argc, argv);
+       // Parse the command line
+       r = cli_parse(options, NULL, args_doc, doc, parse, argc, argv, &config);
        if (r)
-               return r;
+               goto ERROR;
 
-       // XXX check if we have enough arguments
+       // Setup pakfire
+       r = cli_setup_pakfire(&pakfire, cli_config);
+       if (r)
+               goto ERROR;
 
-       // Set path
-       config.path = argv[optind++]; argc--;
-       config.files = argv + optind;
+       // Read the key (if any)
+       if (config.key) {
+               r = cli_import_key(&key, pakfire, config.key);
+               if (r)
+                       goto ERROR;
+       }
+
+       // Write the repository
+       r = pakfire_repo_compose(pakfire, config.path, key, (const char**)config.packages);
 
-       return pakfire_repo_compose(pakfire, config.path, config.key, (const char**)config.files);
+ERROR:
+       if (key)
+               pakfire_key_unref(key);
+       if (pakfire)
+               pakfire_unref(pakfire);
+
+       return r;
 }
index ff6b61c41d20825a9f5d9612448a0374cdfd860d..8e3ae82861d6005d4dfa8c2797553f77b77cfaa3 100644 (file)
 #############################################################################*/
 
 #ifndef PAKFIRE_CLI_REPO_COMPOSE_H
-#define PAKFIRE_CLI_REPO_COMPOSE_H
+#define PAKFIRE_CLI_REPO_COM
 
-#include <pakfire/pakfire.h>
-
-int cli_repo_compose(struct pakfire* pakfire, int argc, char* argv[]);
+int cli_repo_compose(void* data, int argc, char* argv[]);
 
 #endif /* PAKFIRE_CLI_REPO_COMPOSE_H */
index 7ada350f891373c736e2e56024a909475b152e49..28e32bf2505f118aa5e61bcf7cab612807447b02 100644 (file)
@@ -70,10 +70,10 @@ static const struct command commands[] = {
        { "image",    cli_image,   -1, -1, 0 },
        { "info",     cli_info,     1, -1, 0 },
        { "provides", cli_provides, 1, -1, 0 },
+       { "repo",     cli_repo,    -1, -1, 0 },
        { "requires", cli_requires, 1, -1, 0 },
        { "search",   cli_search,   1, -1, 0 },
 #if 0
-       { "repo",     0, cli_repo },
        { "repolist", 0, cli_repolist },
        { "shell",    0, cli_shell },
 #endif