aboutsummaryrefslogtreecommitdiff
path: root/src/zen/cmds/version_cmd.cpp
blob: 0948de1bbbaa434a53f88cc3f605c083e74ab83d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright Epic Games, Inc. All Rights Reserved.

#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>

#include <memory>

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