diff options
| author | Dan Engelbrecht <[email protected]> | 2025-06-18 12:47:07 +0200 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2025-06-18 12:47:07 +0200 |
| commit | 18f52147774fa36e1ae7d59a8ae10fb2720f6c01 (patch) | |
| tree | 7ea647a9933c6e86935f5dd38f031c617ffebafb /src | |
| parent | Merge pull request #436 from ue-foundation/lm/toolchain-update (diff) | |
| download | zen-18f52147774fa36e1ae7d59a8ae10fb2720f6c01.tar.xz zen-18f52147774fa36e1ae7d59a8ae10fb2720f6c01.zip | |
`--output-path` option added to `zen version` command (#440)
* `--output-path` option added to `zen version` command
Diffstat (limited to 'src')
| -rw-r--r-- | src/zen/cmds/version_cmd.cpp | 49 | ||||
| -rw-r--r-- | src/zen/cmds/version_cmd.h | 10 | ||||
| -rw-r--r-- | src/zen/zen.cpp | 2 |
3 files changed, 41 insertions, 20 deletions
diff --git a/src/zen/cmds/version_cmd.cpp b/src/zen/cmds/version_cmd.cpp index 41042533d..7dfa125e4 100644 --- a/src/zen/cmds/version_cmd.cpp +++ b/src/zen/cmds/version_cmd.cpp @@ -2,10 +2,12 @@ #include "version_cmd.h" +#include <zencore/basicfile.h> #include <zencore/config.h> #include <zencore/filesystem.h> #include <zencore/fmtutils.h> #include <zencore/logging.h> +#include <zenhttp/httpclient.h> #include <zenhttp/httpcommon.h> #include <zenutil/zenserverprocess.h> @@ -17,11 +19,14 @@ ZEN_THIRD_PARTY_INCLUDES_END namespace zen { +using namespace std::literals; + VersionCommand::VersionCommand() { m_Options.add_options()("h,help", "Print help"); m_Options.add_option("", "u", "hosturl", "Host URL", 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"}); } @@ -51,28 +56,40 @@ VersionCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) } else { - const std::string UrlBase = fmt::format("{}/health", m_HostName); - cpr::Session Session; - std::string VersionRequest = fmt::format("{}/version{}", UrlBase, m_DetailedVersion ? "?detailed=true" : ""); - Session.SetUrl(VersionRequest); - cpr::Response Response = Session.Get(); - if (!zen::IsHttpSuccessCode(Response.status_code)) + if (!m_OutputPath.empty()) { - if (Response.status_code) - { - ZEN_ERROR("{} failed: {}: {} ({})", VersionRequest, Response.status_code, Response.reason, Response.text); - } - else - { - ZEN_ERROR("{} failed: {}", VersionRequest, Response.error.message); - } + ZEN_CONSOLE("Querying host {}", m_HostName); + } + HttpClient Client(m_HostName, HttpClientSettings{.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); + HttpClient::Response Response = Client.Get(VersionRequest, {}, Parameters); + if (!Response.IsSuccess()) + { + ZEN_ERROR("{} failed: {}", VersionRequest, Response.ErrorMessage(""sv)); return 1; } - Version = Response.text; + Version = Response.AsText(); } - ZEN_CONSOLE("{}", Version); + 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(); + } return 0; } diff --git a/src/zen/cmds/version_cmd.h b/src/zen/cmds/version_cmd.h index f8d16fb96..7a910e463 100644 --- a/src/zen/cmds/version_cmd.h +++ b/src/zen/cmds/version_cmd.h @@ -9,6 +9,9 @@ namespace zen { class VersionCommand : public ZenCmdBase { public: + static constexpr char Name[] = "version"; + static constexpr char Description[] = "Get zen service version"; + VersionCommand(); ~VersionCommand(); @@ -16,9 +19,10 @@ public: virtual cxxopts::Options& Options() override { return m_Options; } private: - cxxopts::Options m_Options{"version", "Get zen service version"}; - std::string m_HostName; - bool m_DetailedVersion = false; + cxxopts::Options m_Options{Name, Description}; + std::string m_HostName; + bool m_DetailedVersion = false; + std::filesystem::path m_OutputPath; }; } // namespace zen diff --git a/src/zen/zen.cpp b/src/zen/zen.cpp index 76a866204..598ef9314 100644 --- a/src/zen/zen.cpp +++ b/src/zen/zen.cpp @@ -736,7 +736,7 @@ main(int argc, char** argv) {"top", &TopCmd, "Monitor zen server activity"}, {"trace", &TraceCmd, "Control zen realtime tracing"}, {"up", &UpCmd, "Bring zen server up"}, - {"version", &VersionCmd, "Get zen server version"}, + {VersionCommand::Name, &VersionCmd, VersionCommand::Description}, {"vfs", &VfsCmd, "Manage virtual file system"}, {"flush", &FlushCmd, "Flush storage"}, {WipeCommand::Name, &WipeCmd, WipeCommand::Description}, |