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

index 982de8e35ca4337992bc6f63cc29639711aab80a..94c1e5b22237baedeb2874476c19e01a3d78028b 100644 (file)
 #                                                                             #
 #############################################################################*/
 
-#include <errno.h>
-#include <getopt.h>
-#include <stdlib.h>
+#include <argp.h>
 
 #include "command.h"
+#include "pakfire.h"
 #include "shell.h"
 
 #include <pakfire/build.h>
 
 struct config {
        int flags;
+
+       // Packages
        const char* packages[MAX_PACKAGES];
        unsigned int num_packages;
 };
 
-static void help(void) {
-       printf(
-               "%s [OPTIONS...] shell [OPTIONS...]\n\n"
-               "Options:\n"
-               "     --disable-snapshot     Do not use the snapshot\n"
-               "     --install PACKAGE      Installs the given package\n"
-               "  -h --help                 Show help\n",
-               program_invocation_short_name
-       );
-
-       exit(0);
-}
-
-static int parse_argv(struct config* config, int argc, char* argv[]) {
-       enum {
-               ARG_DISABLE_SNAPSHOT,
-               ARG_INSTALL,
-       };
-
-       static const struct option options[] = {
-               { "disable-snapshot",    no_argument,       NULL, ARG_DISABLE_SNAPSHOT },
-               { "help",                no_argument,       NULL, 'h' },
-               { "install",             required_argument, NULL, ARG_INSTALL },
-               { NULL },
-       };
-       int c;
+static const char* doc =
+       "Creates a build environment and launches an interactive shell in it";
 
-       for (;;) {
-               c = getopt_long(argc, argv, "h", options, NULL);
-               if (c < 0)
-                       break;
+enum {
+       OPT_DISABLE_SNAPSHOT = 1,
+       OPT_INSTALL          = 2,
+};
 
-               switch (c) {
-                       case 'h':
-                               help();
+static struct argp_option options[] = {
+       { "disable-snapshot", OPT_DISABLE_SNAPSHOT,      NULL, 0, "Do not use the snapshot", 0 },
+       { "install",          OPT_INSTALL,          "PACKAGE", 0, "Install this package", 0 },
+       { NULL },
+};
 
-                       case ARG_DISABLE_SNAPSHOT:
-                               config->flags |= PAKFIRE_BUILD_DISABLE_SNAPSHOT;
-                               break;
+static error_t parse(int key, char* arg, void* data) {
+       struct config* config = data;
 
-                       case ARG_INSTALL:
-                               if (config->num_packages >= MAX_PACKAGES)
-                                       return -ENOBUFS;
+       switch (key) {
+               case OPT_DISABLE_SNAPSHOT:
+                       config->flags |= PAKFIRE_BUILD_DISABLE_SNAPSHOT;
+                       break;
 
-                               config->packages[config->num_packages++] = optarg;
-                               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_shell(struct pakfire* pakfire, int argc, char* argv[]) {
+int cli_shell(void* data, int argc, char* argv[]) {
+       struct pakfire* pakfire = NULL;
+       int r;
+
+       struct cli_config* cli_config = data;
+
        struct config config = {
-               .packages = { NULL },
+               .flags        = 0,
+               .packages     = { NULL },
                .num_packages = 0,
        };
-       int r;
 
-       // Parse commandline
-       r = parse_argv(&config, argc, argv);
+       // Parse the command line
+       r = cli_parse(options, NULL, NULL, doc, parse, argc, argv, &config);
        if (r)
-               return r;
+               goto ERROR;
+
+       // Setup pakfire
+       r = cli_setup_pakfire(&pakfire, cli_config);
+       if (r)
+               goto ERROR;
+
+       // Run the shell
+       r = pakfire_shell(pakfire, config.packages, config.flags);
+       if (r)
+               goto ERROR;
+
+ERROR:
+       if (pakfire)
+               pakfire_unref(pakfire);
 
-       return pakfire_shell(pakfire, config.packages, config.flags);
+       return r;
 }
index 1bc6fe735da3647be28c29cf53086c240778e588..3f780bdea9ea71345dd556573d30ad97068e2652 100644 (file)
@@ -21,8 +21,6 @@
 #ifndef PAKFIRE_CLI_SHELL_H
 #define PAKFIRE_CLI_SHELL_H
 
-#include <pakfire/pakfire.h>
-
-int cli_shell(struct pakfire* pakfire, int argc, char* argv[]);
+int cli_shell(void* data, int argc, char* argv[]);
 
 #endif /* PAKFIRE_CLI_SHELL_H */
index 67d1efd44f7e72ae1135d3b178c007ac39f55732..7e1a4e25367e017240dcd0c88a1a55a011d94dbc 100644 (file)
@@ -73,11 +73,8 @@ static const struct command commands[] = {
        { "repo",     cli_repo,    -1, -1, 0 },
        { "repolist", cli_repolist, 0,  0, 0 },
        { "requires", cli_requires, 1, -1, 0 },
+       { "shell",    cli_shell,    0,  0, 0 },
        { "search",   cli_search,   1, -1, 0 },
-
-#if 0
-       { "shell",    0, cli_shell },
-#endif
        { NULL },
 };