diff --git a/.github/workflows/linux_test.yml b/.github/workflows/linux_test.yml index f08518c9..3c0b2bea 100644 --- a/.github/workflows/linux_test.yml +++ b/.github/workflows/linux_test.yml @@ -61,6 +61,9 @@ jobs: cmake --build build -j4 - name: Run Tests + env: + DATABASE_BACKEND: sqlite:new.db run: | cd build + ./bin/TgBot++_DatabaseCtl set_owner_id 12345 ./bin/TgBot++_test diff --git a/.github/workflows/macos_test.yml b/.github/workflows/macos_test.yml index fb58d336..0ee424bb 100644 --- a/.github/workflows/macos_test.yml +++ b/.github/workflows/macos_test.yml @@ -34,7 +34,10 @@ jobs: cmake --build build -j4 - name: Run Tests + env: + DATABASE_BACKEND: sqlite:new.db run: | cd build + ./bin/TgBot++_DatabaseCtl set_owner_id 12345 ./bin/TgBot++_test diff --git a/.github/workflows/windows_test.yml b/.github/workflows/windows_test.yml index 5330d122..227a2952 100644 --- a/.github/workflows/windows_test.yml +++ b/.github/workflows/windows_test.yml @@ -62,6 +62,9 @@ jobs: cmake --build build -j4 - name: Run Tests + env: + DATABASE_BACKEND: sqlite:new.db run: | - cd build - ./bin/TgBot++_test.exe + cd build + ./bin/TgBot++_DatabaseCtl.exe set_owner_id 12345 + ./bin/TgBot++_test.exe \ No newline at end of file diff --git a/src/database/utils/DatabaseCtrl.cc b/src/database/utils/DatabaseCtrl.cc index 72f46a07..0485fd90 100644 --- a/src/database/utils/DatabaseCtrl.cc +++ b/src/database/utils/DatabaseCtrl.cc @@ -5,28 +5,44 @@ #include #include #include +#include +#include "ConfigManager.h" #include "TryParseStr.hpp" -enum class Commands { Help, Dump, DeleteMedia, AddChat, DeleteChat }; +enum class Commands { + Noop, + Help, + Dump, + AddChat, + SetOwnerId, + DeleteMedia = Noop, + DeleteChat = Noop, +}; // Structure to hold command data for easier manipulation and handling struct CommandData { - int argc; // Number of arguments after (exe, command) - const char** argv; // Array of command arguments after (exe, command) + std::vector args; // Array of command arguments after (exe, command) std::shared_ptr impl; // Instance of TgBotDatabaseImpl }; namespace { + template void executeCommand(const CommandData& data) { // Implement command logic here } +template <> +void executeCommand(const CommandData& /*data*/) { + // No-op command for testing + std::cout << "No-op command executed" << std::endl; +} + template <> void executeCommand(const CommandData& data) { std::cout << std::endl << "Telegram Bot Database CLI" << std::endl; - std::cout << "Usage: " << data.argv[0] << " [command] [args...]" + std::cout << "Usage: " << data.args[0] << " [command] [args...]" << std::endl; std::cout << "Available commands:" << std::endl; std::cout << "- dump: Dump the database contents to stdout" << std::endl; @@ -34,6 +50,8 @@ void executeCommand(const CommandData& data) { std::cout << "- add_chat: Add a chat info to the database" << std::endl; std::cout << "- delete_chat: Delete a chat info from the database" << std::endl; + std::cout << "- set_owner_id: Set the owner ID for the database" + << std::endl; } template <> @@ -41,24 +59,18 @@ void executeCommand(const CommandData& data) { data.impl->dump(std::cout); } -template <> -void executeCommand(const CommandData& data) { - // Not implemented as it will need DatabaseBase::deleteMedia() function - // implementation -} - template <> void executeCommand(const CommandData& data) { - if (data.argc != 2) { + if (data.args.size() != 2) { LOG(ERROR) << "Need as arguments"; return; } ChatId chatid{}; - if (!try_parse(data.argv[0], &chatid)) { + if (!try_parse(data.args[0], &chatid)) { LOG(ERROR) << "Invalid chatid specified"; return; } - const std::string name = data.argv[1]; + const std::string name = data.args[1]; if (data.impl->addChatInfo(chatid, name)) { LOG(INFO) << "Added chat info for chatid=" << chatid << " name=" << name; @@ -67,30 +79,50 @@ void executeCommand(const CommandData& data) { } } + template <> -void executeCommand(const CommandData& data) { - // Again, not implemented for now +void executeCommand(const CommandData& data) { + if (data.impl->getOwnerUserId()) { + LOG(ERROR) << "Owner ID already set"; + return; + } + if (data.args.size() != 1) { + LOG(ERROR) << "Need as argument"; + return; + } + UserId ownerId{}; + if (!try_parse(data.args[0], &ownerId)) { + LOG(ERROR) << "Invalid owner_id specified"; + return; + } + data.impl->setOwnerUserId(ownerId); + LOG(INFO) << "Owner ID set to " << ownerId; } } // namespace -int main(const int argc, const char** argv) { +int main(int argc, char* const* argv) { TgBot_AbslLogInit(); - CommandData data = {argc, argv, nullptr}; + copyCommandLine(CommandLineOp::INSERT, &argc, &argv); + + std::vector args; + for (int i = 1; i < argc; ++i) { + args.emplace_back(argv[i]); + } + CommandData data = {args, nullptr}; auto dbImpl = TgBotDatabaseImpl::getInstance(); if (!dbImpl->loadDBFromConfig()) { LOG(ERROR) << "Failed to load database"; return EXIT_FAILURE; } - if (argc < 2) { + if (args.size() < 2) { executeCommand(data); return EXIT_SUCCESS; } - const std::string command = argv[1]; - const int argcPartial = argc - 2; - const char** argvPartial = argv += 2; - data = {argcPartial, argvPartial, dbImpl}; + const std::string command = args[1]; + args.erase(args.begin(), args.begin() + 2); + data = {args, dbImpl}; DLOG(INFO) << "Executing command: " << command; if (command == "dump") { @@ -101,6 +133,8 @@ int main(const int argc, const char** argv) { executeCommand(data); } else if (command == "delete_chat") { executeCommand(data); + } else if (command == "set_owner_id") { + executeCommand(data); } else { LOG(ERROR) << "Unknown command: " << command; }