]> git.ipfire.org Git - pakfire.git/commitdiff
cli: sync: Migrate command to the new system
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 31 Oct 2023 18:36:04 +0000 (18:36 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 31 Oct 2023 18:36:04 +0000 (18:36 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/sync.c
src/cli/lib/sync.h
src/cli/pakfire.c

index fc2ed876fd3cec64063d3af8384b9c6119bc2e7c..b602c556dc8230a9fa79799388e4d1a802927cd9 100644 (file)
 #                                                                             #
 #############################################################################*/
 
-#include <errno.h>
-#include <getopt.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include <argp.h>
 
 #include <pakfire/pakfire.h>
-#include <pakfire/transaction.h>
 
+#include "command.h"
+#include "pakfire.h"
 #include "sync.h"
 #include "transaction.h"
 
+static const char* args_doc = "sync [OPTIONS...]";
+
+static const char* doc = "Synchronize packages";
+
 struct config {
        int job_flags;
 };
 
-static void help(void) {
-       printf(
-               "%s [OPTIONS...] sync [OPTIONS...]\n\n"
-               "Options:\n"
-               "     --keep-orphaned        Do not uninstall orphaned packages\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_KEEP_ORPHANED,
-       };
-
-       static const struct option options[] = {
-               { "help",                no_argument, NULL, 'h' },
-               { "keep-orphaned",       no_argument, NULL, ARG_KEEP_ORPHANED },
-               { NULL },
-       };
-       int c;
-
-       for (;;) {
-               c = getopt_long(argc, argv, "h", options, NULL);
-               if (c < 0)
-                       break;
+enum {
+       OPT_KEEP_ORPHANED = 1,
+};
 
-               switch (c) {
-                       case 'h':
-                               help();
+static struct argp_option options[] = {
+       { "keep-orphaned", OPT_KEEP_ORPHANED, NULL, 0, "Keep orphaned packages", 0 },
+       { NULL },
+};
 
-                       case ARG_KEEP_ORPHANED:
-                               config->job_flags |= PAKFIRE_JOB_KEEP_ORPHANED;
-                               break;
+static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
+       struct config* config = data;
 
-                       case '?':
-                               return -EINVAL;
+       switch (key) {
+               case OPT_KEEP_ORPHANED:
+                       config->job_flags |= PAKFIRE_JOB_KEEP_ORPHANED;
+                       break;
 
-                       default:
-                               break;
-               }
+               default:
+                       return ARGP_ERR_UNKNOWN;
        }
 
        return 0;
@@ -95,16 +73,28 @@ static int __cli_sync(struct pakfire_transaction* transaction, int argc, char* a
        return 0;
 }
 
-int cli_sync(struct pakfire* pakfire, int argc, char* argv[]) {
-       struct config config = {
-               .job_flags = 0,
-       };
+int cli_sync(void* data, int argc, char* argv[]) {
+       struct pakfire* pakfire = NULL;
+       struct config config = {};
        int r;
 
-       // Parse CLI options
-       r = parse_argv(&config, argc, argv);
+       struct cli_config* cli_config = data;
+
+       // Parse the command line
+       r = cli_parse(options, NULL, args_doc, doc, parse, argc, argv, &config);
        if (r)
-               return r;
+               goto ERROR;
+
+       // Setup Pakfire
+       r = cli_setup_pakfire(&pakfire, cli_config);
+       if (r)
+               goto ERROR;
+
+       r = cli_transaction(pakfire, argc, argv, 0, __cli_sync, &config);
+
+ERROR:
+       if (pakfire)
+               pakfire_unref(pakfire);
 
-       return cli_transaction(pakfire, argc, argv, 0, __cli_sync, &config);
+       return r;
 }
index 3c8fdb02fad7161b4387ebb7439f42c857992c85..e79df1913148728c054bcd55fdfde328081cb1f7 100644 (file)
@@ -21,8 +21,6 @@
 #ifndef PAKFIRE_CLI_SYNC_H
 #define PAKFIRE_CLI_SYNC_H
 
-#include <pakfire/pakfire.h>
-
-int cli_sync(struct pakfire* pakfire, int argc, char* argv[]);
+int cli_sync(void* data, int argc, char* argv[]);
 
 #endif /* PAKFIRE_CLI_SYNC_H */
index 9816b0b113954d1270c435c2df7665fa904e07f4..03ffb5ca6c4f23bf6f3644c9e637c3a2abed5eb9 100644 (file)
@@ -83,7 +83,7 @@ static const struct command commands[] = {
        { "repolist", cli_repolist, 0,  0, 0 },
        { "requires", cli_requires, 1, -1, 0 },
        { "search",   cli_search,   1, -1, 0 },
-       //{ "sync",     cli_sync,     0,  0, 0 },
+       { "sync",     cli_sync,     0,  0, 0 },
        //{ "update",   cli_update,   0, -1, 0 },
        { NULL },
 };