67 rader
2.4 KiB
C
67 rader
2.4 KiB
C
|
#ifndef CLI_H
|
||
|
#define CLI_H
|
||
|
#include <iostream>
|
||
|
#include <string>
|
||
|
#include <vector>
|
||
|
#include "database/database.h"
|
||
|
#include "config.h"
|
||
|
|
||
|
class CLIHandler
|
||
|
{
|
||
|
struct cmd
|
||
|
{
|
||
|
std::string name;
|
||
|
std::string helptext;
|
||
|
unsigned int required_args;
|
||
|
std::vector<cmd> subCommands;
|
||
|
std::function<std::pair<bool, std::string>(CLIHandler *, const std::vector<std::string> &)> func;
|
||
|
};
|
||
|
|
||
|
private:
|
||
|
Database *db;
|
||
|
Config *conf;
|
||
|
|
||
|
protected:
|
||
|
std::pair<bool, std::string> attach(const std::vector<std::string> &args);
|
||
|
std::pair<bool, std::string> cli_help(const std::vector<std::string> &args);
|
||
|
std::pair<bool, std::string> user_add(const std::vector<std::string> &args);
|
||
|
std::pair<bool, std::string> user_change_pw(const std::vector<std::string> &args);
|
||
|
std::pair<bool, std::string> user_rename(const std::vector<std::string> &args);
|
||
|
std::pair<bool, std::string> user_set_perms(const std::vector<std::string> &args);
|
||
|
std::pair<bool, std::string> user_list(const std::vector<std::string> &args);
|
||
|
std::pair<bool, std::string> user_show(const std::vector<std::string> &args);
|
||
|
|
||
|
std::vector<struct cmd> cmds{
|
||
|
{{"user",
|
||
|
"user operations on the database",
|
||
|
1,
|
||
|
{{{"add", "[user] [password] - creates a user", 2, {}, &CLIHandler::user_add},
|
||
|
{"changepw", "[user] [password] - changes the password of user", 2, {}, &CLIHandler::user_change_pw},
|
||
|
{"rename", "[user] [new name] - renames a user", 2, {}, &CLIHandler::user_rename},
|
||
|
{"setperms", "[user] [perms] - sets the permissions of the user", 2, {}, &CLIHandler::user_set_perms},
|
||
|
{"list", "- lists users", 0, {}, &CLIHandler::user_list},
|
||
|
{"show", "[user] - show detailed information about user", 1, {}, &CLIHandler::user_show}}},
|
||
|
&CLIHandler::cli_help},
|
||
|
{"exit",
|
||
|
"exit cli",
|
||
|
0,
|
||
|
{},
|
||
|
[](CLIHandler *, const std::vector<std::string> &args) -> std::pair<bool, std::string>
|
||
|
{
|
||
|
exit(EXIT_SUCCESS);
|
||
|
return {true, ""};
|
||
|
}},
|
||
|
{"help", "print this help", 0, {}, &CLIHandler::cli_help},
|
||
|
{"attach", "attach to running instance", 0, {}, &CLIHandler::attach}}};
|
||
|
|
||
|
std::pair<bool, std::string> processCommand(const std::vector<CLIHandler::cmd> &commands, std::string cmd,
|
||
|
const std::vector<std::string> &args);
|
||
|
|
||
|
public:
|
||
|
CLIHandler(Config &config, Database &d);
|
||
|
std::pair<bool, std::string> processCommand(std::string cmd, const std::vector<std::string> &args);
|
||
|
static std::pair<std::string, std::vector<std::string>> splitCommand(std::string input);
|
||
|
};
|
||
|
|
||
|
#endif // CLI_H
|