]> git.stg.codes - stg.git/blobdiff - projects/sgconf/main.cpp
Merge branch 'master' into naffanya-dev
[stg.git] / projects / sgconf / main.cpp
index a39f57cee6b7887b3b995101109c67823553018d..9025602e541288294bc25bf7d7880f2befb5f3b8 100644 (file)
@@ -23,6 +23,7 @@
 #include "common_sg.h"
 #include "sg_error_codes.h"
 
+#include "xml.h"
 #include "options.h"
 #include "actions.h"
 #include "config.h"
@@ -134,6 +135,16 @@ 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;
+    }
+PrintXML(response);
+}
+
 void Usage();
 void UsageAll();
 void UsageImpl(bool full);
@@ -198,6 +209,66 @@ class CONFIG_ACTION : public ACTION
         void ParseHostAndPort(const std::string & hostAndPort);
 };
 
+class COMMAND_FUNCTOR
+{
+    public:
+        virtual ~COMMAND_FUNCTOR() {}
+        virtual bool operator()(const std::string& arg, const std::map<std::string, std::string>& options) = 0;
+};
+
+class COMMAND_ACTION : public ACTION
+{
+    public:
+        COMMAND_ACTION(CONFIG & config,
+                       const std::string & paramDescription,
+                       bool needArgument,
+                       const OPTION_BLOCK& suboptions,
+                       COMMAND_FUNCTOR* funPtr)
+            : m_config(config),
+              m_description(paramDescription),
+              m_argument(needArgument),
+              m_suboptions(suboptions),
+              m_funPtr(funPtr)
+        {}
+
+        virtual ACTION * Clone() const { return new COMMAND_ACTION(*this); }
+
+        virtual std::string ParamDescription() const { return m_description; }
+        virtual std::string DefaultDescription() const { return ""; }
+        virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
+        virtual PARSER_STATE Parse(int argc, char ** argv)
+        {
+        PARSER_STATE state(false, argc, argv);
+        if (m_argument)
+            {
+            if (argc == 0 ||
+                argv == NULL ||
+                *argv == NULL)
+                throw ERROR("Missing argument.");
+            m_argument = *argv;
+            --state.argc;
+            ++state.argv;
+            }
+        std::list<OPTION_BLOCK>::iterator it(m_suboptions.begin());
+        while (!state.stop && it != m_suboptions.end())
+            {
+            state = it->Parse(state.argc, state.argv);
+            ++it;
+            }
+        m_funPtr(m_argument, m_params);
+        return state;
+        }
+
+    private:
+        CONFIG & m_config;
+        std::string m_description;
+        bool m_needArgument;
+        std::string m_argument;
+        OPTION_BLOCK m_suboptions;
+        std::map<std::string, std::string> m_params;
+        COMMAND_FUNCTOR* m_funPtr;
+};
+
 PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv)
 {
 if (argc == 0 ||
@@ -1239,6 +1310,14 @@ SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options")
       .Add("u", "username", SGCONF::MakeParamAction(config.userName, std::string("admin"), "<username>"), "\tadministrative login")
       .Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, "<password>"), "\tpassword for the administrative login")
       .Add("a", "address", SGCONF::MakeParamAction(config, "<connection string>"), "connection params as a single string in format: <login>:<password>@<host>:<port>");
+blocks.Add("Raw XML")
+      .Add("r", "raw", SGCONF::MakeFunc1Action(), "\t\tmake raw XML request")
+/*blocks.Add("Admins management options")
+      .Add("get-admins", SGCONF::MakeConfAction())
+      .Add("get-admin", SGCONF::MakeConfAction())
+      .Add("add-admin", SGCONF::MakeConfAction())
+      .Add("del-admin", SGCONF::MakeConfAction())
+      .Add("chg-admin", SGCONF::MakeConfAction());*/
 
 
 SGCONF::PARSER_STATE state(false, argc, argv);
@@ -1280,7 +1359,7 @@ else
 
 config = configOverride;
 }
-catch (const SGCONF::OPTION::ERROR& ex)
+catch (const std::exception& ex)
 {
 std::cerr << ex.what() << "\n";
 return -1;