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

@ -36,18 +36,16 @@
*/
namespace gdpm::package_manager{
CURL *curl;
CURLcode res;
config::context config;
remote::repository_map remote_sources;
action_e action;
CURL *curl;
CURLcode res;
config::context config;
action_e action;
// opts_t opts;
bool skip_prompt = false;
bool clean_tmp_dir = false;
int priority = -1;
error initialize(int argc, char **argv){
// curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
@ -83,166 +81,178 @@ namespace gdpm::package_manager{
error parse_arguments(int argc, char **argv){
using namespace clipp;
/* 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;
auto doc_format = clipp::doc_formatting{}
.first_column(7)
.doc_column(45)
.last_column(99);
/* 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";
auto debugOpt = option("-d", "--debug").set(config.verbose, to_int(log::DEBUG)) % "show debug output";
auto configOpt = option("--config-path").set(config.path) % "set config path";
auto fileOpt = repeatable(option("--file", "-f").set(params.args) % "read file as input");
auto pathOpt = option("--path").set(params.paths) % "specify a path to use with command";
auto typeOpt = option("--type").set(config.info.type) % "set package type (any|addon|project)";
auto sortOpt = option("--sort").set(config.api_params.sort) % "sort packages in order (rating|cost|name|updated)";
auto supportOpt = option("--support").set(config.api_params.support) % "set the support level for API (all|official|community|testing)";
auto maxResultsOpt = option("--max-results").set(config.api_params.max_results) % "set the request max results";
auto godotVersionOpt = option("--godot-version").set(config.api_params.godot_version) % "set the request Godot version";
auto packageDirOpt = option("--package-dir").set(config.packages_dir) % "set the global package location";
auto tmpDirOpt = option("--tmp-dir").set(config.tmp_dir) % "set the temporary download location";
auto timeoutOpt = option("--timeout").set(config.timeout) % "set the request timeout";
auto verboseOpt = repeatable(option("-v", "--verbose", "-v").call([]{ config.verbose += 1; })) % "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 cleanOpt = option("--clean").set(config.clean_temporary) % "enable/disable cleaning temps";
auto parallelOpt = option("--jobs").set(config.jobs) % "set number of parallel jobs";
auto cacheOpt = option("--enable-cache").set(config.enable_cache) % "enable/disable local caching";
auto syncOpt = option("--enable-sync").set(config.enable_sync) % "enable/disable remote syncing";
auto skipOpt = option("--skip-prompt").set(config.skip_prompt) % "skip the y/n prompt";
auto remoteOpt = 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 packageValues = values("packages", package_titles);
auto requiredPath = required("--path", params.args);
auto installCmd = "install" % (
command("install").set(action, action_e::install),
packageValues % "packages to install from asset library",
godotVersionOpt, 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 addCmd = "add" % (
command("add").set(action, action_e::add),
packageValues % "package(s) to add to local project",
parallelOpt, skipOpt, remoteOpt
);
auto removeCmd = (
clipp::command("remove")
.set(action, action_e::remove)
.doc("Remove a package from local project"),
packageValues
auto removeCmd = "remove" % (
command("remove").set(action, action_e::remove),
packageValues % "package(s) to remove from local project"
);
auto updateCmd = (
clipp::command("update")
.set(action, action_e::update)
.doc("Update package(s)"),
packageValues
auto updateCmd = "update" % (
command("update").set(action, action_e::update),
packageValues % "update package(s)"
);
auto searchCmd = (
clipp::command("search")
.set(action, action_e::search)
.doc("Search for package(s)"),
packageValues
auto searchCmd = "search" % (
command("search").set(action, action_e::search),
packageValues % "package(s) to search for"
);
auto exportCmd = (
clipp::command("export")
.set(action, action_e::p_export)
.doc("Export package list"),
clipp::values("path", input)
auto exportCmd = "export" % (
command("export").set(action, action_e::p_export),
values("paths", params.args) % "export installed package list to file"
);
auto listCmd = (
clipp::command("list")
.set(action, action_e::list)
.doc("Show installed packages")
auto listCmd = "show installed packages" % (
command("list").set(action, action_e::list)
);
auto linkCmd = (
clipp::command("link")
.set(action, action_e::link)
.doc("Create symlink packages to project"),
packageValues,
requiredPath
auto linkCmd = "link" % (
command("link").set(action, action_e::link),
value("package", package_titles) % "package name to link",
value("path", params.args) % "path to project"
);
auto cloneCmd = (
clipp::command("clone")
.set(action, action_e::clone)
.doc("Clone packages to project"),
packageValues,
requiredPath
auto cloneCmd = "clone" % (
command("clone").set(action, action_e::clone),
value("package", package_titles) % "packages to clone",
value("path", params.args) % "path to project"
);
auto cleanCmd = (
clipp::command("clean")
.set(action, action_e::clean)
.doc("Clean temporary files"),
packageValues
auto cleanCmd = "clean" % (
command("clean").set(action, action_e::clean),
values("packages", package_titles) % "package temporary files to remove"
);
auto configCmd = (
clipp::command("config")
.set(action, action_e::config)
.doc("Set/get config properties")
auto configCmd = "get/set config properties" % (
command("config").set(action, action_e::config_get),
(
( greedy(command("get")).set(action, action_e::config_get),
option(repeatable(values("properties", params.args))) % "get config properties"
)
|
( command("set").set(action, action_e::config_set),
value("property", params.args[1]).call([]{}) % "config property",
value("value", params.args[2]).call([]{}) % "config value"
)
)
);
auto fetchCmd = (
clipp::command("fetch")
.set(action, action_e::fetch)
.doc("Fetch asset metadata from remote source")
auto fetchCmd = "fetch" % (
command("fetch").set(action, action_e::fetch),
option(values("remote", params.args)) % "remote to fetch asset data"
);
auto add_arg = [&params](string arg) { params.args.emplace_back(arg); };
auto remoteCmd = (
clipp::command("remote")
.set(action, action_e::remote)
.doc("Manage remote sources")
.required("subcommand")
command("remote").set(action, action_e::remote_list).if_missing(
[]{
remote::print_repositories(config);
}
),
(
"add a remote source" % ( command("add").set(action, action_e::remote_add),
word("name").call(add_arg) % "remote name",
value("url").call(add_arg) % "remote URL"
)
|
"remove a remote source" % ( command("remove").set(action, action_e::remote_remove),
words("names", params.args) % "remote name(s)"
)
|
"list remote sources" % ( command("list").set(action, action_e::remote_list))
)
);
auto uiCmd = (
clipp::command("ui")
.set(action, action_e::ui)
.doc("Show the UI")
auto uiCmd = "start with UI" % (
command("ui").set(action, action_e::ui)
);
auto helpCmd = (
clipp::command("help")
.set(action, action_e::help)
auto helpCmd = "show this message and exit" % (
command("help").set(action, action_e::help)
);
auto cli = (
debugOpt, configOpt,
(installCmd | addCmd | removeCmd | updateCmd | searchCmd | exportCmd |
listCmd | linkCmd | cloneCmd | cleanCmd | configCmd | fetchCmd |
remoteCmd | uiCmd | helpCmd)
);
/* Make help output */
string map_page_format("");
auto man_page = clipp::make_man_page(cli);
string man_page_format("");
auto man_page = make_man_page(cli, argv[0], doc_format)
.prepend_section("DESCRIPTION", "\tManage Godot Game Engine assets from the command-line.")
.append_section("LICENSE", "\tSee the 'LICENSE.md' file for more details.");
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";
[&man_page_format](const man_page::section& s){
man_page_format += s.title() + "\n";
man_page_format += s.content() + "\n\n";
}
);
// log::level = config.verbose;
if(clipp::parse(argc, argv, cli)){
log::level = config.verbose;
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, 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;
case action_e::install: package::install(config, package_titles, params); break;
case action_e::add: package::add(config, package_titles);
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(params.args); break;
case action_e::list: package::list(config, params); break;
/* ...opts are the paths here */
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_get: config::print_properties(config, params.args); break;
case action_e::config_set: config::handle_config(config, package_titles, params.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_list: remote::print_repositories(config); break;
case action_e::remote_add: remote::add_repository(config, params.args); break;
case action_e::remote_remove: remote::remove_respositories(config, params.args); break;
case action_e::ui: log::println("UI not implemented yet"); break;
case action_e::help: log::println("{}", man_page_format); break;
case action_e::none: /* ...here to run with no command */ break;
}
} else {
log::println("{}", map_page_format);
log::println("usage:\n{}", usage_lines(cli, argv[0]).str());
}
return error();
}