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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "master_cmd.h"
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/string.h>
#include <zenhttp/httpclient.h>
using namespace std::literals;
namespace zen::master {
class StatusCommand : public MasterCommand::SubCommand
{
public:
StatusCommand() : MasterCommand::SubCommand("status"sv) {}
virtual void Run(MasterCommand::MasterConfig& Config, int argc, char* argv[]) override
{
ZEN_UNUSED(argc, argv);
HttpClient Http(Config.m_HostName);
if (HttpClient::Response Result = Http.Get("/admin/info"sv, HttpClient::Accept(ZenContentType::kYAML)))
{
ZEN_CONSOLE("{}", Result.ToText());
}
else
{
Result.ThrowError(fmt::format("Failed getting info from '{}' ('{}')"sv, Config.m_HostName, Result.ErrorMessage("error: ")));
}
}
private:
};
class StartCommand : public MasterCommand::SubCommand
{
public:
StartCommand() : MasterCommand::SubCommand("start"sv) {}
virtual void Run(MasterCommand::MasterConfig& Config, int argc, char* argv[]) override
{
ZEN_UNUSED(argc, argv);
HttpClient Http(Config.m_HostName);
if (HttpClient::Response Result = Http.Get("/admin/info"sv, HttpClient::Accept(ZenContentType::kYAML)))
{
ZEN_CONSOLE("{}", Result.ToText());
}
else
{
Result.ThrowError(fmt::format("Failed getting info from '{}' ('{}')"sv, Config.m_HostName, Result.ErrorMessage("error: ")));
}
}
private:
};
class StopCommand : public MasterCommand::SubCommand
{
public:
StopCommand() : MasterCommand::SubCommand("stop"sv) {}
virtual void Run(MasterCommand::MasterConfig& Config, int argc, char* argv[]) override
{
ZEN_UNUSED(argc, argv);
HttpClient Http(Config.m_HostName);
if (HttpClient::Response Result = Http.Get("/admin/info"sv, HttpClient::Accept(ZenContentType::kYAML)))
{
ZEN_CONSOLE("{}", Result.ToText());
}
else
{
Result.ThrowError(fmt::format("Failed getting info from '{}' ('{}')"sv, Config.m_HostName, Result.ErrorMessage("error: ")));
}
}
private:
};
} // namespace zen::master
namespace zen {
//////////////////////////////////////////////////////////////////////////
MasterCommand::SubCommand::SubCommand(std::string_view CommandName) : m_CommandName(CommandName), m_Options(m_CommandName)
{
}
MasterCommand::SubCommand::~SubCommand()
{
}
MasterCommand::MasterCommand()
{
m_Options.add_options()("h,help", "Print help");
m_Options.add_option("", "u", "hosturl", "Host URL", cxxopts::value(m_Config.m_HostName)->default_value(""), "<hosturl>");
}
MasterCommand::~MasterCommand()
{
}
void
MasterCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
ZEN_UNUSED(GlobalOptions);
master::StatusCommand StatusCmd;
master::StartCommand StartCmd;
master::StopCommand StopCmd;
SubCommand* SubCommands[] = {&StatusCmd, &StartCmd, &StopCmd};
SubCommand* Cmd = nullptr;
int ParentArgCount = 0;
for (int i = 1; i < argc; ++i)
{
for (SubCommand* SubCmd : SubCommands)
{
if (argv[i] == SubCmd->Name())
{
Cmd = SubCmd;
ParentArgCount = i;
goto parsed;
}
}
}
throw OptionParseException("no subcommand found", m_Options.help());
parsed:
// Parse main command line options
if (!ParseOptions(ParentArgCount, argv))
{
return;
}
m_Config.m_HostName = ResolveTargetHostSpec(m_Config.m_HostName);
if (m_Config.m_HostName.empty())
{
throw OptionParseException("unable to resolve server specification", m_Options.help());
}
if (Cmd)
{
Cmd->Run(m_Config, argc - ParentArgCount, argv + ParentArgCount);
}
}
} // namespace zen
|