aboutsummaryrefslogtreecommitdiff
path: root/src/zen/cmds/version_cmd.cpp
blob: 7dfa125e4b975a2ff923d0c6dd347fb971a22a64 (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
93
94
95
96
// 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>

ZEN_THIRD_PARTY_INCLUDES_START
#include <cpr/cpr.h>
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"});
}

VersionCommand::~VersionCommand() = default;

int
VersionCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
	ZEN_UNUSED(GlobalOptions);
	if (!ParseOptions(argc, argv))
	{
		return 0;
	}

	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(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.AsText();
	}

	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;
}
}  // namespace zen