From 2b973ad4fd9651b8e5a22b2e5811ce82bec7e381 Mon Sep 17 00:00:00 2001 From: nekobit Date: Tue, 18 Oct 2022 21:46:18 +0000 Subject: [PATCH] Wormhole control options FossilOrigin-Name: bc55df0989e94415349d692793cfa1aa645c8b3677c35ef039810dd2ed891c30 --- CMakeLists.txt | 1 + src/control.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++++++ src/control.h | 33 ++++++++++++++++++ src/main.cpp | 17 +++++++-- 4 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 src/control.cpp create mode 100644 src/control.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1062ea5..b4e84b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ add_library(wormhole_lib src/config/config_db.cpp src/config/config_loader.cpp src/config/config_instance.cpp + src/control.cpp src/logger.cpp) add_definitions(-DCONFIG_DIR="${CONFIG_DIR}" diff --git a/src/control.cpp b/src/control.cpp new file mode 100644 index 0000000..5f8c400 --- /dev/null +++ b/src/control.cpp @@ -0,0 +1,91 @@ +/* + * Wormhole - Federated social network + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include "control.h" + +using CTLFunction_t = std::function; + +struct CTLOptions +{ + const std::string name; + const std::string desc; + CTLFunction_t func; +}; + +namespace CTL { + int create_user(ArgvVector_t) + { + return 0; + } + + int delete_user(ArgvVector_t) + { + return 0; + } + + int luser_local(ArgvVector_t) + { + return 0; + } +} + +void print_usage(const std::string& command, std::ostream& buf) +{ + buf << "Usage: " << command << " [command] [command-args]...\n"; +} + +int control_mode_init(ArgvVector_t args) +{ + // Put commands in here as CTLOptions struct + const std::array commands = { + CTLOptions { "create_user", "Creates a user", CTL::create_user }, + CTLOptions { "delete_user", "Deletes a user", CTL::delete_user }, + CTLOptions { "luser_local", "Lists all local users", CTL::luser_local }, + }; + + // Now do the thing + for (const CTLOptions& cmd : commands) + { + if (args[1] == cmd.name) + return cmd.func(args); + } + + // Command doesn't exist, display help message + print_usage(args[0], std::cout); + std::cout << "\n" << + "Commands available:\n"; + + size_t l = commands.size(); + for (const CTLOptions& cmd : commands) + { + // Long name tab wizardry + std::cout << " - " << cmd.name << (cmd.name.length() < 12 ? "\t" : "") << "\t\t" << + cmd.desc << (--l != 0 ? "\n" : ""); + } + + // Flush and leave + std::cout << std::endl; + + return EXIT_FAILURE; +} diff --git a/src/control.h b/src/control.h new file mode 100644 index 0000000..cf32b10 --- /dev/null +++ b/src/control.h @@ -0,0 +1,33 @@ +/* + * Wormhole - Federated social network + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include +#include +#include + +/* Control mode (aka. ctl mode) is a parameter controlled + * mode where the user can configure, create users, manipulate options, + * test things, get information, and much more about an instance while it (probably) + * isn't running. + */ + +using ArgvVector_t = const std::vector; + +int control_mode_init(ArgvVector_t args); diff --git a/src/main.cpp b/src/main.cpp index 5fb6741..eadea45 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,7 @@ #include #include #include +#include "control.h" #include "config/config_loader.h" #include "database/database.h" #include "database/sqlite/sqlite.h" @@ -154,10 +155,20 @@ int start_wormhole() return EXIT_SUCCESS; } -int main() +int main(int argc, char** argv) { - // Load wormhole up in another function incase we need to do any global inits here - int res = start_wormhole(); + int res = EXIT_SUCCESS; + // Start wormhole as is, no arguments needed + if (argc <= 1) + { + res = start_wormhole(); + } + // User provided an argument, entering control mode... + else { + // Convert to string + return control_mode_init(std::vector(argv, argv + argc)); + } + return res; }