]> git.ipfire.org Git - pakfire.git/commitdiff
cli: Create context before setting parsing the command line
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 16 Oct 2023 10:43:59 +0000 (10:43 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 16 Oct 2023 10:43:59 +0000 (10:43 +0000)
This way, we can configure the context directly instead of passing
arguments around too much.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/pakfire.c
src/cli/lib/pakfire.h
src/cli/pakfire-builder.c
src/cli/pakfire.c

index 4133ab7d4a17ff9cb67f148aa2babf882c1b9471..6d9b048a8ec095b02c7503fad9463d472c7a27f4 100644 (file)
@@ -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);
 
index cda3a2fa91b19ddc620d73c2522413ec419b231b..669ff86ee3d97ffdd15380b9532bac4179fd389e 100644 (file)
 #ifndef PAKFIRE_CLI_PAKFIRE_H
 #define PAKFIRE_CLI_PAKFIRE_H
 
+#include <pakfire/ctx.h>
 #include <pakfire/pakfire.h>
 
 #define MAX_REPOS 16
 
 struct cli_config {
+       struct pakfire_ctx* ctx;
+
        const char* distro;
        const char* config;
        const char* arch;
index 70a92cedd9e90333ac98e54c22429dabc22d8a1a..017cb60ecd9d2d84dd2e76a5e35944f5692f1433 100644 (file)
@@ -22,6 +22,7 @@
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <syslog.h>
 #include <unistd.h>
 
 #include <pakfire/pakfire.h>
@@ -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;
 }
index 16aff819a1d4db1f3d01bba315e7f044cae72de6..7d0c41021d1fd0d7ec4ac5caecec4967d46b8bb1 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <argp.h>
+#include <syslog.h>
 
 #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;
 }