Skip to content

Commit

Permalink
TgBot++: database: DBCtrl: Add set_ownerid and fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Royna2544 committed Jun 29, 2024
1 parent b6d8886 commit 9aad968
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/linux_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions .github/workflows/macos_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 additions & 2 deletions .github/workflows/windows_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
78 changes: 56 additions & 22 deletions src/database/utils/DatabaseCtrl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,72 @@
#include <database/bot/TgBotDatabaseImpl.hpp>
#include <iostream>
#include <memory>
#include <vector>

#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<std::string> args; // Array of command arguments after (exe, command)
std::shared_ptr<TgBotDatabaseImpl> impl; // Instance of TgBotDatabaseImpl
};

namespace {

template <Commands>
void executeCommand(const CommandData& data) {
// Implement command logic here
}

template <>
void executeCommand<Commands::Noop>(const CommandData& /*data*/) {
// No-op command for testing
std::cout << "No-op command executed" << std::endl;
}

template <>
void executeCommand<Commands::Help>(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;
std::cout << "- delete_media: Delete media from the database" << std::endl;
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 <>
void executeCommand<Commands::Dump>(const CommandData& data) {
data.impl->dump(std::cout);
}

template <>
void executeCommand<Commands::DeleteMedia>(const CommandData& data) {
// Not implemented as it will need DatabaseBase::deleteMedia() function
// implementation
}

template <>
void executeCommand<Commands::AddChat>(const CommandData& data) {
if (data.argc != 2) {
if (data.args.size() != 2) {
LOG(ERROR) << "Need <chatid> <name> 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;
Expand All @@ -67,30 +79,50 @@ void executeCommand<Commands::AddChat>(const CommandData& data) {
}
}


template <>
void executeCommand<Commands::DeleteChat>(const CommandData& data) {
// Again, not implemented for now
void executeCommand<Commands::SetOwnerId>(const CommandData& data) {
if (data.impl->getOwnerUserId()) {
LOG(ERROR) << "Owner ID already set";
return;
}
if (data.args.size() != 1) {
LOG(ERROR) << "Need <owner_id> 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<std::string> 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<Commands::Help>(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") {
Expand All @@ -101,6 +133,8 @@ int main(const int argc, const char** argv) {
executeCommand<Commands::AddChat>(data);
} else if (command == "delete_chat") {
executeCommand<Commands::DeleteChat>(data);
} else if (command == "set_owner_id") {
executeCommand<Commands::SetOwnerId>(data);
} else {
LOG(ERROR) << "Unknown command: " << command;
}
Expand Down

0 comments on commit 9aad968

Please sign in to comment.