transm/client/main.cpp

127 lines
3.9 KiB
C++

#include "client.h"
#include "config.h"
#include "version.h"
#include <CLI11.hpp>
#include <iostream>
#include <regex>
std::shared_ptr<spdlog::logger> g_Logger = nullptr;
std::shared_ptr<CServerConfig> g_Config = nullptr;
bool parse_cmd(int argc, char** argv, CmdParam& param)
{
std::string intro("transmc cmd introduce.");
CLI::App app(intro);
app.add_option("-n, --number", param.use_config, "使用服务器地址组(值为使用--show中显示的序号)");
app.add_option("-a, --append", param.appendValue, "添加服务器地址组(地址格式:127.0.0.1:9898)");
app.add_flag("-s, --show", param.showValue, "查看服务器地址组");
app.add_option("-r, --remove", param.removeValue, "移除服务器地址组(值为使用--show中显示的序号)");
try {
CLI11_PARSE(app, argc, argv);
return true;
} catch (const CLI::ParseError& e) {
std::cerr << "Error parsing command line: " << e.what() << std::endl;
return false;
}
return false;
}
bool exec_cmd(const CmdParam& param, bool& run)
{
run = false;
// 如果是展示
if (param.showValue) {
std::vector<TransmSet> set;
if (!g_Config->read_ini(set)) {
return false;
}
for (const auto& item : set) {
g_Logger->info("{} => {}:{}", item.group, item.ip, item.port);
}
return true;
}
if (param.use_config != -1) {
run = true;
return true;
}
if (!param.appendValue.empty() && !param.removeValue.empty()) {
g_Logger->error("append and remove can't simultaneous operate!");
return false;
}
// 如果是移除
if (!param.appendValue.empty()) {
std::regex rg(R"(([^:]+):(\d+))");
std::smatch match;
if (!std::regex_search(param.appendValue, match, rg)) {
g_Logger->error("append invalid format!");
return false;
}
std::string ip = match[1].str();
std::string port = match[2].str();
if (!g_Config->append_ini(ip, std::stol(port))) {
g_Logger->error("add {}:{} failed.", ip, port);
return false;
}
g_Logger->info("add {}:{} success.", ip, port);
return true;
}
if (!param.removeValue.empty()) {
if (!g_Config->remove_ini(std::stol(param.removeValue))) {
g_Logger->error("remove {} failed, please check!", param.removeValue);
return false;
}
g_Logger->info("remove {} success!", param.removeValue);
return true;
}
g_Logger->error("not matched!", param.removeValue);
return false;
}
int main(int argc, char* argv[])
{
auto log_path = ofen::COfPath::to_full("client.log");
g_Logger = get_logger("client", log_path);
g_Config = std::make_shared<CServerConfig>(g_Logger);
if (!g_Config->baseInit()) {
return -1;
}
bool run = false;
CmdParam param;
if (!parse_cmd(argc, argv, param)) {
g_Logger->error("parse cmd failed!");
return -1;
}
if (param.appendValue.empty() && param.removeValue.empty() && param.showValue == false &&
param.use_config == -1) {
g_Logger->warn("Use --help To Get How To Use.");
return 0;
}
if (!exec_cmd(param, run)) {
g_Logger->error("exec_cmd failed!");
return -1;
}
if (!run) {
return 0;
}
g_Logger->info("Configure At {} under {} on {}", VERSION_BUILD_DATE, VERSION_GIT_HASH,
VERSION_GIT_BRANCH);
std::vector<TransmSet> set;
if (!g_Config->read_ini(set)) {
return -1;
}
TransmSet use;
if (!g_Config->get_ini(set, param.use_config, use)) {
return -1;
}
g_Logger->info("use ip:[{}], port:[{}]", use.ip, use.port);
CClient client(g_Logger);
client.run(use.ip, std::to_string(use.port));
g_Logger->info("exit ==========");
return 0;
}