X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/5a98249bfadbe1d22d316dc3e9e770e0c305e5d6..186fb2b18b89a0280b8cc4b5ea8f7a59d3bd0853:/projects/sgconf/main.cpp diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index 135cd3c0..6a71f652 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -23,48 +23,23 @@ #include "admins.h" #include "tariffs.h" #include "users.h" +#include "services.h" +#include "corps.h" + #include "options.h" #include "actions.h" #include "config.h" -#include "stg/servconf.h" -#include "stg/user_conf.h" -#include "stg/user_stat.h" -#include "stg/common.h" - -#include -#include -#include -#include -#include #include -#include - -#include -#include -#include -#include +#include -namespace -{ - -template -struct ARRAY_TYPE -{ -typedef typename T::value_type type; -}; +#include // getenv +#include // basename -template -struct ARRAY_TYPE -{ -typedef T type; -}; +#include // access -template -struct ARRAY_TYPE +namespace { -typedef T type; -}; template struct nullary_function @@ -91,6 +66,16 @@ binder0 bind0(const F & func, const typename F::argument_type & arg) return binder0(func, arg); } +template +class FUNC1_ADAPTER : public std::unary_function +{ + public: + FUNC1_ADAPTER(R (*func)(A)) : m_func(func) {} + const R operator()(A arg) const { return (m_func)(arg); } + private: + R (*m_func)(A); +}; + template class METHOD1_ADAPTER : public std::unary_function { @@ -113,6 +98,12 @@ class CONST_METHOD1_ADAPTER : public std::unary_function C & m_obj; }; +template +FUNC1_ADAPTER Func1Adapt(R (func)(A)) +{ +return FUNC1_ADAPTER(func); +} + template METHOD1_ADAPTER Method1Adapt(R (C::* func)(A), C & obj) { @@ -125,38 +116,11 @@ CONST_METHOD1_ADAPTER Method1Adapt(R (C::* func)(A) const, C & obj) return CONST_METHOD1_ADAPTER(func, obj); } -template -bool SetArrayItem(T & array, const char * index, const typename ARRAY_TYPE::type & value) +void Version(const std::string & self) { -size_t pos = 0; -if (str2x(index, pos)) - return false; -array[pos] = value; -return true; -} - -void RawXMLCallback(bool result, const std::string & reason, const std::string & response, void * /*data*/) -{ -if (!result) - { - std::cerr << "Failed to get raw XML response. Reason: '" << reason << "'." << std::endl; - return; - } -SGCONF::PrintXML(response); +std::cout << self << ", version: 2.0.0-alpha.\n"; } -void Usage(); -void UsageAll(); -void UsageImpl(bool full); -void UsageConnection(); -void UsageAdmins(bool full); -void UsageTariffs(bool full); -void UsageUsers(bool full); -void UsageServices(bool full); -void UsageCorporations(bool full); - -void Version(); - void ReadUserConfigFile(SGCONF::OPTION_BLOCK & block) { std::vector paths; @@ -384,17 +348,6 @@ ACTION * MakeAPIAction(COMMANDS & commands, return new API_ACTION(commands, "", false, funPtr); } -bool RawXMLFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) -{ - STG::SERVCONF proto(config.server.data(), - config.port.data(), - config.userName.data(), - config.userPass.data()); - return proto.RawXML(arg, RawXMLCallback, NULL) == STG::st_ok; -} - } // namespace SGCONF time_t stgTime; @@ -402,6 +355,7 @@ time_t stgTime; //----------------------------------------------------------------------------- int main(int argc, char **argv) { +std::string self(basename(argv[0])); SGCONF::CONFIG config; SGCONF::COMMANDS commands; @@ -409,8 +363,8 @@ SGCONF::OPTION_BLOCKS blocks; blocks.Add("General options") .Add("c", "config", SGCONF::MakeParamAction(config.configFile, std::string("~/.config/stg/sgconf.conf"), ""), "override default config file") .Add("h", "help", SGCONF::MakeFunc0Action(bind0(Method1Adapt(&SGCONF::OPTION_BLOCKS::Help, blocks), 0)), "\t\tshow this help and exit") - .Add("help-all", SGCONF::MakeFunc0Action(UsageAll), "\t\tshow full help and exit") - .Add("v", "version", SGCONF::MakeFunc0Action(Version), "\t\tshow version information and exit"); + //.Add("help-all", SGCONF::MakeFunc0Action(UsageAll), "\t\tshow full help and exit") + .Add("v", "version", SGCONF::MakeFunc0Action(bind0(Func1Adapt(Version), self)), "\t\tshow version information and exit"); SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options") .Add("s", "server", SGCONF::MakeParamAction(config.server, std::string("localhost"), "
"), "\t\thost to connect") .Add("p", "port", SGCONF::MakeParamAction(config.port, uint16_t(5555), ""), "\t\tport to connect") @@ -421,22 +375,36 @@ blocks.Add("Raw XML") .Add("r", "raw", SGCONF::MakeAPIAction(commands, "", true, SGCONF::RawXMLFunction), "\tmake raw XML request"); blocks.Add("Admin management options") .Add("get-admins", SGCONF::MakeAPIAction(commands, SGCONF::GetAdminsFunction), "\tget admin list") - .Add("get-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetAdminFunction), "\tget admin") - .Add("add-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddAdminFunction), "\tadd admin") - .Add("del-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelAdminFunction), "\tdel admin") - .Add("chg-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgAdminFunction), "\tchange admin"); + .Add("get-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetAdminFunction), "get admin") + .Add("add-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddAdminFunction), "add admin") + .Add("del-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelAdminFunction), "del admin") + .Add("chg-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgAdminFunction), "change admin"); blocks.Add("Tariff management options") .Add("get-tariffs", SGCONF::MakeAPIAction(commands, SGCONF::GetTariffsFunction), "\tget tariff list") - .Add("get-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetTariffFunction), "\tget tariff") - .Add("add-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddTariffFunction), "\tadd tariff") - .Add("del-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelTariffFunction), "\tdel tariff") - .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgTariffFunction), "\tchange tariff"); + .Add("get-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetTariffFunction), "get tariff") + .Add("add-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddTariffFunction), "add tariff") + .Add("del-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelTariffFunction), "del tariff") + .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgTariffFunction), "change tariff"); blocks.Add("User management options") .Add("get-users", SGCONF::MakeAPIAction(commands, SGCONF::GetUsersFunction), "\tget user list") - .Add("get-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetUserFunction), "\tget user") - .Add("add-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddUserFunction), "\tadd user") - .Add("del-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelUserFunction), "\tdel user") - .Add("chg-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgUserFunction), "\tchange user"); + .Add("get-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetUserFunction), "get user") + .Add("add-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddUserFunction), "add user") + .Add("del-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelUserFunction), "del user") + .Add("chg-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgUserFunction), "change user") + .Add("check-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::CheckUserFunction), "check user existance and credentials") + .Add("send-message", SGCONF::MakeAPIAction(commands, "", true, SGCONF::SendMessageFunction), "send message"); +blocks.Add("Service management options") + .Add("get-services", SGCONF::MakeAPIAction(commands, SGCONF::GetServicesFunction), "\tget service list") + .Add("get-service", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetServiceFunction), "get service") + .Add("add-service", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddServiceFunction), "add service") + .Add("del-service", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelServiceFunction), "del service") + .Add("chg-service", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgServiceFunction), "change service"); +blocks.Add("Corporation management options") + .Add("get-corps", SGCONF::MakeAPIAction(commands, SGCONF::GetCorpsFunction), "\tget corporation list") + .Add("get-corp", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetCorpFunction), "get corporation") + .Add("add-corp", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddCorpFunction), "add corporation") + .Add("del-corp", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelCorpFunction), "del corporation") + .Add("chg-corp", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgCorpFunction), "change corporation"); SGCONF::PARSER_STATE state(false, argc, argv); @@ -485,109 +453,13 @@ return -1; std::cerr << "Config: " << config.Serialize() << std::endl; return commands.Execute(config) ? 0 : -1; - -/*return 0; - -if (argc < 2) - { - Usage(); - return 1; - } - -if (argc <= 2) - { - UsageConf(); - exit(PARAMETER_PARSING_ERR_CODE); - } - -if (strcmp(argv[1], "get") == 0) - { - //printf("get\n"); - return mainGet(argc - 1, argv + 1); - } -else if (strcmp(argv[1], "set") == 0) - { - //printf("set\n"); - if (mainSet(argc - 1, argv + 1) ) - return 0; - return -1; - } -else - { - UsageConf(); - exit(PARAMETER_PARSING_ERR_CODE); - } -return UNKNOWN_ERR_CODE;*/ } //----------------------------------------------------------------------------- namespace { -void Usage() -{ -UsageImpl(false); -} - -void UsageAll() -{ -UsageImpl(true); -} - -void UsageImpl(bool full) -{ -std::cout << "sgconf is the Stargazer management utility.\n\n" - << "Usage:\n" - << "\tsgconf [options]\n\n" - << "General options:\n" - << "\t-c, --config \t\toverride default config file (default: \"~/.config/stg/sgconf.conf\")\n" - << "\t-h, --help\t\t\t\tshow this help and exit\n" - << "\t--help-all\t\t\t\tshow full help and exit\n" - << "\t-v, --version\t\t\t\tshow version information and exit\n\n"; -UsageConnection(); -UsageAdmins(full); -UsageTariffs(full); -UsageUsers(full); -UsageServices(full); -UsageCorporations(full); -} -//----------------------------------------------------------------------------- -void UsageConnection() -{ -std::cout << "Connection options:\n" - << "\t-s, --server
\t\t\thost to connect (ip or domain name, default: \"localhost\")\n" - << "\t-p, --port \t\t\tport to connect (default: \"5555\")\n" - << "\t-u, --username \t\tadministrative login (default: \"admin\")\n" - << "\t-w, --userpass \t\tpassword for administrative login\n" - << "\t-a, --address \tconnection params as a single string in format: :@:\n\n"; -} -//----------------------------------------------------------------------------- -void UsageAdmins(bool full) -{ -std::cout << "Admins management options:\n" - << "\t--get-admins\t\t\t\tget a list of admins (subsequent options will define what to show)\n"; -if (full) - std::cout << "\t\t--login\t\t\t\tshow admin's login\n" - << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n"; -std::cout << "\t--get-admin\t\t\t\tget the information about admin\n"; -if (full) - std::cout << "\t\t--login \t\t\tlogin of the admin to show\n" - << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n"; -std::cout << "\t--add-admin\t\t\t\tadd a new admin\n"; -if (full) - std::cout << "\t\t--login \t\t\tlogin of the admin to add\n" - << "\t\t--password \t\tpassword of the admin to add\n" - << "\t\t--priv \t\tpriviledges of the admin to add\n\n"; -std::cout << "\t--del-admin\t\t\t\tdelete an existing admin\n"; -if (full) - std::cout << "\t\t--login \t\t\tlogin of the admin to delete\n\n"; -std::cout << "\t--chg-admin\t\t\t\tchange an existing admin\n"; -if (full) - std::cout << "\t\t--login \t\t\tlogin of the admin to change\n" - << "\t\t--priv \t\tnew priviledges\n\n"; -} -//----------------------------------------------------------------------------- -void UsageTariffs(bool full) +/*void UsageTariffs(bool full) { std::cout << "Tariffs management options:\n" << "\t--get-tariffs\t\t\t\tget a list of tariffs (subsequent options will define what to show)\n"; @@ -723,11 +595,6 @@ if (full) std::cout << "\t\t--name \t\t\tname of the corporation to change\n" << "\t\t--add-cash [:]\tadd cash to the corporation's account and optional comment message\n" << "\t\t--set-cash [:]\tnew corporation's cash and optional comment message\n\n"; -} - -void Version() -{ -std::cout << "sgconf, version: 2.0.0-alpha.\n"; -} +}*/ } // namespace anonymous