reman3/tooling/cmd_verify.cpp

48 lines
1.6 KiB
C++

#include "tool.hpp"
#include <CLI11.hpp>
bool processDuplicates(DatabaseManager &db) {
// Scan all files in the database, and for non-existing files, remove them from the database
auto files = db.getAllFiles();
for (auto &file : files) {
if (!std::filesystem::exists(file)) {
spdlog::warn("File not found, removing from database: {}", file);
db.removeFile(file);
}
}
spdlog::info("=== Checking for duplicate addresses ===");
bool found_address_duplicates = db.checkDuplicateAddresses();
if (found_address_duplicates) {
spdlog::error("Found duplicate addresses in the database!");
} else {
spdlog::info("No duplicate addresses found in the database.");
}
spdlog::info("=== Checking for duplicate names ===");
bool found_name_duplicates = db.checkDuplicateNames();
if (found_name_duplicates) {
spdlog::error("Found duplicate names in the database!");
} else {
spdlog::info("No duplicate names found in the database.");
}
return !found_address_duplicates && !found_name_duplicates;
}
void register_cmd_verify(CLI::App &app) {
auto cmd = app.add_subcommand("verify", "Verify the database");
cmd->final_callback([]() {
auto &options = Options::get();
DatabaseManager db(options.db_path);
// For duplicates mode, we only check the database, no file processing
spdlog::info("=== Checking database for duplicates ===");
bool has_duplicates = !processDuplicates(db);
spdlog::info("=== Summary ===");
spdlog::info("Mode: {}", options.mode);
spdlog::info("Database: {}", options.db_path);
return has_duplicates ? 1 : 0; // Return 1 if duplicates found, 0 if none
});
}