// Copyright Epic Games, Inc. All Rights Reserved. #include "version_cmd.h" #include #include #include #include #include #include #include #include #include namespace zen { using namespace std::literals; VersionCommand::VersionCommand() { m_Options.add_options()("h,help", "Print help"); m_Options.add_option("", "u", "hosturl", kHostUrlHelp, cxxopts::value(m_HostName), "[hosturl]"); m_Options.add_option("", "d", "detailed", "Detailed Version", cxxopts::value(m_DetailedVersion), "[detailedversion]"); m_Options.add_option("", "o", "output-path", "Path for output", cxxopts::value(m_OutputPath), "[outputpath]"); m_Options.parse_positional({"hosturl"}); } VersionCommand::~VersionCommand() = default; void VersionCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) { ZEN_UNUSED(GlobalOptions); if (!ParseOptions(argc, argv)) { return; } std::string Version; if (m_HostName.empty()) { if (m_DetailedVersion) { Version = ZEN_CFG_VERSION_BUILD_STRING_FULL; } else { Version = ZEN_CFG_VERSION; } } else { if (!m_OutputPath.empty()) { ZEN_CONSOLE("Querying host {}", m_HostName); } HttpClient Client = CreateHttpClient(m_HostName, {.Timeout = std::chrono::milliseconds(5000)}); HttpClient::KeyValueMap Parameters; if (m_DetailedVersion) { Parameters.Entries.insert_or_assign("detailed", "true"); } const std::string_view VersionRequest("/health/version"sv); if (HttpClient::Response Response = Client.Get(VersionRequest, {}, Parameters)) { Version = Response.AsText(); } else { Response.ThrowError(fmt::format("{} failed", VersionRequest)); } } if (m_OutputPath.empty()) { ZEN_CONSOLE("{}", Version); } else { ZEN_CONSOLE("Writing version '{}' to '{}'", Version, m_OutputPath); BasicFile OutputFile(m_OutputPath, BasicFile::Mode::kTruncate); OutputFile.Write(Version.data(), Version.length(), 0); OutputFile.Close(); } } } // namespace zen