]> git.ipfire.org Git - pakfire.git/commitdiff
CLI: Add a function to show the API errors
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 27 Jun 2025 10:07:40 +0000 (10:07 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 27 Jun 2025 10:07:40 +0000 (10:07 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/cli/lib/api.c [new file with mode: 0644]
src/cli/lib/api.h [new file with mode: 0644]
src/cli/lib/client-build.c

index af3aa3965d0dc76c1182efd82881c0b9a86a1130..20bc82ec7ba16aa2c94179f501a74c51f9d7e0fa 100644 (file)
@@ -486,6 +486,8 @@ noinst_LTLIBRARIES += \
        libcli.la
 
 libcli_la_SOURCES = \
+       src/cli/lib/api.c \
+       src/cli/lib/api.h \
        src/cli/lib/assert.c \
        src/cli/lib/assert.h \
        src/cli/lib/auth.c \
diff --git a/src/cli/lib/api.c b/src/cli/lib/api.c
new file mode 100644 (file)
index 0000000..d92cad7
--- /dev/null
@@ -0,0 +1,85 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2025 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdio.h>
+
+#include <pakfire/xfer.h>
+
+#include "api.h"
+
+int cli_api_error(const struct pakfire_xfer_response* response,
+               const char* format, ...) {
+       char* prefix = NULL;
+       char* error = NULL;
+       va_list args;
+       int r;
+
+       // Do nothing if there has not been an error
+       if (!response->error)
+               return 0;
+
+       // Fetch the error
+       r = pakfire_xfer_response_get_error(response, &error);
+       if (r < 0)
+               goto ERROR;
+
+       // Format the prefix
+       if (format) {
+               va_start(args, format);
+               r = vasprintf(&prefix, format, args);
+               va_end(args);
+
+               // Handle any errors
+               if (r < 0) {
+                       r = -errno;
+                       goto ERROR;
+               }
+       }
+
+       // Print prefix & error
+       if (prefix && error)
+               r = fprintf(stderr, "%s: %s\n", prefix, error);
+
+       // If there is no error, we only print the prefix
+       else if (prefix)
+               r = fprintf(stderr, "%s\n", prefix);
+
+       // If there is no prefix, we only print the error
+       else if (error)
+               r = fprintf(stderr, "%s\n", error);
+
+       // Handle any errors
+       if (r < 0) {
+               r = -errno;
+               goto ERROR;
+       }
+
+       // For now, assume all of these are I/O errors
+       r = -EIO;
+
+ERROR:
+       if (prefix)
+               free(prefix);
+       if (error)
+               free(error);
+
+       return r;
+}
diff --git a/src/cli/lib/api.h b/src/cli/lib/api.h
new file mode 100644 (file)
index 0000000..748157c
--- /dev/null
@@ -0,0 +1,29 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2025 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_CLI_API_H
+#define PAKFIRE_CLI_API_H
+
+#include <pakfire/xfer.h>
+
+int cli_api_error(const struct pakfire_xfer_response* response, const char* format, ...)
+       __attribute__((format(printf, 2, 3)));;
+
+#endif /* PAKFIRE_CLI_API_H */
index 37fc19df848920a939b37782de7fb6f338f62288..a29390a1c818422e33e254420266478748154a3a 100644 (file)
@@ -24,6 +24,7 @@
 #include <pakfire/json.h>
 #include <pakfire/xfer.h>
 
+#include "api.h"
 #include "client-build.h"
 #include "command.h"
 #include "pakfire.h"
@@ -101,33 +102,22 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
 static int build_callback(struct pakfire_xfer* xfer,
                const pakfire_xfer_response* response, void* data) {
        const char* uuid = NULL;
-       char* error = NULL;
        int r;
 
        // Handle errors
-       if (response->error) {
-               r = pakfire_xfer_response_get_error(response, &error);
-               if (r < 0)
-                       goto ERROR;
-
-               fprintf(stderr, "Failed to create build: %s\n", error);
-               r = 1;
-               goto ERROR;
-       }
+       r = cli_api_error(response, "Failed to create build");
+       if (r < 0)
+               return 0;
 
        // Fetch the UUID
        r = pakfire_json_get_string(response->data, "uuid", &uuid);
        if (r < 0)
-               goto ERROR;
+               return r;
 
        // Show status
        printf("Created build %s\n", uuid);
 
-ERROR:
-       if (error)
-               free(error);
-
-       return r;
+       return 0;
 }
 
 // Called when an upload was successful