Updated and refactored code

- Added `doxygen` API documentation with theme
- Added error-compatible logging function
- Added more error codes
- Added `non_copyable` function
- Added test function from exporting package list
- Changed how errors are handled with returns
- Change `gdpm remote` API to reflect `git`
- Change most functions to accept a vector of arguments instead of a single parameter
- Updated `.gitignore` and `README.md` files
- Fixed issue with `gdpm export` command crashing
This commit is contained in:
David Allen 2023-05-14 19:30:33 -06:00
parent 8e82a22ebd
commit ba23299777
14 changed files with 2950 additions and 93 deletions

View file

@ -1,12 +1,15 @@
#include <fmt/core.h>
#include <string>
#include "log.hpp"
namespace gdpm::error_codes{
enum {
UNKNOWN = 0,
NOT_FOUND = 1,
NONE = 0,
UNKNOWN = 1,
NOT_FOUND = 2,
FILE_EXISTS = 3,
HOST_UNREACHABLE = 4,
};
inline std::string to_string(int error_code) {
@ -15,7 +18,7 @@ namespace gdpm::error_codes{
};
namespace gdpm{
class error{
class error {
public:
error(int code = 0, const std::string& message = "", bool print_message = false):
m_code(code), m_message(message)
@ -36,4 +39,17 @@ namespace gdpm{
int m_code;
std::string m_message;
};
// Add logging function that can handle error objects
namespace log {
template <typename S, typename...Args>
static constexpr void error(const gdpm::error& e){
#if GDPM_LOG_LEVEL > 1
vlog(
fmt::format(GDPM_COLOR_LOG_ERROR "[ERROR {}" GDPM_COLOR_LOG_RESET, e.get_message()),
fmt::make_format_args("" /*e.get_message()*/)
);
#endif
}
}
}

View file

@ -12,10 +12,30 @@
#include <fmt/printf.h>
#include <fmt/format.h>
/*
TODO: Allow setting the logging *prefix*
TODO: Write log information to file
*/
namespace gdpm::log
{
template <typename...Args> concept RequireMinArgs = requires (std::size_t min){ sizeof...(Args) > min; };
enum level{
NONE,
INFO,
WARNING,
DEBUG,
ERROR
};
struct config {
static int level;
static std::string prefix;
static std::string path;
static bool print_to_stdout;
static bool print_to_stderr;
};
static void vlog(fmt::string_view format, fmt::format_args args){
fmt::vprint(format, args);
}

View file

@ -6,6 +6,7 @@
#include <cxxopts.hpp>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include <rapidjson/document.h>
@ -15,7 +16,7 @@ namespace gdpm::package_manager{
extern std::vector<std::string> repo_sources;
extern CURL *curl;
extern CURLcode res;
extern config::context config;
extern config::context config;
struct package_info{
size_t asset_id;
@ -59,6 +60,12 @@ namespace gdpm::package_manager{
none
};
using package_list = std::vector<package_info>;
using package_titles = std::vector<std::string>;
using cl_arg = std::variant<int, bool, float, std::string>;
using cl_args = std::vector<cl_arg>;
using cl_opts = std::unordered_map<std::string, cl_args>;
GDPM_DLL_EXPORT int initialize(int argc, char **argv);
GDPM_DLL_EXPORT int execute();
GDPM_DLL_EXPORT void finalize();
@ -69,17 +76,18 @@ namespace gdpm::package_manager{
GDPM_DLL_EXPORT error remove_all_packages();
GDPM_DLL_EXPORT error update_packages(const std::vector<std::string>& package_titles, bool skip_prompt = false);
GDPM_DLL_EXPORT error search_for_packages(const std::vector<std::string>& package_titles, bool skip_prompt = false);
GDPM_DLL_EXPORT error export_packages(const std::string& path);
GDPM_DLL_EXPORT error export_packages(const std::vector<std::string>& paths);
GDPM_DLL_EXPORT std::vector<std::string> list_information(const std::vector<std::string>& opts, bool print_list = true);
GDPM_DLL_EXPORT void clean_temporary(const std::vector<std::string>& package_titles);
GDPM_DLL_EXPORT void link_packages(const std::vector<std::string>& package_titles, const std::vector<std::string>& paths);
GDPM_DLL_EXPORT void clone_packages(const std::vector<std::string>& package_titles, const std::vector<std::string>& paths);
/* Remote API */
GDPM_DLL_EXPORT void _handle_remote(const std::string& repository);
GDPM_DLL_EXPORT void remote_add_repository(const std::string& repository, ssize_t offset = -1);
GDPM_DLL_EXPORT void remote_remove_respository(const std::string& repository);
GDPM_DLL_EXPORT error _handle_remote(const std::vector<std::string>& args, const std::vector<std::string>& opts);
GDPM_DLL_EXPORT void remote_add_repository(const std::vector<std::string>& repositories);
GDPM_DLL_EXPORT void remote_remove_respository(const std::vector<std::string>& repositories);
GDPM_DLL_EXPORT void remote_remove_respository(ssize_t index);
GDPM_DLL_EXPORT void remote_move_repository(int old_position, int new_position);
/* Auxiliary Functions */
GDPM_DLL_EXPORT cxxargs _parse_arguments(int argc, char **argv);

23
include/types.hpp Normal file
View file

@ -0,0 +1,23 @@
#pragma once
namespace gdpm{
/*
Base class to prevent derived class from creating copies.
*/
class non_copyable{
public:
non_copyable(){}
private:
non_copyable(const non_copyable&);
non_copyable& operator=(const non_copyable&);
};
/*
Base class to prevent derived classes from moving objects.
*/
class non_movable{
non_movable(const non_movable&) = delete;
non_movable(non_movable&&) = delete;
};
}

View file

@ -103,4 +103,20 @@ namespace gdpm::utils{
bool prompt_user_yn(const char *message);
void delay(std::chrono::milliseconds milliseconds = GDPM_REQUEST_DELAY);
// TODO: Add function to get size of decompressed zip
}
namespace gdpm{
class non_copyable{
public:
non_copyable(){}
private:
non_copyable(const non_copyable&);
non_copyable& operator=(const non_copyable&);
};
class non_movable{
non_movable(const non_movable&) = delete;
non_movable(non_movable&&) = delete;
};
}