// Copyright Epic Games, Inc. All Rights Reserved. #include "master_cmd.h" #include #include #include #include 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(""), ""); } 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