Refactored, simplified, and removed code; replaced cli header lib

- Reinitialized submodules
- Slightly restructed project
- Added more options to `compile.sh` script
- Added more utility functions
This commit is contained in:
David Allen 2023-06-10 19:22:21 -06:00
parent d34243db74
commit e48c54aa40
27 changed files with 564 additions and 3232 deletions

1
.gitignore vendored
View file

@ -13,3 +13,4 @@ vgcore.*
.vscode
config.json
*.txt
actions-runner/

8
.gitmodules vendored
View file

@ -1,6 +1,6 @@
[submodule "modules/doxygen-awesome-css"]
path = modules/doxygen-awesome-css
url = https://github.com/jothepro/doxygen-awesome-css.git
[submodule "modules/clipp"]
path = modules/clipp
url = https://github.com/GerHobbelt/clipp.git
url = https://github.com/GerHobbelt/clipp
[submodule "modules/doxygen-awesome-css"]
path = modules/doxygen-awesome-css
url = https://github.com/jothepro/doxygen-awesome-css/

2736
Doxyfile

File diff suppressed because it is too large Load diff

View file

@ -5,6 +5,7 @@ exe=gdpm
static=gdpm.static
tests=gdpm.tests
function link_exe(){
path=$1
link=$2
@ -19,6 +20,7 @@ function link_exe(){
fi
}
function strip(){
path=$1
if test -f "$path"
@ -28,11 +30,6 @@ function strip(){
fi
}
function generate_docs(){
# Generate documentation using `doxygen`
cd ${script_dir}/..
doxygen
}
function strip_all(){
# Strip debug symbols
@ -41,6 +38,7 @@ function strip_all(){
strip ${script_dir}/../build/gdpm.tests
}
function link_all(){
# Create symlinks to executables in build folder if necessary
link_exe $script_dir/../build/gdpm $script_dir/../bin/$exe
@ -48,6 +46,7 @@ function link_all(){
link_exe $script_dir/../build/gdpm.tests $script_dir/../bin/$tests
}
function clean(){
rm ${script_dir}/../bin/$exe
rm ${script_dir}/../bin/$static
@ -58,12 +57,49 @@ function clean(){
#meson configure build
#CXX=clang++ meson compile -C build -j$(proc)
PROCS=$(nproc)
CMAKE_COMMAND="cmake -B build -S . -D CMAKE_EXPORT_COMPILE_COMMANDS=1 -D CMAKE_BUILD_TYPE=Debug -G Ninja"
NINJA_COMMAND="ninja -C build -j ${PROCS}"
# CMake/ninja build system
function build_binaries(){
function build_all(){
mkdir -p build
cmake -B build -S . -D CMAKE_EXPORT_COMPILE_COMMANDS=1 -D CMAKE_BUILD_TYPE=Debug -G Ninja
ninja -C build -j $(nproc)
$CMAKE_COMMAND
$NINJA_COMMAND
}
function build_exe(){
mkdir -p build
$CMAKE_COMMAND \
--target gdpm \
--target gdpm.static
$NINJA_COMMAND
}
function build_libs(){
mkdir -p build
$CMAKE_COMMAND \\
--target gdpm-static \
--target gdpm-shared \
--target gdpm-http \
--target gdpm-restapi
$NINJA_COMMAND
}
function build_tests(){
mkdir -p build
$CMAKE_COMMAND --target gdpm.tests
$NINJA_COMMAND
}
function build_docs(){
# Generate documentation using `doxygen`
pushd ${script_dir}/docs/doxygen
doxygen
popd
}
@ -78,12 +114,16 @@ i=$(($#-1))
while [ $i -ge 0 ];
do
case "$1" in
-a|--all) build_binaries shift;;
-a|--all) build_all shift;;
--exe) build_exe shift;;
--libs) build_libs shift;;
--tests) build_tests shift;;
-d|--docs) build_docs shift;;
-c|--clean) clean shift;;
-d|--docs) generate_docs shift;;
-s|--strip) strip_all shift;;
-l|--link) link_all shift;;
--*) echo "Bad option: $1"
--procs=*) PROCS="${i#*=}" shift;;
-*|--*) echo "Bad option: $1"
esac
i=$((i-1))
done

View file

View file

@ -31,8 +31,8 @@ jobs:
- name: Build
run:
echo -e "Building executable and libraries...\n$PWD"
./bin/compile.sh
echo -e "Building executable, libraries, documentation, and symlinks...\n$PWD"
./bin/compile.sh --all --docs --link --strip
- name: Test
run:

1
include/clipp.h Symbolic link
View file

@ -0,0 +1 @@
../modules/clipp/include/clipp.h

View file

@ -2,6 +2,7 @@
#include "constants.hpp"
#include "error.hpp"
#include "package.hpp"
#include <rapidjson/document.h>
#include <string>
@ -9,27 +10,40 @@
#include <vector>
#include <unordered_map>
namespace gdpm::package{
struct info;
struct params;
}
namespace gdpm::config{
struct context{
string username;
string password;
string path;
string token;
string godot_version;
string packages_dir;
string tmp_dir;
string_map remote_sources;
size_t threads;
size_t timeout;
bool enable_sync;
size_t jobs = 1;
size_t timeout = 3000;
bool enable_sync = true;
bool enable_cache = true;
bool skip_prompt = false;
bool enable_file_logging;
bool clean_temporary;
int verbose;
package::info info;
rest_api::request_params api_params;
};
string to_json(const context& params);
error load(std::filesystem::path path, context& config, int verbose = 0);
error save(std::filesystem::path path, const context& config, int verbose = 0);
string to_json(const context& config, bool pretty_print = false);
error load(std::filesystem::path path, context& config);
error save(std::filesystem::path path, const context& 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);
extern context config;
}

View file

@ -12,7 +12,7 @@ namespace gdpm::constants{
const std::string LockfilePath(HomePath + ".config/gdpm/gdpm.lck");
const std::string LocalPackagesDir(HomePath + ".config/gdpm/packages");
const std::string TemporaryPath(HomePath + ".config/gdpm/tmp");
const std::string UserAgent("libcurl-agent/1.0");
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");
}

View file

@ -5,7 +5,7 @@
#include "package.hpp"
#include "types.hpp"
#include "result.hpp"
#include "config.hpp"
#include "rest_api.hpp"
#include <cstdio>
#include <filesystem>
#include <string>
@ -13,6 +13,10 @@
#include <rapidjson/document.h>
namespace gdpm::config{
struct context;
}
namespace gdpm::package {
struct info {
@ -42,11 +46,12 @@ namespace gdpm::package {
GLOBAL_ONLY = 2,
LOCAL_ONLY = 3
};
struct params {
int parallel_jobs = 1;
bool enable_cache = true;
bool enable_sync = true;
bool skip_prompt = false;
args_t sub_commands;
var_opts opts;
string_list paths;
string_list input_files;
string remote_source = "origin";
install_method_e install_method = GLOBAL_LINK_LOCAL;
};
@ -101,10 +106,10 @@ namespace gdpm::package {
GDPM_DLL_EXPORT error remove_all(const config::context& config, const params& params = package::params());
GDPM_DLL_EXPORT error update(const config::context& config, const title_list& package_titles, const params& params = package::params());
GDPM_DLL_EXPORT error search(const config::context& config, const title_list& package_titles, const params& params = package::params());
GDPM_DLL_EXPORT error list(const config::context& config, const args_t& args, const opts_t& opts);
GDPM_DLL_EXPORT error list(const config::context& config, const params& params = package::params());
GDPM_DLL_EXPORT error export_to(const path_list& paths);
GDPM_DLL_EXPORT error link(const config::context& config, const title_list& package_titles, const opts_t& opts);
GDPM_DLL_EXPORT error clone(const config::context& config, const title_list& package_titles, const opts_t& opts);
GDPM_DLL_EXPORT error link(const config::context& config, const title_list& package_titles, const params& params = package::params());
GDPM_DLL_EXPORT error clone(const config::context& config, const title_list& package_titles, const params& params = package::params());
GDPM_DLL_EXPORT void print_list(const rapidjson::Document& json);
@ -117,4 +122,6 @@ namespace gdpm::package {
/* Dependency Management API */
GDPM_DLL_EXPORT result_t<info_list> synchronize_database(const config::context& config, const title_list& package_titles);
GDPM_DLL_EXPORT result_t<info_list> resolve_dependencies(const config::context& config, const title_list& package_titles);
GDPM_DLL_EXPORT string to_json(const info& info, bool pretty_print = false);
}

View file

@ -21,16 +21,6 @@ namespace gdpm::package_manager {
extern CURLcode res;
extern config::context config;
struct cxxargs{
cxxopts::ParseResult result;
cxxopts::Options options;
};
struct exec_args{
var_args args;
var_opts opts;
};
enum class action_e{
install,
add,
@ -42,18 +32,17 @@ namespace gdpm::package_manager {
link,
clone,
clean,
config,
fetch,
sync,
remote,
ui,
help,
none
};
GDPM_DLL_EXPORT result_t<exec_args> initialize(int argc, char **argv);
GDPM_DLL_EXPORT int execute(const exec_args& in);
GDPM_DLL_EXPORT void finalize();
/* Auxiliary Functions */
GDPM_DLL_EXPORT cxxargs _parse_arguments(int argc, char **argv);
GDPM_DLL_EXPORT result_t<exec_args> _handle_arguments(const cxxargs& args);
GDPM_DLL_EXPORT error initialize(int argc, char **argv);
GDPM_DLL_EXPORT error parse_arguments(int argc, char **argv);
GDPM_DLL_EXPORT error finalize();
GDPM_DLL_EXPORT void run_command(action_e command, const var_args& args, const var_opts& opts);
}

View file

@ -12,7 +12,7 @@ namespace gdpm::remote{
using repo_urls = string_list;
using repository_map = string_map;
GDPM_DLL_EXPORT error _handle_remote(config::context& config, const args_t& args, const opts_t& opts);
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);

View file

@ -1,11 +1,17 @@
#pragma once
#include "constants.hpp"
#include "config.hpp"
#include "types.hpp"
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h>
namespace gdpm::config{
struct context;
}
namespace gdpm::rest_api{
// See GitHub reference: https://github.com/godotengine/godot-asset-library/blob/master/API.md
namespace endpoints{
@ -30,33 +36,34 @@ namespace gdpm::rest_api{
bool logout();
// bool change_password()
enum type_e { any, addon, project };
enum support_e { all, official, community, testing };
enum sort_e { none, rating, cost, name, updated };
enum type_e: int { any = 0, addon, project };
enum support_e: int { all = 0, official, community, testing };
enum sort_e: int { none = 0, rating, cost, name, updated };
struct context{
type_e type;
struct request_params{
int type ;
int category;
support_e support;
int support;
string filter;
string user;
string godot_version;
int max_results;
int page;
sort_e sort;
int sort;
bool reverse;
int verbose;
};
context make_from_config(const config::context& config);
context make_context(type_e type = GDPM_DEFAULT_ASSET_TYPE, int category = GDPM_DEFAULT_ASSET_CATEGORY, support_e support = GDPM_DEFAULT_ASSET_SUPPORT, const std::string& filter = GDPM_DEFAULT_ASSET_FILTER, const std::string& user = GDPM_DEFAULT_ASSET_USER, const std::string& godot_version = GDPM_DEFAULT_ASSET_GODOT_VERSION, int max_results = GDPM_DEFAULT_ASSET_MAX_RESULTS, int page = GDPM_DEFAULT_ASSET_PAGE, sort_e sort = GDPM_DEFAULT_ASSET_SORT, bool reverse = GDPM_DEFAULT_ASSET_REVERSE, int verbose = GDPM_DEFAULT_ASSET_VERBOSE);
request_params make_from_config(const config::context& config);
request_params make_request_params(type_e type = GDPM_DEFAULT_ASSET_TYPE, int category = GDPM_DEFAULT_ASSET_CATEGORY, support_e support = GDPM_DEFAULT_ASSET_SUPPORT, const std::string& filter = GDPM_DEFAULT_ASSET_FILTER, const std::string& user = GDPM_DEFAULT_ASSET_USER, const std::string& godot_version = GDPM_DEFAULT_ASSET_GODOT_VERSION, int max_results = GDPM_DEFAULT_ASSET_MAX_RESULTS, int page = GDPM_DEFAULT_ASSET_PAGE, sort_e sort = GDPM_DEFAULT_ASSET_SORT, bool reverse = GDPM_DEFAULT_ASSET_REVERSE, int verbose = GDPM_DEFAULT_ASSET_VERBOSE);
string to_json(const rapidjson::Document& doc);
string to_string(type_e type);
string to_string(support_e support);
string to_string(sort_e sort);
void _print_params(const context& params);
void _print_params(const request_params& params);
rapidjson::Document _parse_json(const string& r, int verbose = 0);
string _prepare_request(const string& url, const context& context);
string _prepare_request(const string& url, const request_params& context);
bool register_account(const string& username, const string& password, const string& email);
bool login(const string& username, const string& password);
@ -64,8 +71,8 @@ namespace gdpm::rest_api{
rapidjson::Document configure(const string& url = constants::HostUrl, type_e type = any, int verbose = 0);
rapidjson::Document get_assets_list(const string& url = constants::HostUrl, type_e type = GDPM_DEFAULT_ASSET_TYPE, int category = GDPM_DEFAULT_ASSET_CATEGORY, support_e support = GDPM_DEFAULT_ASSET_SUPPORT, const string& filter = GDPM_DEFAULT_ASSET_FILTER, const std::string& user = GDPM_DEFAULT_ASSET_USER, const std::string& godot_version = GDPM_DEFAULT_ASSET_GODOT_VERSION, int max_results = GDPM_DEFAULT_ASSET_MAX_RESULTS, int page = GDPM_DEFAULT_ASSET_PAGE, sort_e sort = GDPM_DEFAULT_ASSET_SORT, bool reverse = GDPM_DEFAULT_ASSET_REVERSE, int verbose = GDPM_DEFAULT_ASSET_VERBOSE);
rapidjson::Document get_assets_list(const string& url, const context& params = {});
rapidjson::Document get_asset(const string& url, int asset_id, const context& params = {});
rapidjson::Document get_assets_list(const string& url, const request_params& params = {});
rapidjson::Document get_asset(const string& url, int asset_id, const request_params& params = {});
bool delete_asset(int asset_id); // ...for moderators
bool undelete_asset(int asset_id); // ...for moderators
bool set_support_level(int asset_id); // ...for moderators

View file

@ -87,6 +87,7 @@ namespace gdpm{
return o;
}
template <typename T>
constexpr void get(const var& v, T& target){
switch(v.index()){

View file

@ -9,13 +9,15 @@
#include <string>
#include <fstream>
#include <chrono>
#include <unordered_map>
#include <vector>
#include <thread>
#include <set>
#include <fmt/format.h>
#include <fmt/chrono.h>
namespace gdpm::utils{
namespace gdpm::utils {
using namespace std::chrono_literals;
@ -103,5 +105,11 @@ 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 = ", ");
// TODO: Add function to get size of decompressed zip
namespace json {
std::string from_array(const std::set<std::string>& a, const std::string& prefix);
std::string from_object(const std::unordered_map<std::string, std::string>& m, const std::string& prefix, const std::string& spaces);
}
}

@ -1 +1 @@
Subproject commit 2c32b2f1f7cc530b1ec1f62c92f698643bb368db
Subproject commit 02783b6782ebedbb2bebc2e6ceda738ee51c7df2

@ -1 +1 @@
Subproject commit a7f7891706c656903326f79baf74beb2b711688d
Subproject commit df83fbf22cfff76b875c13d324baf584c74e96d0

View file

@ -4,6 +4,7 @@
#include "utils.hpp"
#include "constants.hpp"
#include "error.hpp"
#include "types.hpp"
// RapidJSON
#include <rapidjson/ostreamwrapper.h>
@ -31,43 +32,30 @@
namespace gdpm::config{
context config;
std::string to_json(const context& params){
auto _build_json_array = [](std::set<std::string> a){
std::string o{"["};
for(const std::string& src : a)
o += "\"" + src + "\",";
if(o.back() == ',')
o.pop_back();
o += "]";
return o;
};
auto _build_json_object = [](const string_map& m){
string o{"{"};
std::for_each(m.begin(), m.end(), [&o](const string_pair& p){
o += std::format("\n\"{}\": \"{}\",", p.first, p.second);
});
if(o.back() == ',')
o.pop_back();
o += "}";
return o;
};
string to_json(
const context& config,
bool pretty_print
){
/* Build a JSON string to pass to document */
string prefix = (pretty_print) ? "\n\t" : "";
string spaces = (pretty_print) ? " " : "";
string json{
"{\"username\":\"" + params.username + "\","
+ "\"password\":\"" + params.password + "\","
+ "\"path\":\"" + params.path + "\","
+ "\"token\":\"" + params.token + "\","
+ "\"godot_version\":\"" + params.godot_version + "\","
+ "\"packages_dir\":\"" + params.packages_dir + "\","
+ "\"tmp_dir\":\"" + params.tmp_dir + "\","
+ "\"remote_sources\":" + _build_json_object(params.remote_sources) + ","
+ "\"threads\":" + fmt::to_string(params.threads) + ","
+ "\"timeout\":" + fmt::to_string(params.timeout) + ","
+ "\"enable_sync\":" + fmt::to_string(params.enable_sync) + ","
+ "\"enable_file_logging\":" + fmt::to_string(params.enable_file_logging)
+ "}"
"{" + prefix + "\"username\":" + spaces + "\"" + config.username + "\","
+ prefix + "\"password\":" + spaces + "\"" + config.password + "\","
+ prefix + "\"path\":"+ spaces + "\"" + config.path + "\","
+ prefix + "\"token\":" + spaces + "\"" + config.token + "\","
+ prefix + "\"godot_version\":" + spaces + "\"" + config.info.godot_version + "\","
+ prefix + "\"packages_dir\":" + spaces + "\"" + config.packages_dir + "\","
+ prefix + "\"tmp_dir\":" + spaces + "\"" + config.tmp_dir + "\","
+ prefix + "\"remote_sources\":" + spaces + utils::json::from_object(config.remote_sources, prefix, spaces) + ","
+ prefix + "\"threads\":" + spaces + fmt::to_string(config.jobs) + ","
+ prefix + "\"timeout\":" + spaces + fmt::to_string(config.timeout) + ","
+ prefix + "\"enable_sync\":" + spaces + fmt::to_string(config.enable_sync) + ","
+ prefix + "\"enable_file_logging\":" + spaces + fmt::to_string(config.enable_file_logging)
+ "\n}"
};
return json;
}
@ -75,16 +63,15 @@ namespace gdpm::config{
error load(
std::filesystem::path path,
context& config,
int verbose
context& config
){
std::fstream file;
file.open(path, std::ios::in);
if(!file){
if(verbose)
if(config.verbose)
log::info("No configuration file found. Creating a new one.");
config = make_context();
save(config.path, config, verbose);
save(config.path, config);
return error();
}
else if(file.is_open()){
@ -100,7 +87,7 @@ namespace gdpm::config{
while(std::getline(file, line))
contents += line + "\n";
if(verbose > 0)
if(config.verbose > 0)
log::info("Loading configuration file...\n{}", contents.c_str());
Document doc;
@ -164,10 +151,10 @@ namespace gdpm::config{
config.password = _get_value_string(doc, "password");
config.path = _get_value_string(doc, "path");
config.token = _get_value_string(doc, "token");
config.godot_version = _get_value_string(doc, "godot_version");
config.info.godot_version = _get_value_string(doc, "godot_version");
config.packages_dir = _get_value_string(doc, "packages_dir");
config.tmp_dir = _get_value_string(doc, "tmp_dir");
config.threads = _get_value_int(doc, "threads");
config.jobs = _get_value_int(doc, "threads");
config.enable_sync = _get_value_int(doc, "enable_sync");
config.enable_file_logging = _get_value_int(doc, "enable_file_logging");
}
@ -177,14 +164,13 @@ namespace gdpm::config{
error save(
std::filesystem::path path,
const context& config,
int verbose
const context& config
){
using namespace rapidjson;
/* Build a JSON string to pass to document */
string json = to_json(config);
if(verbose > 0)
if(config.verbose > 0)
log::info("Saving configuration file...\n{}", json.c_str());
/* Dump JSON config to file */
@ -200,6 +186,52 @@ namespace gdpm::config{
}
error handle_config(
config::context& config,
const args_t& args,
const var_opts& opts
){
std::for_each(opts.begin(), opts.end(), [](const var_opt& p){
log::println("opt: {}", p.first);
if (p.second.index() == STRING_LIST){
string_list p_list = get<string_list>(p.second);
std::for_each(p_list.begin(), p_list.end(), [](const string& o){
log::println("\t{}", o);
});
}
});
log::println("opts count: {}", opts.size());
if(opts.contains("--username")){
string_list v = get<string_list>(opts.at("--username"));
log::println("username: {}", v[0]);
if(v.empty()){
log::println("username: {}", v[0]);
}
else
config.username = v[0];
}
if(opts.contains("godot_version")){
string_list v = get<string_list>(opts.at("godot-version"));
if(v.empty()){
// print godot-version
}
else
config.info.godot_version = get<string_list>(opts.at("godot-version"))[0];
}
if(opts.contains("threads")){
string_list v = get<string_list>(opts.at("threads"));
if(v.empty()){
}
}
save(config.path, config);
return error();
}
context make_context(
const string& username,
const string& password,
@ -220,15 +252,17 @@ namespace gdpm::config{
.password = password,
.path = path,
.token = token,
.godot_version = godot_version,
.packages_dir = (packages_dir.empty()) ? string(getenv("HOME")) + ".gdpm" : packages_dir,
.tmp_dir = tmp_dir,
.remote_sources = remote_sources,
.threads = threads,
.jobs = threads,
.timeout = timeout,
.enable_sync = enable_sync,
.enable_file_logging = enable_file_logging,
.verbose = verbose
.verbose = verbose,
.info = {
.godot_version = godot_version,
},
};
return config;
}
@ -252,4 +286,8 @@ namespace gdpm::config{
return error;
}
void print(const context& config){
log::println("{}", to_json(config, true));
}
}

View file

@ -11,9 +11,8 @@ int main(int argc, char **argv){
using namespace gdpm;
using namespace gdpm::package_manager;
result_t <exec_args> r_input = initialize(argc, argv);
exec_args input = r_input.unwrap_unsafe();
execute(input);
error error = initialize(argc, argv);
parse_arguments(argc, argv);
finalize();
return 0;
}

View file

@ -85,7 +85,7 @@ namespace gdpm::package{
}
log::println("");
if(!params.skip_prompt){
if(!config.skip_prompt){
if(!utils::prompt_user_yn("Do you want to install these packages? (y/n)"))
return error();
}
@ -103,7 +103,7 @@ namespace gdpm::package{
/* Try and obtain all requested packages. */
std::vector<string_pair> dir_pairs;
task_list tasks;
rest_api::context rest_api_params = rest_api::make_from_config(config);
rest_api::request_params rest_api_params = rest_api::make_from_config(config);
for(auto& p : p_found){ // TODO: Execute each in parallel using coroutines??
/* Check if a remote source was provided. If not, then try to get packages
@ -131,22 +131,6 @@ namespace gdpm::package{
}
else{
log::error("Not a valid package.");
/* Package for in cache so no remote request. Still need to populate RapidJson::Document to write to package.json.
NOTE: This may not be necessary at all!
*/
// doc["asset_id"].SetUint64(p.asset_id
// doc["type"].SetString(p.type, doc.GetAllocator());
// doc["title"].SetString(p.title, doc.GetAllocator());
// doc["author"].SetString(p.author, doc.GetAllocator());
// doc["author_id"].SetUint64(p.author_id);
// doc["version"].SetString(p.version, doc.GetAllocator());
// doc["category"].SetString(p.category, doc.GetAllocator());
// doc["godot_version"].SetString(p.godot_version, doc.GetAllocator());
// doc["cost"].SetString(p.cost, doc.GetAllocator());
// doc["description"].SetString(p.description, doc.GetAllocator());
// doc["support_level"].SetString(p.support_level, doc.GetAllocator());
// doc["download_url"].SetString(p.download_url, doc.GetAllocator());
// doc["download_hash"].SetString(p.download_hash, doc.GetAllocator;
}
/* Set directory and temp paths for storage */
@ -263,7 +247,7 @@ namespace gdpm::package{
log::print(" {} ", p.title);
log::println("");
if(!params.skip_prompt){
if(!config.skip_prompt){
if(!utils::prompt_user_yn("Do you want to remove these packages? (y/n)"))
return error();
}
@ -336,7 +320,7 @@ namespace gdpm::package{
using namespace rapidjson;
/* If no package titles provided, update everything and then exit */
rest_api::context rest_api_params = rest_api::make_from_config(config);
rest_api::request_params rest_api_params = rest_api::make_from_config(config);
if(package_titles.empty()){
std::string url{constants::HostUrl};
url += rest_api::endpoints::GET_AssetId;
@ -370,7 +354,7 @@ namespace gdpm::package{
}
}
if(!params.skip_prompt){
if(!config.skip_prompt){
if(!utils::prompt_user_yn("Do you want to update the following packages? (y/n)"))
return error();
}
@ -397,13 +381,13 @@ namespace gdpm::package{
return error();
}
rest_api::context rest_api_params = rest_api::make_from_config(config);
rest_api::request_params rest_api_params = rest_api::make_from_config(config);
for(const auto& p_title : package_titles){
using namespace rapidjson;
rest_api_params.filter = http::url_escape(p_title);
rest_api_params.verbose = config.verbose;
rest_api_params.godot_version = config.godot_version;
rest_api_params.godot_version = config.info.godot_version;
rest_api_params.max_results = 200;
std::string request_url{constants::HostUrl};
@ -427,13 +411,12 @@ namespace gdpm::package{
error list(
const config::context& config,
const args_t& args,
const opts_t& opts
const package::params& params
){
using namespace rapidjson;
using namespace std::filesystem;
string show((!args.empty()) ? args[0] : "");
string show((!params.sub_commands.empty()) ? params.sub_commands[0] : "");
if(show.empty() || show == "packages"){
result_t r_installed = cache::get_installed_packages();
info_list p_installed = r_installed.unwrap_unsafe();
@ -493,13 +476,13 @@ namespace gdpm::package{
error link(
const config::context& config,
const title_list& package_titles,
const opts_t& opts
const package::params& params
){
using namespace std::filesystem;
path_list paths = {};
if(opts.contains("path")){
paths = opts.at("path");
if(params.opts.contains("path")){
paths = get<path_list>(params.opts.at("path"));
}
if(paths.empty()){
@ -567,11 +550,11 @@ namespace gdpm::package{
error clone(
const config::context& config,
const title_list& package_titles,
const opts_t& paths
const package::params& params
){
using namespace std::filesystem;
if(paths.empty()){
if(params.opts.empty()){
error error(
constants::error::PATH_NOT_DEFINED,
"No path set. Use '--path' option to set a path."
@ -609,10 +592,10 @@ namespace gdpm::package{
}
/* Get the storage paths for all packages to create clones */
path_list paths = get<path_list>(params.opts.at("--path"));
const path package_dir{config.packages_dir};
for(const auto& p : p_found){
for(const auto& path_list : paths){
for(const auto& path : path_list.second){
for(const auto& path : paths){
log::info("Cloning \"{}\" package to {}", p.title, path + "/" + p.title);
std::filesystem::path from{config.packages_dir + "/" + p.title};
std::filesystem::path to{path + "/" + p.title};
@ -623,7 +606,6 @@ namespace gdpm::package{
std::filesystem::copy(from, to, copy_options::update_existing | copy_options::recursive);
}
}
}
return error();
}
@ -705,12 +687,11 @@ namespace gdpm::package{
if(o.count(k)){ p = std::get<T>(o.at(k)); }
}
params make_params(const var_args& args, const var_opts& opts){
params make_params(
const var_args& args,
const var_opts& opts
){
params p;
set_if_key_exists<int>(opts, "jobs", p.parallel_jobs);
set_if_key_exists(opts, "cache", p.enable_cache);
set_if_key_exists(opts, "sync", p.enable_sync);
set_if_key_exists(opts, "skip-prompt", p.skip_prompt);
set_if_key_exists(opts, "remote-source", p.remote_source);
// set_if_key_exists(opts, "install-method", p.install_method);
@ -724,13 +705,13 @@ namespace gdpm::package{
){
using namespace rapidjson;
rest_api::context rest_api_params = rest_api::make_from_config(config);
rest_api::request_params rest_api_params = rest_api::make_from_config(config);
rest_api_params.page = 0;
int page = 0;
int page_length = 0;
// int total_pages = 0;
int total_items = 0;
int items_left = 0;
// int total_pages = 0;
log::info("Sychronizing database...");
do{
@ -824,4 +805,13 @@ namespace gdpm::package{
return result_t(p_deps, error());
}
string to_json(
const info& info,
bool pretty_pretty
){
string json("");
return json;
}
}

View file

@ -21,6 +21,7 @@
#include <fmt/printf.h>
#include <rapidjson/document.h>
#include <cxxopts.hpp>
#include "clipp.h"
#include <rapidjson/ostreamwrapper.h>
#include <rapidjson/prettywriter.h>
@ -35,24 +36,22 @@
*/
namespace gdpm::package_manager{
remote::repository_map remote_sources;
CURL *curl;
CURLcode res;
config::context config;
rest_api::context api_params;
remote::repository_map remote_sources;
action_e action;
string_list packages;
// opts_t opts;
bool skip_prompt = false;
bool clean_tmp_dir = false;
int priority = -1;
result_t<exec_args> initialize(int argc, char **argv){
error initialize(int argc, char **argv){
// curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
config = config::make_context();
api_params = rest_api::make_context();
action = action_e::none;
/* Check for config and create if not exists */
@ -62,287 +61,190 @@ namespace gdpm::package_manager{
error error = config::load(config.path, config);
if(error.has_occurred()){
log::error(error);
return error;
}
/* Create the local databases if it doesn't exist already */
error = cache::create_package_database();
if(error.has_occurred()){
log::error(error);
return error;
}
/* Run the rest of the program then exit */
cxxargs args = _parse_arguments(argc, argv);
return _handle_arguments(args);
return error;
}
int execute(const exec_args& in){
run_command(action, in.args, in.opts);
if(clean_tmp_dir)
package::clean_temporary(config, packages);
return 0;
}
void finalize(){
error finalize(){
curl_easy_cleanup(curl);
error error = config::save(config.path, config);
if(error()){
}
return error;
}
cxxargs _parse_arguments(int argc, char **argv){
/* Parse command-line arguments using cxxopts */
cxxopts::Options options(
argv[0],
"Experimental package manager made for managing assets for the Godot game engine through the command-line.\n"
error parse_arguments(int argc, char **argv){
/* Replace cxxopts with clipp */
action_e action = action_e::none;
package::title_list package_titles;
string_list input;
package::params params;
args_t args;
var_opts opts;
/* Set global options */
auto configOpt = clipp::option("--config-path").set(config.path)% "set config path";
auto fileOpt = clipp::option("--file", "-f").set(input) % "read file as input";
auto pathOpt = clipp::option("--path").set(params.paths) % "specify a path to use with command";
auto typeOpt = clipp::option("--type").set(config.info.type) % "set package type (any|addon|project)";
auto sortOpt = clipp::option("--sort").set(config.api_params.sort) % "sort packages in order (rating|cost|name|updated)";
auto supportOpt = clipp::option("--support").set(config.api_params.support) % "set the support level for API (all|official|community|testing)";
auto maxResultsOpt = clipp::option("--max-results").set(config.api_params.max_results) % "set the request max results";
auto godotVersionOpt = clipp::option("--godot-version").set(config.api_params.godot_version) % "set the request Godot version";
auto packageDirOpt = clipp::option("--package-dir").set(config.packages_dir) % "set the global package location";
auto tmpDirOpt = clipp::option("--tmp-dir").set(config.tmp_dir) % "set the temporary download location";
auto timeoutOpt = clipp::option("--timeout").set(config.timeout) % "set the request timeout";
auto verboseOpt = clipp::option("--verbose", "-v").set(config.verbose) % "show verbose output";
/* Set the options */
auto cleanOpt = clipp::option("--clean").set(config.clean_temporary) % "enable/disable cleaning temps";
auto parallelOpt = clipp::option("--jobs").set(config.jobs) % "set number of parallel jobs";
auto cacheOpt = clipp::option("--enable-cache").set(config.enable_cache) % "enable/disable local caching";
auto syncOpt = clipp::option("--enable-sync").set(config.enable_sync) % "enable/disable remote syncing";
auto skipOpt = clipp::option("--skip-prompt").set(config.skip_prompt) % "skip the y/n prompt";
auto remoteOpt = clipp::option("--remote").set(params.remote_source) % "set remote source to use";
auto packageValues = clipp::values("packages", package_titles);
auto requiredPath = clipp::required("--path", input);
auto installCmd = (
clipp::command("install")
.set(action, action_e::install)
.doc("Install packages from asset library"),
clipp::values("packages", package_titles),
clipp::option("--godot-version") & clipp::value("version", config.info.godot_version),
cleanOpt, parallelOpt, syncOpt, skipOpt, remoteOpt
);
auto addCmd = (
clipp::command("add").set(action, action_e::add)
.doc("Add a package to a local project"),
packageValues
);
auto removeCmd = (
clipp::command("remove")
.set(action, action_e::remove)
.doc("Remove a package from local project"),
packageValues
);
auto updateCmd = (
clipp::command("update")
.set(action, action_e::update)
.doc("Update package(s)"),
packageValues
);
auto searchCmd = (
clipp::command("search")
.set(action, action_e::search)
.doc("Search for package(s)"),
packageValues
);
auto exportCmd = (
clipp::command("export")
.set(action, action_e::p_export)
.doc("Export package list"),
clipp::values("path", input)
);
auto listCmd = (
clipp::command("list")
.set(action, action_e::list)
.doc("Show installed packages")
);
auto linkCmd = (
clipp::command("link")
.set(action, action_e::link)
.doc("Create symlink packages to project"),
packageValues,
requiredPath
);
auto cloneCmd = (
clipp::command("clone")
.set(action, action_e::clone)
.doc("Clone packages to project"),
packageValues,
requiredPath
);
auto cleanCmd = (
clipp::command("clean")
.set(action, action_e::clean)
.doc("Clean temporary files"),
packageValues
);
auto configCmd = (
clipp::command("config")
.set(action, action_e::config)
.doc("Set/get config properties")
);
auto fetchCmd = (
clipp::command("fetch")
.set(action, action_e::fetch)
.doc("Fetch asset metadata from remote source")
);
auto remoteCmd = (
clipp::command("remote")
.set(action, action_e::remote)
.doc("Manage remote sources")
.required("subcommand")
);
auto uiCmd = (
clipp::command("ui")
.set(action, action_e::ui)
.doc("Show the UI")
);
auto helpCmd = (
clipp::command("help")
.set(action, action_e::help)
);
options.allow_unrecognised_options();
options.custom_help("[COMMAND] [OPTIONS...]");
options.add_options("Command")
("command", "Specify the input parameters", cxxopts::value<string>())
("positional", "", cxxopts::value<string_list>())
("install", "Install package or packages.", cxxopts::value<string_list>()->implicit_value(""), "<packages...>")
("remove", "Remove a package or packages.", cxxopts::value<string_list>()->implicit_value(""), "<packages...>")
("update", "Update a package or packages. This will update all packages if no argument is provided.", cxxopts::value<string_list>()->implicit_value(""), "<packages...>")
("search", "Search for a package or packages.", cxxopts::value<string_list>(), "<packages...>")
("export", "Export list of packages", cxxopts::value<string>()->default_value("./gdpm-packages.txt"))
("list", "Show list of installed packages.")
("link", "Create a symlink (or shortcut) to target directory. Must be used with the `--path` argument.", cxxopts::value<string_list>(), "<packages...>")
("clone", "Clone packages into target directory. Must be used with the `--path` argument.", cxxopts::value<string_list>(), "<packages...>")
("cache", "Caching operations", cxxopts::value<string_list>())
("clean", "Clean temporary downloaded files.")
("fetch", "Fetch asset data from remote sources.")
("remote", "Set a source repository.", cxxopts::value<string>()->default_value(constants::AssetRepo), "<url>")
("h,help", "Print this message and exit.")
("version", "Show the current version and exit.")
;
options.parse_positional({"command", "positional"});
options.positional_help("");
options.add_options("Other")
("c,config", "Set the config file path.", cxxopts::value<string>())
("f,file", "Read file to install or remove packages.", cxxopts::value<string>(), "<path>")
("path", "Specify a path to use with a command", cxxopts::value<string_list>())
("type", "Set package type (any|addon|project).", cxxopts::value<string>())
("sort", "Sort packages in order (rating|cost|name|updated).", cxxopts::value<string>())
("support", "Set the support level for API (all|official|community|testing).")
("max-results", "Set the max results to return from search.", cxxopts::value<int>()->default_value("500"), "<int>")
("godot-version", "Set the Godot version to include in request.", cxxopts::value<string>())
("package-dir", "Set the local package storage location.", cxxopts::value<string>())
("tmp-dir", "Set the local temporary storage location.", cxxopts::value<string>())
("timeout", "Set the amount of time to wait for a response.", cxxopts::value<size_t>())
("no-sync", "Disable synchronizing with remote.", cxxopts::value<bool>()->implicit_value("true")->default_value("false"))
("y,no-prompt", "Bypass yes/no prompt for installing or removing packages.")
("v,verbose", "Show verbose output.", cxxopts::value<int>()->implicit_value("1")->default_value("0"), "0-5")
;
auto result = options.parse(argc, argv);
return {result, options};
}
auto cli = (
(installCmd | addCmd | removeCmd | updateCmd | searchCmd | exportCmd |
listCmd | linkCmd | cloneCmd | cleanCmd | configCmd | fetchCmd |
remoteCmd | uiCmd | helpCmd)
);
template <typename T>
void insert(
var_opts& opts,
const cxxopts::ParseResult& result,
const string& key
){
opts.insert(var_opt(key, result[key].as<T>()));
};
result_t<exec_args> _handle_arguments(const cxxargs& args){
const auto& result = args.result;
const auto& options = args.options;
exec_args in;
// auto get_opt = [](const string& key){
// result[key].as<T>();
// }
// auto get_opt = [&result]<typename V>(const string& key){
// return opt(key, result[key].as<V>());
// };
/* Set option variables first to be used in functions below. */
if(result.count("search")){
in.args = result["search"].as<var_args>();
/* Make help output */
string map_page_format("");
auto man_page = clipp::make_man_page(cli);
std::for_each(man_page.begin(), man_page.end(),
[&map_page_format](const clipp::man_page::section& s){
map_page_format += s.title() + "\n";
map_page_format += s.content() + "\n";
}
if(result.count("help")){
log::println("{}", options.help());
}
if(result.count("config")){
config.path = result["config"].as<string>();
insert<string>(in.opts, result, "config");
config::load(config.path, config);
log::info("Config: {}", config.path);
}
if(result.count("remote")){
string sub_command = result["remote"].as<string>();
log::print("sub command: {}", sub_command);
if(sub_command == "add"){
string argv = result.arguments_string();
log::println("argv: {}", argv);
remote::add_repositories(config, {});
}
else if(sub_command == "remove"){
remote::remove_respositories(config, {});
log::println("argv: {}");
}
}
if(result.count("file")){
string path = result["file"].as<string>();
string contents = utils::readfile(path);
packages = utils::parse_lines(contents);
insert<string>(in.opts, result, "file");
}
if(result.count("path")){
insert<string_list>(in.opts, result, "path");
}
if(result.count("sort")){
string r = result["sort"].as<string>();
rest_api::sort_e sort = rest_api::sort_e::none;
if(r == "none") sort = rest_api::sort_e::none;
else if(r == "rating") sort = rest_api::sort_e::rating;
else if(r == "cost") sort = rest_api::sort_e::cost;
else if(r == "name") sort = rest_api::sort_e::name;
else if(r == "updated") sort = rest_api::sort_e::updated;
api_params.sort = sort;
in.opts.insert(var_opt("sort", r));
}
if(result.count("type")){
string r = result["type"].as<string>();
rest_api::type_e type = rest_api::type_e::any;
if(r == "any") type = rest_api::type_e::any;
else if(r == "addon") type = rest_api::type_e::addon;
else if(r == "project") type = rest_api::type_e::project;
api_params.type = type;
in.opts.insert(var_opt("type", r));
}
if(result.count("support")){
string r = result["support"].as<string>();
rest_api::support_e support = rest_api::support_e::all;
if(r == "all") support = rest_api::support_e::all;
else if(r == "official") support = rest_api::support_e::official;
else if(r == "community") support = rest_api::support_e::community;
else if(r == "testing") support = rest_api::support_e::testing;
api_params.support = support;
in.opts.insert(var_opt("support", r));
}
if(result.count("max-results")){
api_params.max_results = result["max-results"].as<int>();
insert<int>(in.opts, result, "max-results");
}
if(result.count("godot-version")){
config.godot_version = result["godot-version"].as<string>();
insert<string>(in.opts, result, "godot-version");
}
if(result.count("timeout")){
config.timeout = result["timeout"].as<size_t>();
insert<size_t>(in.opts, result, "timeout");
}
if(result.count("no-sync")){
config.enable_sync = false;
in.opts.insert(var_opt("sync", "disabled"));
}
if(result.count("package-dir")){
config.packages_dir = result["package-dir"].as<string>();
insert<string>(in.opts, result, "package-dir");
}
if(result.count("tmp-dir")){
config.tmp_dir = result["tmp-dir"].as<string>();
insert<string>(in.opts, result, "tmp-dir");
}
if(result.count("yes")){
skip_prompt = true;
in.opts.insert(opt("skip-prompt", true));
}
if(result.count("link")){
packages = result["link"].as<string_list>();
insert<string_list>(in.opts, result, "link");
}
if(result.count("clone")){
packages = result["clone"].as<string_list>();
insert<string_list>(in.opts, result, "clone");
}
if(result.count("clean")){
in.opts.insert(opt("clean", true));
clean_tmp_dir = true;
}
config.verbose = 0;
config.verbose += result["verbose"].as<int>();
insert<int>(in.opts, result, "verbose");
string json = to_json(config);
if(config.verbose > 0){
log::println("Verbose set to level {}", config.verbose);
log::println("{}", json);
}
if(!result.count("command")){
log::error("Command required. See \"help\" for more information.");
return result_t(in, error());
}
string sub_command = result["command"].as<string>();
if(result.count("positional")){
string_list _argv = result["positional"].as<string_list>();
args_t argv{_argv.begin(), _argv.end()};
if(!argv.empty()){
for(const auto& arg : argv){
in.args.emplace_back(arg);
}
}
}
/* Catch arguments passed with dashes */
if(sub_command == "install") action = action_e::install;
else if(sub_command == "add") action = action_e::add;
else if(sub_command == "remove") action = action_e::remove;
else if(sub_command == "update") action = action_e::update;
else if(sub_command == "search") action = action_e::search;
else if(sub_command == "export") action = action_e::p_export;
else if(sub_command == "list") action = action_e::list;
else if(sub_command == "link") action = action_e::link;
else if(sub_command == "clone") action = action_e::clone;
else if(sub_command == "clean") action = action_e::clean;
else if(sub_command == "sync") action = action_e::sync;
else if(sub_command == "remote") action = action_e::remote;
else if(sub_command == "help"){ action = action_e::help;
log::println("{}", options.help());
}
else{
log::error("Unrecognized command. Try 'gdpm help' for more info.");
}
return result_t(in, error());
}
/* Used to run the command AFTER parsing and setting all command line args. */
void run_command(action_e c, const var_args& args, const var_opts& opts){
package::params params = package::make_params(args, opts);
string_list args_list = unwrap(args);
opts_t opts_list = unwrap(opts);
params.skip_prompt = skip_prompt;
switch(c){
case action_e::install: package::install(config, args_list, params); break;
case action_e::remove: package::remove(config, args_list, params); break;
case action_e::update: package::update(config, args_list, params); break;
case action_e::search: package::search(config, args_list, params); break;
case action_e::p_export: package::export_to(args_list); break;
case action_e::list: package::list(config, args_list, opts_list); break;
);
if(clipp::parse(argc, argv, cli)){
switch(action){
case action_e::install: package::install(config, package_titles, params); break;
case action_e::add: break;
case action_e::remove: package::remove(config, package_titles, params); break;
case action_e::update: package::update(config, package_titles, params); break;
case action_e::search: package::search(config, package_titles, params); break;
case action_e::p_export: package::export_to(input); break;
case action_e::list: package::list(config, params); break;
/* ...opts are the paths here */
case action_e::link: package::link(config, args_list, opts_list); break;
case action_e::clone: package::clone(config, args_list, opts_list); break;
case action_e::clean: package::clean_temporary(config, args_list); break;
case action_e::sync: package::synchronize_database(config, args_list); break;
case action_e::remote: remote::_handle_remote(config, args_list, opts_list); break;
case action_e::help: /* ...runs in handle_arguments() */ break;
case action_e::link: package::link(config, package_titles, params); break;
case action_e::clone: package::clone(config, package_titles, params); break;
case action_e::clean: package::clean_temporary(config, package_titles); break;
case action_e::config: config::handle_config(config, package_titles, opts); break;
case action_e::fetch: package::synchronize_database(config, package_titles); break;
case action_e::sync: package::synchronize_database(config, package_titles); break;
case action_e::remote: remote::handle_remote(config, args, opts); break;
case action_e::ui: log::info("ui not implemented yet"); break;
case action_e::help: log::println("{}", map_page_format); break;
case action_e::none: /* ...here to run with no command */ break;
}
} else {
log::println("{}", map_page_format);
}
return error();
}
} // namespace gdpm::package_manager

View file

@ -6,10 +6,10 @@
#include <readline/readline.h>
namespace gdpm::remote{
error _handle_remote(
error handle_remote(
config::context& config,
const args_t& args,
const opts_t& opts
const var_opts& opts
){
/* Check if enough arguments are supplied */
size_t argc = args.size();

View file

@ -1,4 +1,5 @@
#include "config.hpp"
#include "rest_api.hpp"
#include "constants.hpp"
#include "http.hpp"
@ -16,15 +17,27 @@
namespace gdpm::rest_api{
context make_from_config(const config::context& config){
context params = make_context();
params.godot_version = config.godot_version;
request_params make_from_config(const config::context& config){
request_params params = make_request_params();
params.godot_version = config.info.godot_version;
params.verbose = config.verbose;
return params;
}
context make_context(type_e type, int category, support_e support, const std::string& filter, const std::string& user, const std::string& godot_version, int max_results, int page, sort_e sort, bool reverse, int verbose){
context params{
request_params make_request_params(
type_e type,
int category,
support_e support,
const string& filter,
const string& user,
const string& godot_version,
int max_results,
int page,
sort_e sort,
bool reverse,
int verbose
){
request_params params{
.type = type,
.category = category,
.support = support,
@ -76,6 +89,10 @@ namespace gdpm::rest_api{
return d;
}
string to_json(const rapidjson::Document& doc){
return doc.GetString();
}
string to_string(type_e type){
string _s{"type="};
switch(type){
@ -98,6 +115,7 @@ namespace gdpm::rest_api{
}
string to_string(sort_e sort){
string _s{"sort="};
switch(sort){
case none: _s += ""; break;
@ -111,13 +129,13 @@ namespace gdpm::rest_api{
string _prepare_request(
const string &url,
const context &c
const request_params &c
){
string request_url{url};
request_url += to_string(c.type);
request_url += to_string(static_cast<type_e>(c.type));
request_url += (c.category <= 0) ? "&category=" : "&category="+std::to_string(c.category);
request_url += "&" + to_string(c.support);
request_url += "&" + to_string(c.sort);
request_url += "&" + to_string(static_cast<support_e>(c.support));
request_url += "&" + to_string(static_cast<sort_e>(c.sort));
request_url += (!c.filter.empty()) ? "&filter="+c.filter : "";
request_url += (!c.godot_version.empty()) ? "&godot_version="+c.godot_version : "";
request_url += "&max_results=" + std::to_string(c.max_results);
@ -126,7 +144,7 @@ namespace gdpm::rest_api{
return request_url;
}
void _print_params(const context& params){
void _print_params(const request_params& params){
log::println("params: \n"
"\ttype: {}\n"
"\tcategory: {}\n"
@ -170,7 +188,7 @@ namespace gdpm::rest_api{
bool reverse,
int verbose
){
context c{
request_params c{
.type = type,
.category = category,
.support = support,
@ -188,7 +206,7 @@ namespace gdpm::rest_api{
rapidjson::Document get_assets_list(
const string& url,
const context& c
const request_params& c
){
string request_url = _prepare_request(url, c);
http::response r = http::request_get(request_url);
@ -200,13 +218,14 @@ namespace gdpm::rest_api{
rapidjson::Document get_asset(
const string& url,
int asset_id,
const context& params
const request_params& params
){
string request_url = _prepare_request(url, params);
utils::replace_all(request_url, "{id}", std::to_string(asset_id));
http::response r = http::request_get(request_url.c_str());
if(params.verbose > 0)
log::info("URL: {}", request_url);
return _parse_json(r.body);
}

View file

@ -3,6 +3,7 @@
#include "config.hpp"
#include "log.hpp"
#include <asm-generic/errno-base.h>
#include <chrono>
#include <cstdio>
@ -202,4 +203,39 @@ namespace gdpm::utils{
});
return o;
}
namespace json {
std::string from_array(
const std::set<std::string>& a,
const std::string& prefix
){
std::string o{"["};
for(const std::string& src : a)
o += prefix + "\t\"" + src + "\",";
if(o.back() == ',')
o.pop_back();
o += prefix + "]";
return o;
};
std::string from_object(
const std::unordered_map<std::string, std::string>& m,
const std::string& prefix,
const std::string& spaces
){
std::string o{"{"};
std::for_each(m.begin(), m.end(),
[&o, &prefix, &spaces](const std::pair<std::string, std::string>& p){
o += std::format("{}\t\"{}\":{}\"{}\",", prefix, p.first, spaces, p.second);
}
);
if(o.back() == ',')
o.pop_back();
o += prefix + "}";
return o;
};
}
} // namespace gdpm::utils

View file

@ -21,10 +21,26 @@ TEST_SUITE("Command functions"){
using namespace gdpm;
using namespace gdpm::package_manager;
config::context config = config::make_context();
package::params params = package::params();
package::params params = package::params{
.remote_source = "test"
};
config::context config = config::context{
.username = "",
.password = "",
.path = "tests/gdpm/config.json",
.packages_dir = "tests/gdpm/packages",
.tmp_dir = "tests/gdpm/.tmp",
.remote_sources = {
{"test", "http://godotengine.org/asset-library/api"}
},
.info {
.godot_version = "latest",
}
};
package::title_list package_titles{"ResolutionManagerPlugin","godot-hmac", "Godot"};
/* Set the default parameters to use. */
auto check_error = [](const error& error){
if(error.has_occurred()){
log::error(error);