]> git.stg.codes - stg.git/blobdiff - projects/sgconf/main.cpp
Fixed version info.
[stg.git] / projects / sgconf / main.cpp
index e10d21f0b7ec41c41ff8756ca71923b0ed11c5b6..625e8564a5ce0ad7842b242a34b272d4a0ec6e75 100644 (file)
  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
  */
 
-#include "request.h"
-#include "common_sg.h"
-#include "sg_error_codes.h"
-
 #include "xml.h"
 #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"
@@ -94,6 +94,16 @@ binder0<F> bind0(const F & func, const typename F::argument_type & arg)
 return binder0<F>(func, arg);
 }
 
+template <typename A, typename R>
+class FUNC1_ADAPTER : public std::unary_function<A, R>
+{
+    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 <typename C, typename A, typename R>
 class METHOD1_ADAPTER : public std::unary_function<A, R>
 {
@@ -116,6 +126,12 @@ class CONST_METHOD1_ADAPTER : public std::unary_function<A, R>
         C & m_obj;
 };
 
+template <typename A, typename R>
+FUNC1_ADAPTER<A, R> Func1Adapt(R (func)(A))
+{
+return FUNC1_ADAPTER<A, R>(func);
+}
+
 template <typename C, typename A, typename R>
 METHOD1_ADAPTER<C, A, R> Method1Adapt(R (C::* func)(A), C & obj)
 {
@@ -148,17 +164,10 @@ if (!result)
 SGCONF::PrintXML(response);
 }
 
-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 Version(const std::string & self)
+{
+std::cout << self << ", version: 2.0.0-alpha.\n";
+}
 
 void ReadUserConfigFile(SGCONF::OPTION_BLOCK & block)
 {
@@ -405,6 +414,7 @@ time_t stgTime;
 //-----------------------------------------------------------------------------
 int main(int argc, char **argv)
 {
+std::string self(basename(argv[0]));
 SGCONF::CONFIG config;
 SGCONF::COMMANDS commands;
 
@@ -412,8 +422,8 @@ SGCONF::OPTION_BLOCKS blocks;
 blocks.Add("General options")
       .Add("c", "config", SGCONF::MakeParamAction(config.configFile, std::string("~/.config/stg/sgconf.conf"), "<config file>"), "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"), "<address>"), "\t\thost to connect")
       .Add("p", "port", SGCONF::MakeParamAction(config.port, uint16_t(5555), "<port>"), "\t\tport to connect")
@@ -424,22 +434,36 @@ blocks.Add("Raw XML")
       .Add("r", "raw", SGCONF::MakeAPIAction(commands, "<xml>", 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, "<login>", true, SGCONF::GetAdminFunction), "\tget admin")
-      .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::AddAdminFunction), "\tadd admin")
-      .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::DelAdminFunction), "\tdel admin")
-      .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::ChgAdminFunction), "\tchange admin");
+      .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::GetAdminFunction), "get admin")
+      .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::AddAdminFunction), "add admin")
+      .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::DelAdminFunction), "del admin")
+      .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", 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, "<name>", true, SGCONF::GetTariffFunction), "\tget tariff")
-      .Add("add-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::AddTariffFunction), "\tadd tariff")
-      .Add("del-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::DelTariffFunction), "\tdel tariff")
-      .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::ChgTariffFunction), "\tchange tariff");
+      .Add("get-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::GetTariffFunction), "get tariff")
+      .Add("add-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::AddTariffFunction), "add tariff")
+      .Add("del-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::DelTariffFunction), "del tariff")
+      .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", 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, "<name>", true, SGCONF::GetUserFunction), "\tget user")
-      .Add("add-user", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::AddUserFunction), "\tadd user")
-      .Add("del-user", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::DelUserFunction), "\tdel user")
-      .Add("chg-user", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::ChgUserFunction), "\tchange user");
+      .Add("get-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::GetUserFunction), "get user")
+      .Add("add-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::AddUserFunction), "add user")
+      .Add("del-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::DelUserFunction), "del user")
+      .Add("chg-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::ChgUserFunction), "change user")
+      .Add("check-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::CheckUserFunction), "check user existance and credentials")
+      .Add("send-message", SGCONF::MakeAPIAction(commands, "<login>", 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, "<name>", true, SGCONF::GetServiceFunction), "get service")
+      .Add("add-service", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::AddServiceFunction), "add service")
+      .Add("del-service", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::DelServiceFunction), "del service")
+      .Add("chg-service", SGCONF::MakeAPIAction(commands, "<name>", 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, "<name>", true, SGCONF::GetCorpFunction), "get corporation")
+      .Add("add-corp", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::AddCorpFunction), "add corporation")
+      .Add("del-corp", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::DelCorpFunction), "del corporation")
+      .Add("chg-corp", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::ChgCorpFunction), "change corporation");
 
 SGCONF::PARSER_STATE state(false, argc, argv);
 
@@ -488,109 +512,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 <config file>\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 <address>\t\t\thost to connect (ip or domain name, default: \"localhost\")\n"
-          << "\t-p, --port <port>\t\t\tport to connect (default: \"5555\")\n"
-          << "\t-u, --username <username>\t\tadministrative login (default: \"admin\")\n"
-          << "\t-w, --userpass <password>\t\tpassword for administrative login\n"
-          << "\t-a, --address <connection string>\tconnection params as a single string in format: <login>:<password>@<host>:<port>\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 <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 <login>\t\t\tlogin of the admin to add\n"
-              << "\t\t--password <password>\t\tpassword of the admin to add\n"
-              << "\t\t--priv <priv number>\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 <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 <login>\t\t\tlogin of the admin to change\n"
-              << "\t\t--priv <priv number>\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";
@@ -726,11 +654,6 @@ if (full)
     std::cout << "\t\t--name <name>\t\t\tname of the corporation to change\n"
               << "\t\t--add-cash <amount>[:<message>]\tadd cash to the corporation's account and optional comment message\n"
               << "\t\t--set-cash <cash>[:<message>]\tnew corporation's cash and optional comment message\n\n";
-}
-
-void Version()
-{
-std::cout << "sgconf, version: 2.0.0-alpha.\n";
-}
+}*/
 
 } // namespace anonymous