#include "sg_error_codes.h"
#include "xml.h"
+#include "admins.h"
#include "options.h"
#include "actions.h"
#include "config.h"
return true;
}
-void RawXMLCallback(bool result, const std::string & reason, const std::string & response, void * data)
+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);
+SGCONF::PrintXML(response);
}
void Usage();
class CONFIG_ACTION : public ACTION
{
public:
- CONFIG_ACTION(CONFIG & config,
+ CONFIG_ACTION(SGCONF::CONFIG & config,
const std::string & paramDescription)
: m_config(config),
m_description(paramDescription)
virtual PARSER_STATE Parse(int argc, char ** argv);
private:
- CONFIG & m_config;
+ SGCONF::CONFIG & m_config;
std::string m_description;
OPTION_BLOCK m_suboptions;
void ParseHostAndPort(const std::string & hostAndPort);
};
+typedef bool (* API_FUNCTION) (const SGCONF::CONFIG &,
+ const std::string &,
+ const std::map<std::string, std::string> &);
+
+class COMMAND
+{
+ public:
+ COMMAND(API_FUNCTION funPtr,
+ const std::string & arg,
+ const std::map<std::string, std::string> & options)
+ : m_funPtr(funPtr),
+ m_arg(arg),
+ m_options(options)
+ {}
+ bool Execute(const SGCONF::CONFIG & config) const
+ {
+ return m_funPtr(config, m_arg, m_options);
+ }
+
+ private:
+ API_FUNCTION m_funPtr;
+ std::string m_arg;
+ std::map<std::string, std::string> m_options;
+};
+
+class COMMANDS
+{
+ public:
+ void Add(API_FUNCTION funPtr,
+ const std::string & arg,
+ const std::map<std::string, std::string> & options) { m_commands.push_back(COMMAND(funPtr, arg, options)); }
+ bool Execute(const SGCONF::CONFIG & config) const
+ {
+ std::list<COMMAND>::const_iterator it(m_commands.begin());
+ bool res = true;
+ while (it != m_commands.end() && res)
+ {
+ res = res && it->Execute(config);
+ ++it;
+ }
+ return res;
+ }
+ private:
+ std::list<COMMAND> m_commands;
+};
+
+class API_ACTION : public ACTION
+{
+ public:
+ API_ACTION(COMMANDS & commands,
+ const std::string & paramDescription,
+ bool needArgument,
+ const OPTION_BLOCK& suboptions,
+ API_FUNCTION funPtr)
+ : m_commands(commands),
+ m_description(paramDescription),
+ m_argument(needArgument ? "1" : ""), // Hack
+ m_suboptions(suboptions),
+ m_funPtr(funPtr)
+ {}
+ API_ACTION(COMMANDS & commands,
+ const std::string & paramDescription,
+ bool needArgument,
+ API_FUNCTION funPtr)
+ : m_commands(commands),
+ m_description(paramDescription),
+ m_argument(needArgument ? "1" : ""), // Hack
+ m_funPtr(funPtr)
+ {}
+
+ virtual ACTION * Clone() const { return new API_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.empty())
+ {
+ if (argc == 0 ||
+ argv == NULL ||
+ *argv == NULL)
+ throw ERROR("Missing argument.");
+ m_argument = *argv;
+ --state.argc;
+ ++state.argv;
+ }
+ m_suboptions.Parse(state.argc, state.argv);
+ m_commands.Add(m_funPtr, m_argument, m_params);
+ return state;
+ }
+
+ private:
+ COMMANDS & m_commands;
+ std::string m_description;
+ std::string m_argument;
+ OPTION_BLOCK m_suboptions;
+ std::map<std::string, std::string> m_params;
+ API_FUNCTION m_funPtr;
+};
+
PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv)
{
if (argc == 0 ||
}
inline
-CONFIG_ACTION * MakeParamAction(CONFIG & config,
+CONFIG_ACTION * MakeParamAction(SGCONF::CONFIG & config,
const std::string & paramDescription)
{
return new CONFIG_ACTION(config, paramDescription);
}
+inline
+ACTION * MakeAPIAction(COMMANDS & commands,
+ const std::string & paramDescription,
+ bool needArgument,
+ API_FUNCTION funPtr)
+{
+return new API_ACTION(commands, paramDescription, needArgument, funPtr);
+}
+
+inline
+ACTION * MakeAPIAction(COMMANDS & commands,
+ API_FUNCTION funPtr)
+{
+return new API_ACTION(commands, "", false, funPtr);
+}
+
+bool RawXMLFunction(const SGCONF::CONFIG & config,
+ const std::string & arg,
+ const std::map<std::string, std::string> & /*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;
int main(int argc, char **argv)
{
SGCONF::CONFIG config;
+SGCONF::COMMANDS commands;
SGCONF::OPTION_BLOCKS blocks;
blocks.Add("General options")
.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());*/
+ .Add("r", "raw", SGCONF::MakeAPIAction(commands, "<xml>", true, SGCONF::RawXMLFunction), "\tmake raw XML request");
+blocks.Add("Admins 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");
SGCONF::PARSER_STATE state(false, argc, argv);
}
std::cerr << "Config: " << config.Serialize() << std::endl;
+return commands.Execute(config) ? 0 : -1;
-return 0;
+/*return 0;
if (argc < 2)
{
UsageConf();
exit(PARAMETER_PARSING_ERR_CODE);
}
-return UNKNOWN_ERR_CODE;
+return UNKNOWN_ERR_CODE;*/
}
//-----------------------------------------------------------------------------