]> git.ipfire.org Git - people/ms/pakfire.git/commitdiff
CLI: Create a convenience function to set up a build environment
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Jan 2025 15:48:34 +0000 (15:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Jan 2025 15:48:34 +0000 (15:48 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/build.c
src/cli/lib/image_create.c
src/cli/lib/pakfire.c
src/cli/lib/pakfire.h
src/cli/lib/shell.c

index d7512823c002adb7d0e09efa61050dd86a9ffb42..6995dbabc7cebd6ec02015e13734c5ac18789a32 100644 (file)
@@ -26,7 +26,6 @@
 #include "build.h"
 #include "color.h"
 #include "command.h"
-#include "config.h"
 #include "pakfire.h"
 
 #include <pakfire/build.h>
@@ -158,9 +157,8 @@ int cli_build(void* data, int argc, char* argv[]) {
                        BUILD_ENABLE_SNAPSHOT |
                        BUILD_ENABLE_TESTS,
        };
-       struct pakfire_config* config = NULL;
        struct pakfire_build* build = NULL;
-       int build_flags = 0;
+       int flags = 0;
        int r;
 
        // Parse the command line
@@ -173,31 +171,24 @@ int cli_build(void* data, int argc, char* argv[]) {
 
        // Use the snapshot?
        if (local_args.flags & BUILD_ENABLE_SNAPSHOT)
-               build_flags |= PAKFIRE_BUILD_ENABLE_SNAPSHOT;
+               flags |= PAKFIRE_BUILD_ENABLE_SNAPSHOT;
 
        // Is the build interactive?
        if (local_args.flags & BUILD_INTERACTIVE)
-               build_flags |=  PAKFIRE_BUILD_INTERACTIVE;
+               flags |=        PAKFIRE_BUILD_INTERACTIVE;
 
        // Enable ccache?
        if (!(local_args.flags & BUILD_ENABLE_CCACHE))
-               build_flags |= PAKFIRE_BUILD_DISABLE_CCACHE;
+               flags |= PAKFIRE_BUILD_DISABLE_CCACHE;
 
        // Enable tests?
        if (!(local_args.flags & BUILD_ENABLE_TESTS))
-               build_flags |= PAKFIRE_BUILD_DISABLE_TESTS;
-
-       // Read configuration
-       r = cli_setup_config(&config, global_args);
-       if (r < 0)
-               goto ERROR;
+               flags |= PAKFIRE_BUILD_DISABLE_TESTS;
 
        // Setup the build environment
-       r = pakfire_build_create(&build, global_args->ctx, config, global_args->arch, local_args.id, build_flags);
-       if (r) {
-               fprintf(stderr, "Could not setup the build environment: %m\n");
+       r = cli_setup_build(&build, global_args, flags);
+       if (r < 0)
                goto ERROR;
-       }
 
        // Set target
        if (local_args.target) {
@@ -221,8 +212,6 @@ int cli_build(void* data, int argc, char* argv[]) {
 ERROR:
        if (build)
                pakfire_build_unref(build);
-       if (config)
-               pakfire_config_unref(config);
 
        return r;
 }
index 50f7d700bc4481a32caab6b70af32f25d5c69fac..e82d7670bd2285e88e5770404bee34bf4ecc5c48 100644 (file)
@@ -24,7 +24,6 @@
 #include <pakfire/pakfire.h>
 
 #include "command.h"
-#include "config.h"
 #include "image_create.h"
 #include "pakfire.h"
 
@@ -61,7 +60,6 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
 int cli_image_create(void* data, int argc, char* argv[]) {
        struct cli_global_args* global_args = data;
        struct cli_local_args local_args = {};
-       struct pakfire_config* config = NULL;
        struct pakfire_build* build = NULL;
        FILE* f = NULL;
        int r;
@@ -71,11 +69,6 @@ int cli_image_create(void* data, int argc, char* argv[]) {
        if (r)
                goto ERROR;
 
-       // Read configuration
-       r = cli_setup_config(&config, global_args);
-       if (r < 0)
-               goto ERROR;
-
        f = fopen(local_args.path, "w");
        if (!f) {
                fprintf(stderr, "Could not open %s: %m\n", local_args.path);
@@ -84,8 +77,8 @@ int cli_image_create(void* data, int argc, char* argv[]) {
        }
 
        // Setup the build environment
-       r = pakfire_build_create(&build, global_args->ctx, config, global_args->arch, NULL, 0);
-       if (r)
+       r = cli_setup_build(&build, global_args, 0);
+       if (r < 0)
                goto ERROR;
 
        // Create the image
@@ -96,8 +89,6 @@ int cli_image_create(void* data, int argc, char* argv[]) {
 ERROR:
        if (build)
                pakfire_build_unref(build);
-       if (config)
-               pakfire_config_unref(config);
        if (f)
                fclose(f);
 
index bed1494748b832f277e53d9afe77572ee84cda6b..2764cac60bd6ad44637fb4d46f1f34d9a747cc5b 100644 (file)
 #include <errno.h>
 #include <limits.h>
 
+#include <pakfire/build.h>
 #include <pakfire/config.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/repo.h>
 
 #include "config.h"
 #include "pakfire.h"
-#include "progressbar.h"
 
 static void cli_set_repo_enabled(struct pakfire* pakfire, const char* name, int enabled) {
        struct pakfire_repo* repo = NULL;
@@ -111,3 +111,30 @@ ERROR:
 
        return r;
 }
+
+int cli_setup_build(struct pakfire_build** build, struct cli_global_args* args, int flags) {
+       struct pakfire_config* config = NULL;
+       struct pakfire_build* b = NULL;
+       int r;
+
+       // Setup the configuration
+       r = cli_setup_config(&config, args);
+       if (r < 0)
+               goto ERROR;
+
+       // Setup the build environment
+       r = pakfire_build_create(&b, args->ctx, config, args->arch, NULL, flags);
+       if (r < 0) {
+               fprintf(stderr, "Could not setup the build environment: %s\n", strerror(-r));
+               goto ERROR;
+       }
+
+       // Return pointer
+       *build = b;
+
+ERROR:
+       if (config)
+               pakfire_config_unref(config);
+
+       return r;
+}
index 5214e39b26ce3fe920f5ae6f6ae2f4befd427473..47fef8ce328201827ea7b1cafd1669e852ce07b0 100644 (file)
@@ -21,6 +21,7 @@
 #ifndef PAKFIRE_CLI_PAKFIRE_H
 #define PAKFIRE_CLI_PAKFIRE_H
 
+#include <pakfire/build.h>
 #include <pakfire/config.h>
 #include <pakfire/ctx.h>
 #include <pakfire/pakfire.h>
@@ -46,5 +47,6 @@ struct cli_global_args {
 
 int cli_setup_config(struct pakfire_config** config, struct cli_global_args* args);
 int cli_setup_pakfire(struct pakfire** pakfire, struct cli_global_args* args);
+int cli_setup_build(struct pakfire_build** build, struct cli_global_args* args, int flags);
 
 #endif /* PAKFIRE_CLI_PAKFIRE_H */
index cc6df2e8ac2eced91ccdd94dc865abe5b47d09c1..8c5bfdc384e65ece1995b3f0f3c7abb2e9163e54 100644 (file)
@@ -21,7 +21,6 @@
 #include <argp.h>
 
 #include "command.h"
-#include "config.h"
 #include "pakfire.h"
 #include "shell.h"
 
@@ -97,9 +96,8 @@ int cli_shell(void* data, int argc, char* argv[]) {
                .packages     = {},
                .num_packages = 0,
        };
-       struct pakfire_config* config = NULL;
        struct pakfire_build* build = NULL;
-       int build_flags = PAKFIRE_BUILD_INTERACTIVE;
+       int flags = PAKFIRE_BUILD_INTERACTIVE;
        int r;
 
        // Parse the command line
@@ -109,19 +107,12 @@ int cli_shell(void* data, int argc, char* argv[]) {
 
        // Enable snapshots?
        if (local_args.flags & SHELL_ENABLE_SNAPSHOT)
-               build_flags |= PAKFIRE_BUILD_ENABLE_SNAPSHOT;
-
-       // Read configuration
-       r = cli_setup_config(&config, global_args);
-       if (r < 0)
-               goto ERROR;
+               flags |= PAKFIRE_BUILD_ENABLE_SNAPSHOT;
 
        // Setup the build environment
-       r = pakfire_build_create(&build, global_args->ctx, config, global_args->arch, NULL, build_flags);
-       if (r) {
-               fprintf(stderr, "Could not setup the build environment: %m\n");
+       r = cli_setup_build(&build, global_args, flags);
+       if (r < 0)
                goto ERROR;
-       }
 
        // Install any additional packages
        if (local_args.num_packages) {
@@ -136,8 +127,6 @@ int cli_shell(void* data, int argc, char* argv[]) {
 ERROR:
        if (build)
                pakfire_build_unref(build);
-       if (config)
-               pakfire_config_unref(config);
 
        return r;
 }