From 996c47466e333d4df6862730750a6fe860750612 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Sat, 27 May 2023 13:47:52 -0600 Subject: [PATCH] Fixed installing issue with no set remote - Added default remote repo - Fixed adding and removing remote repos --- include/error.hpp | 2 ++ include/package.hpp | 2 +- include/remote.hpp | 2 +- src/README.md | 0 src/package.cpp | 14 ++++++++++-- src/remote.cpp | 55 ++++++++++++++++++++++++--------------------- 6 files changed, 46 insertions(+), 29 deletions(-) create mode 100644 src/README.md diff --git a/include/error.hpp b/include/error.hpp index 4e13f54..1a21b7a 100644 --- a/include/error.hpp +++ b/include/error.hpp @@ -23,8 +23,10 @@ namespace gdpm::constants::error{ DIRECTORY_EXISTS, DIRECTORY_NOT_FOULD, HOST_UNREACHABLE, + REMOTE_NOT_FOUND, EMPTY_RESPONSE, INVALID_ARGS, + INVALID_ARG_COUNT, INVALID_CONFIG, INVALID_KEY, HTTP_RESPONSE_ERROR, diff --git a/include/package.hpp b/include/package.hpp index 9cbb3bd..117af13 100644 --- a/include/package.hpp +++ b/include/package.hpp @@ -47,7 +47,7 @@ namespace gdpm::package { bool enable_cache = true; bool enable_sync = true; bool skip_prompt = false; - string remote_source = ""; + string remote_source = "origin"; install_method_e install_method = GLOBAL_LINK_LOCAL; }; diff --git a/include/remote.hpp b/include/remote.hpp index 3446c54..310670d 100644 --- a/include/remote.hpp +++ b/include/remote.hpp @@ -15,7 +15,7 @@ namespace gdpm::remote{ GDPM_DLL_EXPORT error _handle_remote(config::context& config, const args_t& args, const opts_t& 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& name); + 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); } \ No newline at end of file diff --git a/src/README.md b/src/README.md new file mode 100644 index 0000000..e69de29 diff --git a/src/package.cpp b/src/package.cpp index 4890115..338e6c8 100644 --- a/src/package.cpp +++ b/src/package.cpp @@ -90,6 +90,16 @@ namespace gdpm::package{ return error(); } + /* Check if provided param is in remote sources*/ + if(!config.remote_sources.contains(params.remote_source)){ + error error( + constants::error::NOT_FOUND, + "Remote resource not found in config." + ); + log::error(error); + return error; + } + /* Try and obtain all requested packages. */ std::vector dir_pairs; task_list tasks; @@ -161,7 +171,7 @@ namespace gdpm::package{ /* Check if we already have a stored temporary file before attempting to download */ if(std::filesystem::exists(tmp_zip) && std::filesystem::is_regular_file(tmp_zip)){ - log::println("Found cached package. Skipping download.", p.title); + log::info("Found cached package. Skipping download.", p.title); } else{ /* Download all the package files and place them in tmp directory. */ @@ -226,7 +236,7 @@ namespace gdpm::package{ if(p_cache.empty()){ error error( constants::error::NOT_FOUND, - "\nCould not find any packages to remove." + "Could not find any packages to remove." ); log::error(error); return error; diff --git a/src/remote.cpp b/src/remote.cpp index 52de291..8e0b123 100644 --- a/src/remote.cpp +++ b/src/remote.cpp @@ -11,14 +11,6 @@ namespace gdpm::remote{ const args_t& args, const opts_t& opts ){ - log::println("_handle_remote"); - for(const auto& arg : args){ - log::println("arg: {}", arg); - } - for(const auto& opt : opts){ - log::println("opt: {}:{}", opt.first, utils::join(opt.second)); - } - /* Check if enough arguments are supplied */ size_t argc = args.size(); if (argc < 1){ @@ -28,19 +20,30 @@ namespace gdpm::remote{ /* Check which subcommand is supplied */ string sub_command = args.front(); - args_t argv(args.begin()+1, args.end()); - if(argv.size() < 2){ - error error( - constants::error::INVALID_ARGS, - "Invalid number of args" - ); - log::error(error); - return error; + if(sub_command == "add"){ + if(args.size() < 3 || args.empty()){ + error error( + constants::error::INVALID_ARG_COUNT, + "Invalid number of args." + ); + log::error(error); + return error; + } + string name = args[1]; + string url = args[2]; + add_repositories(config, {{name, url}}); + } + else if (sub_command == "remove") { + if(args.size() < 2 || args.empty()){ + error error( + constants::error::INVALID_ARG_COUNT, + "Invalid number of args." + ); + log::error(error); + return error; + } + remove_respositories(config, {args.begin()+1, args.end()}); } - string name = argv[1]; - string url = argv[2]; - if(sub_command == "add") add_repositories(config, {{name, url}}); - else if (sub_command == "remove") remove_respositories(config, argv); // else if (sub_command == "set") set_repositories(config::context &context, const repository_map &repos) else if (sub_command == "list") print_repositories(config); else{ @@ -79,9 +82,12 @@ namespace gdpm::remote{ config::context& config, const repo_names& names ){ - std::for_each(names.end(), names.begin(), [&config](const string& repo){ - config.remote_sources.erase(repo); - }); + for(auto it = names.begin(); it != names.end();){ + if(config.remote_sources.contains(*it)){ + config.remote_sources.erase(*it); + } + it++; + } } @@ -94,10 +100,9 @@ namespace gdpm::remote{ } void print_repositories(const config::context& config){ - log::println("Remote sources:"); const auto &rs = config.remote_sources; std::for_each(rs.begin(), rs.end(), [](const string_pair& p){ - log::println("\t{}: {}", p.first, p.second); + log::println("{}: {}", p.first, p.second); }); } } \ No newline at end of file