diff options
-rw-r--r-- | src/other_tools/just_mr/TARGETS | 2 | ||||
-rw-r--r-- | src/other_tools/just_mr/mirrors.hpp | 72 |
2 files changed, 73 insertions, 1 deletions
diff --git a/src/other_tools/just_mr/TARGETS b/src/other_tools/just_mr/TARGETS index d98d2e60..c4a75aef 100644 --- a/src/other_tools/just_mr/TARGETS +++ b/src/other_tools/just_mr/TARGETS @@ -190,7 +190,7 @@ { "type": ["@", "rules", "CC", "library"] , "name": ["mirrors"] , "hdrs": ["mirrors.hpp"] - , "deps": [["@", "json", "", "json"]] + , "deps": [["@", "json", "", "json"], ["src/buildtool/logging", "logging"]] , "stage": ["src", "other_tools", "just_mr"] } } diff --git a/src/other_tools/just_mr/mirrors.hpp b/src/other_tools/just_mr/mirrors.hpp index d59300c5..286a9f0c 100644 --- a/src/other_tools/just_mr/mirrors.hpp +++ b/src/other_tools/just_mr/mirrors.hpp @@ -16,8 +16,11 @@ #define INCLUDED_SRC_OTHER_TOOLS_JUST_MR_MIRRORS_HPP #include <memory> +#include <string> +#include <vector> #include "nlohmann/json.hpp" +#include "src/buildtool/logging/logger.hpp" struct Mirrors { nlohmann::json local_mirrors{}; // maps URLs to list of local mirrors @@ -26,4 +29,73 @@ struct Mirrors { using MirrorsPtr = std::shared_ptr<Mirrors>; +namespace MirrorsUtils { + +/// \brief Get the list of local mirrors for given primary URL. +[[nodiscard]] static inline auto GetLocalMirrors( + MirrorsPtr const& additional_mirrors, + std::string const& repo_url) noexcept -> std::vector<std::string> { + try { + if (additional_mirrors->local_mirrors.contains(repo_url)) { + auto const& arr = additional_mirrors->local_mirrors[repo_url]; + if (arr.is_array()) { + std::vector<std::string> res{}; + res.reserve(arr.size()); + for (auto const& [_, val] : arr.items()) { + if (val.is_string()) { + res.emplace_back(val.get<std::string>()); + } + else { + Logger::Log(LogLevel::Warning, + "Retrieving additional mirrors: found " + "non-string list entry {}", + val.dump()); + return {}; + } + } + return res; + } + Logger::Log( + LogLevel::Warning, + "Retrieving additional mirrors: found non-list value {}", + arr.dump()); + } + } catch (std::exception const& ex) { + Logger::Log(LogLevel::Warning, + "Retrieving additional mirrors failed with:\n{}", + ex.what()); + } + return {}; +} + +/// \brief Get the list of preferred hostnames for non-local fetches. +[[nodiscard]] static inline auto GetPreferredHostnames( + MirrorsPtr const& additional_mirrors) noexcept -> std::vector<std::string> { + try { + std::vector<std::string> res{}; + res.reserve(additional_mirrors->preferred_hostnames.size()); + for (auto const& [_, val] : + additional_mirrors->preferred_hostnames.items()) { + if (val.is_string()) { + res.emplace_back(val.get<std::string>()); + } + else { + Logger::Log(LogLevel::Warning, + "Retrieving preferred mirrors: found non-string " + "list entry {}", + val.dump()); + return {}; + } + } + return res; + } catch (std::exception const& ex) { + Logger::Log(LogLevel::Warning, + "Retrieving preferred mirrors failed with:\n{}", + ex.what()); + return {}; + }; +} + +} // namespace MirrorsUtils + #endif // INCLUDED_SRC_OTHER_TOOLS_JUST_MR_MIRRORS_HPP |