Recfactored and simplified more code

- Added function to convert color string to ansi color string
- Added `trim` and `join` utility functions
- Added initial plugin test case
- Implemented `config get` command to see config properties
- Improved logging functionality and removed duplicate logging functions
- Removed unused functions
- Fixed more styling issues
- Fixed some CLI commands not working correctly
- Fixed CLI documentation format
- Fixed some error handling issues
This commit is contained in:
David Allen 2023-06-18 10:47:05 -06:00
parent e48c54aa40
commit 02a4e879a8
21 changed files with 541 additions and 384 deletions

View file

@ -1,3 +1,7 @@
#pragma once
#include "colors.hpp"
#include <string>
#if GDPM_ENABLE_COLORS == 1
#define GDPM_COLOR_BLACK "\033[0;30m"
@ -16,24 +20,26 @@
#define GDPM_COLOR_LIGHT_PURPLE "\033[0;35m"
#define GDPM_COLOR_YELLOW "\033[0;33m"
#define GDPM_COLOR_WHITE "\033[0;37m"
#define GDPM_COLOR_RESET GDPM_COLOR_WHITE
#else
#define GDPM_COLOR_BLACK
#define GDPM_COLOR_BLUE
#define GDPM_COLOR_GREEN
#define GDPM_COLOR_CYAN
#define GDPM_COLOR_RED
#define GDPM_COLOR_PURPLE
#define GDPM_COLOR_BROWN
#define GDPM_COLOR_GRAY
#define GDPM_COLOR_DARK_GRAY
#define GDPM_COLOR_LIGHT_BLUE
#define GDPM_COLOR_LIGHT_GREEN
#define GDPM_COLOR_LIGHT_CYAN
#define GDPM_COLOR_LIGHT_RED
#define GDPM_COLOR_LIGHT_PURPLE
#define GDPM_COLOR_YELLOW
#define GDPM_COLOR_WHITE
#define GDPM_COLOR_BLACK ""
#define GDPM_COLOR_BLUE ""
#define GDPM_COLOR_GREEN ""
#define GDPM_COLOR_CYAN ""
#define GDPM_COLOR_RED ""
#define GDPM_COLOR_PURPLE ""
#define GDPM_COLOR_BROWN ""
#define GDPM_COLOR_GRAY ""
#define GDPM_COLOR_DARK_GRAY ""
#define GDPM_COLOR_LIGHT_BLUE ""
#define GDPM_COLOR_LIGHT_GREEN ""
#define GDPM_COLOR_LIGHT_CYAN ""
#define GDPM_COLOR_LIGHT_RED ""
#define GDPM_COLOR_LIGHT_PURPLE ""
#define GDPM_COLOR_YELLOW ""
#define GDPM_COLOR_WHITE ""
#define GDPM_COLOR_RESET GDPM_COLOR_WHITE
#endif
@ -42,4 +48,24 @@
#define GDPM_COLOR_LOG_INFO GDPM_COLOR_LOG_RESET
#define GDPM_COLOR_LOG_ERROR GDPM_COLOR_RED
#define GDPM_COLOR_LOG_DEBUG GDPM_COLOR_YELLOW
#define GDPM_COLOR_LOG_WARNING GDPM_COLOR_YELLOW
#define GDPM_COLOR_LOG_WARNING GDPM_COLOR_YELLOW
namespace gdpm::color{
inline std::string from_string(const std::string& color_name){
if (color_name == "red"){ return GDPM_COLOR_RED; }
else if (color_name == "yellow"){ return GDPM_COLOR_YELLOW; }
else if (color_name == "green"){ return GDPM_COLOR_GREEN; }
else if (color_name == "blue"){ return GDPM_COLOR_BLUE; }
else if (color_name == "brown"){ return GDPM_COLOR_BROWN; }
else if (color_name == "gray"){ return GDPM_COLOR_GRAY; }
else if (color_name == "black"){ return GDPM_COLOR_BLACK; }
else if (color_name == "purple"){ return GDPM_COLOR_PURPLE; }
else if (color_name == "gray"){ return GDPM_COLOR_DARK_GRAY; }
else if (color_name == "light-blue"){ return GDPM_COLOR_LIGHT_BLUE; }
else if (color_name == "light-green"){ return GDPM_COLOR_LIGHT_GREEN; }
else if (color_name == "light-cyan"){ return GDPM_COLOR_LIGHT_CYAN; }
else if (color_name == "light-red"){ return GDPM_COLOR_LIGHT_RED; }
else if (color_name == "light-purple"){ return GDPM_COLOR_LIGHT_PURPLE; }
return "";
}
}

View file

@ -43,7 +43,8 @@ namespace gdpm::config{
error handle_config(config::context& config, const args_t& args, const var_opts& opts);
context make_context(const string& username = GDPM_CONFIG_USERNAME, const string& password = GDPM_CONFIG_PASSWORD, const string& path = GDPM_CONFIG_PATH, const string& token = GDPM_CONFIG_TOKEN, const string& godot_version = GDPM_CONFIG_GODOT_VERSION, const string& packages_dir = GDPM_CONFIG_LOCAL_PACKAGES_DIR, const string& tmp_dir = GDPM_CONFIG_LOCAL_TMP_DIR, const string_map& remote_sources = {GDPM_CONFIG_REMOTE_SOURCES}, size_t threads = GDPM_CONFIG_THREADS, size_t timeout = 0, bool enable_sync = GDPM_CONFIG_ENABLE_SYNC, bool enable_file_logging = GDPM_CONFIG_ENABLE_FILE_LOGGING, int verbose = GDPM_CONFIG_VERBOSE);
error validate(const rapidjson::Document& doc);
void print(const context& config);
void print_json(const context& config);
void print_properties(const context& config, const string_list& properties);
extern context config;
}

View file

@ -15,6 +15,7 @@ namespace gdpm::constants{
const std::string UserAgent("libcurl-agent/1.0 via GDPM (https://github.com/davidallendj/gdpm)");
const std::string AssetRepo("https://godotengine.org/asset-library/api/asset");
const std::string HostUrl("https://godotengine.org/asset-library/api");
constexpr std::string WHITESPACE = " \n\r\t\f\v";
}
/* Define default macros to set when building with -DGPM_* */

View file

@ -29,8 +29,10 @@ namespace gdpm::constants::error{
INVALID_ARG_COUNT,
INVALID_CONFIG,
INVALID_KEY,
HTTP_RESPONSE_ERROR,
STD_ERROR
HTTP_RESPONSE_ERR,
SQLITE_ERR,
JSON_ERR,
STD_ERR
};
const string_list messages {

View file

@ -1,10 +1,12 @@
#pragma once
#include "clipp.h"
#include "utils.hpp"
#include "colors.hpp"
#include "types.hpp"
#include <format>
#include <bitset>
// #include <fmt/core.h>
#if __cplusplus > 201703L
@ -21,22 +23,51 @@ TODO: Write log information to file
namespace gdpm::log
{
enum level{
NONE = 0,
INFO,
WARNING,
DEBUG,
ERROR
enum level_e : int{
NONE = 0,
INFO = 1,
WARNING = 2,
DEBUG = 3,
ERROR = 4
};
struct context {
int level;
string prefix;
string path;
bool print_to_stdout;
bool print_to_stderr;
enum flag_opt {
PRINT_STDOUT = 0b00000001,
PRINT_STDERR = 0b00000010
};
static int level = INFO;
static string prefix = "";
static string suffix = "";
static string path = "";
static std::bitset<8> flags = PRINT_STDOUT | PRINT_STDERR;
static bool print_to_stdout;
static bool print_to_stderr;
inline constexpr level_e to_level(int l){
return static_cast<level_e>(l);
}
inline constexpr int to_int(const level_e& l){
return static_cast<int>(l);
}
inline constexpr void set_flag(uint8_t flag, bool value){
(value) ? flags.set(flag) : flags.reset(flag);
}
inline constexpr bool get_flag(uint8_t flag){
return flags.test(flag);
}
inline constexpr void set_prefix_if(const std::string& v, bool predicate = !prefix.empty()){
prefix = (predicate) ? v : prefix;
}
inline constexpr void set_suffix_if(const std::string& v, bool predicate = !suffix.empty()){
suffix = (predicate) ? v : suffix;
}
static void vlog(fmt::string_view format, fmt::format_args args){
fmt::vprint(format, args);
}
@ -47,30 +78,13 @@ namespace gdpm::log
template <typename S, typename...Args>
static constexpr void info(const S& format, Args&&...args){
if(log::level < to_int(log::INFO))
return;
#if GDPM_LOG_LEVEL > NONE
set_prefix_if(fmt::format("[INFO {}] ", utils::timestamp()));
set_suffix_if("\n");
vlog(
fmt::format(GDPM_COLOR_LOG_INFO "[INFO {}] {}\n" GDPM_COLOR_LOG_RESET, utils::timestamp(), format),
// fmt::make_format_args<Args...>(args...)
fmt::make_format_args(args...)
);
#endif
}
template <typename S, typename...Args>
static constexpr void info(const S& prefix, const S& format, Args&&...args){
#if GDPM_LOG_LEVEL > INFO
vlog(
fmt::format(GDPM_COLOR_LOG_INFO + prefix + GDPM_COLOR_LOG_RESET, format),
fmt::make_format_args(args...)
);
#endif
}
template <typename S, typename...Args>
static constexpr void info(const context& context, const S& format, Args&&...args){
#if GDPM_LOG_LEVEL > INFO
vlog(
fmt::format(GDPM_COLOR_LOG_INFO + context.prefix + GDPM_COLOR_LOG_RESET, format),
fmt::format(GDPM_COLOR_LOG_INFO "{}{}{}" GDPM_COLOR_LOG_RESET, prefix, format, suffix),
fmt::make_format_args(args...)
);
#endif
@ -78,10 +92,13 @@ namespace gdpm::log
template <typename S, typename...Args>
static constexpr void info_n(const S& format, Args&&...args){
if(log::level < to_int(log::INFO))
return;
#if GDPM_LOG_LEVEL > INFO
set_prefix_if(fmt::format("[INFO {}] ", utils::timestamp()));
set_suffix_if("");
vlog(
fmt::format(GDPM_COLOR_LOG_INFO "[INFO {}] {}" GDPM_COLOR_LOG_RESET, utils::timestamp(), format),
// fmt::make_format_args<Args...>(args...)
fmt::format(GDPM_COLOR_LOG_INFO "{}{}{}" GDPM_COLOR_LOG_RESET, prefix, format, suffix),
fmt::make_format_args(args...)
);
#endif
@ -89,30 +106,13 @@ namespace gdpm::log
template <typename S, typename...Args>
static constexpr void error(const S& format, Args&&...args){
if(log::level < to_int(log::ERROR))
return;
#if GDPM_LOG_LEVEL > ERROR
set_prefix_if(std::format("[ERROR {}] ", utils::timestamp()));
set_suffix_if("\n");
vlog(
fmt::format(GDPM_COLOR_LOG_ERROR "[ERROR {}] {}\n" GDPM_COLOR_LOG_RESET, utils::timestamp(), format),
// fmt::make_format_args<Args...>(args...)
fmt::make_format_args(args...)
);
#endif
}
template <typename S, typename...Args>
static constexpr void error(const S& prefix, const S& format, Args&&...args){
#if GDPM_LOG_LEVEL > ERROR
vlog(
fmt::format(GDPM_COLOR_LOG_ERROR + prefix + GDPM_COLOR_LOG_RESET, format),
fmt::make_format_args(args...)
);
#endif
}
template <typename S, typename...Args>
static constexpr void error(const context& context, const S& format, Args&&...args){
#if GDPM_LOG_LEVEL > ERROR
vlog(
fmt::format(GDPM_COLOR_LOG_ERROR + context.prefix + GDPM_COLOR_LOG_RESET, format),
fmt::format(GDPM_COLOR_LOG_ERROR "{}{}{}" GDPM_COLOR_LOG_RESET, prefix, format, suffix),
fmt::make_format_args(args...)
);
#endif
@ -120,30 +120,13 @@ namespace gdpm::log
template <typename S, typename...Args>
static constexpr void debug(const S& format, Args&&...args){
if(log::level < to_int(log::DEBUG))
return;
#if GDPM_LOG_LEVEL > DEBUG
set_prefix_if(std::format("[DEBUG {}] ", utils::timestamp()));
set_suffix_if("\n");
vlog(
fmt::format(GDPM_COLOR_LOG_DEBUG "[DEBUG {}] {}\n" GDPM_COLOR_LOG_RESET, utils::timestamp(), format),
// fmt::make_format_args<Args...>(args...)
fmt::make_format_args(args...)
);
#endif
}
template <typename S, typename...Args>
static constexpr void debug(const S& prefix, const S& format, Args&&...args){
#if GDPM_LOG_LEVEL > DEBUG
vlog(
fmt::format(GDPM_COLOR_LOG_DEBUG + prefix + GDPM_COLOR_LOG_RESET, format),
fmt::make_format_args(args...)
);
#endif
}
template <typename S, typename...Args>
static constexpr void debug(const context& context, const S& format, Args&&...args){
#if GDPM_LOG_LEVEL > DEBUG
vlog(
fmt::format(GDPM_COLOR_LOG_DEBUG + context.prefix + GDPM_COLOR_LOG_RESET, format),
fmt::format(GDPM_COLOR_LOG_DEBUG "{}{}{}" GDPM_COLOR_LOG_RESET, prefix, format, suffix),
fmt::make_format_args(args...)
);
#endif
@ -153,7 +136,6 @@ namespace gdpm::log
static constexpr void print(const S& format, Args&&...args){
vlog(
fmt::format("{}", format),
// fmt::make_format_args<Args...>(args...)
fmt::make_format_args(args...)
);
}
@ -162,7 +144,6 @@ namespace gdpm::log
static constexpr void println(const S& format, Args&&...args){
vlog(
fmt::format("{}\n", format),
// fmt::make_format_args<Args...>(args...)
fmt::make_format_args(args...)
);
}

View file

@ -8,6 +8,7 @@
#include "rest_api.hpp"
#include <cstdio>
#include <filesystem>
#include <functional>
#include <string>
#include <vector>
#include <rapidjson/document.h>
@ -48,11 +49,11 @@ namespace gdpm::package {
};
struct params {
args_t sub_commands;
args_t args;
var_opts opts;
string_list paths;
string_list input_files;
string remote_source = "origin";
string remote_source = "origin";
install_method_e install_method = GLOBAL_LINK_LOCAL;
};
@ -61,6 +62,7 @@ namespace gdpm::package {
using id_list = std::vector<size_t>;
using path = std::string;
using path_list = std::vector<path>;
using path_refs = std::vector<std::reference_wrapper<const path>>;
/*!
@brief Install a Godot package from the Asset Library in the current project.

View file

@ -16,7 +16,6 @@
#include <curl/curl.h>
namespace gdpm::package_manager {
extern remote::repository_map remote_sources;
extern CURL *curl;
extern CURLcode res;
extern config::context config;
@ -32,10 +31,13 @@ namespace gdpm::package_manager {
link,
clone,
clean,
config,
config_get,
config_set,
fetch,
sync,
remote,
remote_add,
remote_remove,
remote_list,
ui,
help,
none

View file

@ -1,6 +1,7 @@
#pragma once
#include "types.hpp"
#include <string>
#include <filesystem>
namespace gdpm::plugin{
struct info{
@ -8,9 +9,11 @@ namespace gdpm::plugin{
string description;
string version;
};
extern int init(int argc, char **argv);
extern int set_name(const char *name);
extern int set_description(const char *description);
extern int set_version(const char *version);
extern int finalize();
extern error initialize(int argc, char **argv);
extern error set_name(const char *name);
extern error set_description(const char *description);
extern error set_version(const char *version);
extern error finalize();
error load(std::filesystem::path path);
}

View file

@ -8,14 +8,8 @@
#include "config.hpp"
namespace gdpm::remote{
using repo_names = string_list;
using repo_urls = string_list;
using repository_map = string_map;
GDPM_DLL_EXPORT error handle_remote(config::context& config, const args_t& args, const var_opts& opts);
GDPM_DLL_EXPORT void set_repositories(config::context& context, const repository_map& repos);
GDPM_DLL_EXPORT void add_repositories(config::context& context, const repository_map& repos);
GDPM_DLL_EXPORT void remove_respositories(config::context& context, const repo_names& names);
GDPM_DLL_EXPORT void move_repository(config::context& context, int old_position, int new_position);
GDPM_DLL_EXPORT void print_repositories(const config::context& context);
GDPM_DLL_EXPORT error add_repository(config::context& config, const args_t& args);
GDPM_DLL_EXPORT error remove_respositories(config::context& config, const args_t& names);
GDPM_DLL_EXPORT void move_repository(config::context& config, int old_position, int new_position);
GDPM_DLL_EXPORT void print_repositories(const config::context& config);
}

View file

@ -101,5 +101,4 @@ namespace gdpm{
default: /*return*/ target = 0;
}
}
}
}

View file

@ -96,7 +96,12 @@ namespace gdpm::utils {
}
std::string readfile(const std::string& path);
void to_lower(std::string& s);
std::string to_lower(const std::string& s);
std::string trim(const std::string& s);
std::string trim_left(const std::string& s);
std::string trim_left(const std::string& s, const std::string& ref);
std::string trim_right(const std::string& s);
std::string trim_right(const std::string& s, const std::string& ref);
std::vector<std::string> parse_lines(const std::string& s);
std::string replace_first(std::string& s, const std::string& from, const std::string& to);
std::string replace_all(std::string& s, const std::string& from, const std::string& to);
@ -105,7 +110,7 @@ namespace gdpm::utils {
bool prompt_user_yn(const char *message);
void delay(std::chrono::milliseconds milliseconds = GDPM_REQUEST_DELAY);
std::string join(const std::vector<std::string>& target, const std::string& delimiter = ", ");
std::string join(const std::unordered_map<std::string, std::string>& target, const std::string& prefix = "", const std::string& delimiter = "\n");
// TODO: Add function to get size of decompressed zip
namespace json {