Files
blackbird/core/src/registry/InitParser.cpp
T
qing b0d71a4c58 core: 移除 init.txt 向后兼容,仅支持 services.yaml
- InitParser: 移除 parseTxt(),load() 不再回退 init.txt
- pmc_mtd: parse_self_init_list 移除 TXT 分支,非 .yaml 文件报错退出
- main.cpp: --help 中 -i 参数描述移除 .txt
- Makefile: install/uninstall 移除 init.txt 拷贝/删除
- services.yaml: 头注释移除 init.txt 相关措辞
- 删除 init.txt 文件
- README (core + 项目根): 全部 init.txt 引用替换为 services.yaml
- core README: 补全 liblogc/yaml-cpp 依赖声明,修正文件结构
2026-08-10 05:39:00 +00:00

93 lines
2.5 KiB
C++

#include "registry/InitParser.hh"
#include <yaml-cpp/yaml.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <filesystem>
namespace qing::registry {
/* -------- ServiceConfig -------- */
std::string ServiceConfig::toCommand() const {
std::ostringstream oss;
oss << cmd;
for (const auto& a : args) {
oss << ' ' << a;
}
return oss.str();
}
/* -------- InitParser -------- */
std::vector<ServiceConfig> InitParser::load(const std::string& dir) {
namespace fs = std::filesystem;
auto yaml_path = fs::path(dir) / "services.yaml";
if (fs::exists(yaml_path)) {
std::cerr << "[InitParser] 使用 services.yaml: " << yaml_path << std::endl;
return parseYaml(yaml_path.string());
}
std::cerr << "[InitParser] 未找到 services.yaml 于 " << dir << std::endl;
return {};
}
std::vector<ServiceConfig> InitParser::parseYaml(const std::string& path) {
std::vector<ServiceConfig> result;
YAML::Node root;
try {
root = YAML::LoadFile(path);
} catch (const std::exception& e) {
std::cerr << "[InitParser] YAML 解析失败: " << e.what() << std::endl;
return {};
}
if (!root["services"] || !root["services"].IsSequence()) {
std::cerr << "[InitParser] services.yaml 缺少 services 序列" << std::endl;
return {};
}
for (const auto& node : root["services"]) {
ServiceConfig svc;
if (node["name"])
svc.name = node["name"].as<std::string>();
if (node["cmd"])
svc.cmd = node["cmd"].as<std::string>();
if (node["args"] && node["args"].IsSequence())
for (const auto& a : node["args"])
svc.args.push_back(a.as<std::string>());
if (node["env"] && node["env"].IsMap())
for (const auto& kv : node["env"])
svc.env[kv.first.as<std::string>()] = kv.second.as<std::string>();
if (node["depends_on"] && node["depends_on"].IsSequence())
for (const auto& d : node["depends_on"])
svc.depends_on.push_back(d.as<std::string>());
if (node["sleep_before"])
svc.sleep_before = node["sleep_before"].as<int>(0);
if (svc.cmd.empty()) {
std::cerr << "[InitParser] 跳过缺少 cmd 的服务: " << svc.name << std::endl;
continue;
}
result.push_back(std::move(svc));
}
std::cerr << "[InitParser] 解析完成,共 " << result.size() << " 个服务" << std::endl;
return result;
}
} // namespace qing::registry