From: Michael Tremer Date: Tue, 31 Oct 2023 18:36:04 +0000 (+0000) Subject: cli: sync: Migrate command to the new system X-Git-Tag: 0.9.30~1363 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9d72126e145d456662b9424afccb0706df5becdd;p=people%2Fms%2Fpakfire.git cli: sync: Migrate command to the new system Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/sync.c b/src/cli/lib/sync.c index fc2ed876f..b602c556d 100644 --- a/src/cli/lib/sync.c +++ b/src/cli/lib/sync.c @@ -18,64 +18,42 @@ # # #############################################################################*/ -#include -#include -#include -#include +#include #include -#include +#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; } diff --git a/src/cli/lib/sync.h b/src/cli/lib/sync.h index 3c8fdb02f..e79df1913 100644 --- a/src/cli/lib/sync.h +++ b/src/cli/lib/sync.h @@ -21,8 +21,6 @@ #ifndef PAKFIRE_CLI_SYNC_H #define PAKFIRE_CLI_SYNC_H -#include - -int cli_sync(struct pakfire* pakfire, int argc, char* argv[]); +int cli_sync(void* data, int argc, char* argv[]); #endif /* PAKFIRE_CLI_SYNC_H */ diff --git a/src/cli/pakfire.c b/src/cli/pakfire.c index 9816b0b11..03ffb5ca6 100644 --- a/src/cli/pakfire.c +++ b/src/cli/pakfire.c @@ -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 }, };