Files
spark/src/lib/os/spark_data_directory.cpp
T
John Colvin 63e5697b20 Unify conf and conf_json dirs
Put all files (.conf and .json) in sparkdata/conf. This allows us to
implement config profiles via symlink.
2025-02-23 06:10:36 +00:00

81 lines
2.3 KiB
C++

#include "spark_data_directory.h"
#include "lib/config/config.h"
#include "lib/config/protos/system_config.pb.h"
#include "lib/logging/logging.h"
#include "lib/os/os.h"
#include "lib/utils/file_utils.h"
#include <boost/filesystem/path.hpp>
#include <fmt/format.h>
#include <string>
namespace bts::spark {
SparkDataDirectory::SparkDataDirectory()
: SparkDataDirectory(config::get<config::SystemConfig>().data_path()) {}
SparkDataDirectory::SparkDataDirectory(const std::string &root) : root_(root) {}
boost::filesystem::path SparkDataDirectory::root() const { return root_; }
boost::filesystem::path SparkDataDirectory::config_json_dir() const {
auto p = root_;
if (p.string().ends_with('/') && p.has_parent_path()) {
p = p.parent_path();
}
if (p.has_parent_path()) {
p = p.parent_path();
}
return p / "conf/";
}
boost::filesystem::path SparkDataDirectory::config_json_file(
const std::string &name) const {
return config_json_dir() / (name + ".json");
}
boost::filesystem::path SparkDataDirectory::log_dir() const {
return root_ / fmt::format("logging.{}/", os::get_application_name());
}
boost::filesystem::path SparkDataDirectory::log_file() const {
return log_dir() / "log";
}
boost::filesystem::path SparkDataDirectory::log_cache_file() const {
return log_dir() / "log_cache";
}
boost::filesystem::path SparkDataDirectory::database_dir(
const std::string &name) const {
return root_ / "databases" / (name + "/");
}
boost::filesystem::path SparkDataDirectory::database_file(
const std::string &name) const {
return root_ / "databases" / name / (name + ".sqlite");
}
boost::filesystem::path SparkDataDirectory::database_file(
const std::string &dir_name, const std::string &db_name) const {
return root_ / "databases" / dir_name / (db_name + ".sqlite");
}
std::optional<nlohmann::json> SparkDataDirectory::load_config_json(
const std::string &name) const {
const auto json_file = config_json_file(name);
if (!boost::filesystem::exists(json_file)) {
return {};
}
const std::string json_str = utils::read_file(json_file.string());
try {
return nlohmann::json::parse(json_str);
} catch (const nlohmann::json::exception &ex) {
LOG(Gen, Warn, "Failed to parse {}. ex: {}", json_file.string(),
std::string{ex.what()});
}
return {};
}
} // namespace bts::spark