From: Michael Tremer Date: Mon, 16 Oct 2023 10:43:59 +0000 (+0000) Subject: cli: Create context before setting parsing the command line X-Git-Tag: 0.9.30~1490 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bfc9a02a5793cbf75243e2025e714b01aec0542a;p=pakfire.git cli: Create context before setting parsing the command line This way, we can configure the context directly instead of passing arguments around too much. Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/pakfire.c b/src/cli/lib/pakfire.c index 4133ab7d4..6d9b048a8 100644 --- a/src/cli/lib/pakfire.c +++ b/src/cli/lib/pakfire.c @@ -72,7 +72,6 @@ static void cli_set_repo_enabled(struct pakfire* pakfire, const char* name, int } int cli_setup_pakfire(struct pakfire** pakfire, struct cli_config* config) { - struct pakfire_ctx* ctx = NULL; struct pakfire* p = NULL; FILE* f = NULL; int r; @@ -94,13 +93,8 @@ int cli_setup_pakfire(struct pakfire** pakfire, struct cli_config* config) { } } - // Create a new context - r = pakfire_ctx_create(&ctx); - if (r) - goto ERROR; - // Initialize Pakfire - r = pakfire_create(&p, ctx, config->root, config->arch, f, config->flags); + r = pakfire_create(&p, config->ctx, config->root, config->arch, f, config->flags); if (r) goto ERROR; @@ -134,8 +128,6 @@ int cli_setup_pakfire(struct pakfire** pakfire, struct cli_config* config) { *pakfire = p; ERROR: - if (ctx) - pakfire_ctx_unref(ctx); if (f) fclose(f); diff --git a/src/cli/lib/pakfire.h b/src/cli/lib/pakfire.h index cda3a2fa9..669ff86ee 100644 --- a/src/cli/lib/pakfire.h +++ b/src/cli/lib/pakfire.h @@ -21,11 +21,14 @@ #ifndef PAKFIRE_CLI_PAKFIRE_H #define PAKFIRE_CLI_PAKFIRE_H +#include #include #define MAX_REPOS 16 struct cli_config { + struct pakfire_ctx* ctx; + const char* distro; const char* config; const char* arch; diff --git a/src/cli/pakfire-builder.c b/src/cli/pakfire-builder.c index 70a92cedd..017cb60ec 100644 --- a/src/cli/pakfire-builder.c +++ b/src/cli/pakfire-builder.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -102,7 +103,7 @@ static error_t parse(int key, char* arg, void* data) { break; case OPT_DEBUG: - config->flags |= PAKFIRE_FLAGS_DEBUG; + pakfire_ctx_set_log_level(config->ctx, LOG_DEBUG); break; case OPT_DISTRO: @@ -133,7 +134,16 @@ static error_t parse(int key, char* arg, void* data) { } int main(int argc, char* argv[]) { + struct pakfire_ctx* ctx = NULL; + int r; + + // Setup the context + r = pakfire_ctx_create(&ctx); + if (r) + goto ERROR; + struct cli_config config = { + .ctx = ctx, // XXX hard-coded distro .distro = "ipfire3", .arch = NULL, @@ -141,5 +151,11 @@ int main(int argc, char* argv[]) { }; // Parse the command line and run any commands - return cli_parse(options, commands, args_doc, NULL, parse, argc, argv, &config); + r = cli_parse(options, commands, args_doc, NULL, parse, argc, argv, &config); + +ERROR: + if (ctx) + pakfire_ctx_unref(ctx); + + return r; } diff --git a/src/cli/pakfire.c b/src/cli/pakfire.c index 16aff819a..7d0c41021 100644 --- a/src/cli/pakfire.c +++ b/src/cli/pakfire.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include "lib/clean.h" #include "lib/command.h" @@ -92,7 +93,7 @@ static error_t parse(int key, char* arg, void* data) { break; case OPT_DEBUG: - config->flags |= PAKFIRE_FLAGS_DEBUG; + pakfire_ctx_set_log_level(config->ctx, LOG_DEBUG); break; case OPT_OFFLINE: @@ -128,7 +129,16 @@ static error_t parse(int key, char* arg, void* data) { int main(int argc, char* argv[]) { + struct pakfire_ctx* ctx = NULL; + int r; + + // Setup the context + r = pakfire_ctx_create(&ctx); + if (r) + goto ERROR; + struct cli_config config = { + .ctx = ctx, // XXX hard-coded path .config = "/etc/pakfire/general.conf", .arch = NULL, @@ -138,5 +148,11 @@ int main(int argc, char* argv[]) { }; // Parse the command line and run any commands - return cli_parse(options, commands, args_doc, NULL, parse, argc, argv, &config); + r = cli_parse(options, commands, args_doc, NULL, parse, argc, argv, &config); + +ERROR: + if (ctx) + pakfire_ctx_unref(ctx); + + return r; }