diff options
Diffstat (limited to 'src/buildtool/execution_api/remote/config.hpp')
-rw-r--r-- | src/buildtool/execution_api/remote/config.hpp | 147 |
1 files changed, 48 insertions, 99 deletions
diff --git a/src/buildtool/execution_api/remote/config.hpp b/src/buildtool/execution_api/remote/config.hpp index ea7a7093..45420c80 100644 --- a/src/buildtool/execution_api/remote/config.hpp +++ b/src/buildtool/execution_api/remote/config.hpp @@ -15,132 +15,81 @@ #ifndef INCLUDED_SRC_BUILDTOOL_EXECUTION_API_REMOTE_CONFIG_HPP #define INCLUDED_SRC_BUILDTOOL_EXECUTION_API_REMOTE_CONFIG_HPP -#include <cstdint> -#include <exception> #include <filesystem> #include <map> #include <optional> -#include <stdexcept> #include <string> #include <utility> #include <vector> -#include "nlohmann/json.hpp" #include "src/buildtool/common/remote/remote_common.hpp" +#include "src/utils/cpp/expected.hpp" -class RemoteExecutionConfig { - public: - // Obtain global instance - [[nodiscard]] static auto Instance() noexcept -> RemoteExecutionConfig& { - static RemoteExecutionConfig config; - return config; - } +struct RemoteExecutionConfig final { + class Builder; - // Set remote execution and cache address, unsets if parsing `address` fails - [[nodiscard]] static auto SetRemoteAddress( - std::string const& address) noexcept -> bool { - auto& inst = Instance(); - return static_cast<bool>(inst.remote_address_ = inst.cache_address_ = - ParseAddress(address)); - } + // Server address of remote execution. + std::optional<ServerAddress> const remote_address = {}; - // Set remote-execution dispatch property list - [[nodiscard]] static auto SetRemoteExecutionDispatch( - const std::filesystem::path& filename) noexcept -> bool; + // Server dispatch data + std::vector<DispatchEndpoint> const dispatch = {}; - // Set specific cache address, unsets if parsing `address` fails - [[nodiscard]] static auto SetCacheAddress( - std::string const& address) noexcept -> bool { - return static_cast<bool>(Instance().cache_address_ = - ParseAddress(address)); - } + // Server address of cache endpoint for rebuild. + std::optional<ServerAddress> const cache_address = {}; - // Add platform property from string of form "key:val" - [[nodiscard]] static auto AddPlatformProperty( - std::string const& property) noexcept -> bool { - if (auto pair = ParseProperty(property)) { - Instance().platform_properties_[std::move(pair->first)] = - std::move(pair->second); - return true; - } - return false; - } + // Platform properties for execution. + ExecutionProperties const platform_properties = {}; +}; - // Remote execution address, if set - [[nodiscard]] static auto RemoteAddress() noexcept - -> std::optional<ServerAddress> { - return Instance().remote_address_; +class RemoteExecutionConfig::Builder final { + public: + // Set remote execution and cache address. If it fails to parse during + // config build, will reset the fields. + auto SetRemoteAddress(std::optional<std::string> address) noexcept + -> Builder& { + remote_address_raw_ = std::move(address); + return *this; } - // Cache address, if set - [[nodiscard]] static auto CacheAddress() noexcept - -> std::optional<ServerAddress> { - return Instance().cache_address_; + // Set remote-execution dispatch property list filename. + auto SetRemoteExecutionDispatch( + std::optional<std::filesystem::path> filename) noexcept -> Builder& { + dispatch_file_ = std::move(filename); + return *this; } - // Instance dispatch list - [[nodiscard]] static auto DispatchList() noexcept - -> std::vector<DispatchEndpoint> { - return Instance().dispatch_; + // Set specific cache address. If it fails to parse during config build, + // will reset the field. + auto SetCacheAddress(std::optional<std::string> address) noexcept + -> Builder& { + cache_address_raw_ = std::move(address); + return *this; } - [[nodiscard]] static auto PlatformProperties() noexcept - -> ExecutionProperties { - return Instance().platform_properties_; + // Set platform properties given as "key:val" strings. + auto SetPlatformProperties(std::vector<std::string> properties) noexcept + -> Builder& { + platform_properties_raw_ = std::move(properties); + return *this; } - /// \brief String representation of the used execution backend. - [[nodiscard]] static auto DescribeBackend() noexcept -> std::string { - auto address = RemoteAddress(); - auto properties = PlatformProperties(); - auto dispatch = DispatchList(); - auto description = nlohmann::json{ - {"remote_address", address ? address->ToJson() : nlohmann::json{}}, - {"platform_properties", properties}}; - if (!dispatch.empty()) { - try { - // only add the dispatch list, if not empty, so that keys remain - // not only more readable, but also backwards compatible with - // earlier versions. - auto dispatch_list = nlohmann::json::array(); - for (auto const& [props, endpoint] : dispatch) { - auto entry = nlohmann::json::array(); - entry.push_back(nlohmann::json(props)); - entry.push_back(endpoint.ToJson()); - dispatch_list.push_back(entry); - } - description["endpoint dispatch list"] = dispatch_list; - } catch (std::exception const& e) { - Logger::Log(LogLevel::Error, - "Failed to serialize endpoint dispatch list: {}", - e.what()); - } - } - try { - // json::dump with json::error_handler_t::replace will not throw an - // exception if invalid UTF-8 sequences are detected in the input. - // Instead, it will replace them with the UTF-8 replacement - // character, but still it needs to be inside a try-catch clause to - // ensure the noexcept modifier of the enclosing function. - return description.dump( - 2, ' ', false, nlohmann::json::error_handler_t::replace); - } catch (...) { - return ""; - } - } + /// \brief Parse the set data to finalize creation of RemoteExecutionConfig. + /// \return RemoteExecutionConfig on success, an error string on failure. + [[nodiscard]] auto Build() const noexcept + -> expected<RemoteExecutionConfig, std::string>; private: - // Server address of remote execution. - std::optional<ServerAddress> remote_address_{}; + // Server address of remote execution; needs parsing. + std::optional<std::string> remote_address_raw_{}; - // Server dispatch data - std::vector<DispatchEndpoint> dispatch_{}; + // Server dispatch data file; needs parsing. + std::optional<std::filesystem::path> dispatch_file_{}; - // Server address of cache endpoint for rebuild. - std::optional<ServerAddress> cache_address_{}; + // Server address of cache endpoint for rebuild; needs parsing. + std::optional<std::string> cache_address_raw_{}; - // Platform properties for execution. - ExecutionProperties platform_properties_{}; + // Platform properties for execution; needs parsing. + std::vector<std::string> platform_properties_raw_{}; }; #endif // INCLUDED_SRC_BUILDTOOL_EXECUTION_API_REMOTE_CONFIG_HPP |