Wormhole control options

FossilOrigin-Name: bc55df0989e94415349d692793cfa1aa645c8b3677c35ef039810dd2ed891c30
This commit is contained in:
nekobit 2022-10-18 21:46:18 +00:00
parent eb3d8afe15
commit 2b973ad4fd
4 changed files with 139 additions and 3 deletions

View file

@ -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}"

91
src/control.cpp Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <functional>
#include <utility>
#include <iostream>
#include <string_view>
#include <iomanip>
#include "control.h"
using CTLFunction_t = std::function<int(ArgvVector_t)>;
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<CTLOptions, 3> 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;
}

33
src/control.h Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include <string>
#include <getopt.h>
/* 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<std::string>;
int control_mode_init(ArgvVector_t args);

View file

@ -20,6 +20,7 @@
#include <cstdlib>
#include <memory>
#include <any>
#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<std::string>(argv, argv + argc));
}
return res;
}