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 \
--- /dev/null
+/*#############################################################################
+# #
+# 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;
+}
--- /dev/null
+/*#############################################################################
+# #
+# 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 */
#include <pakfire/json.h>
#include <pakfire/xfer.h>
+#include "api.h"
#include "client-build.h"
#include "command.h"
#include "pakfire.h"
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