38 #include <boost/algorithm/string.hpp>
39 #include <boost/tokenizer.hpp>
65 int thriftbroker::CommandInterpreter::execute_line(
const String &line) {
68 parse_line(line, parse);
76 if (parse.
args.empty())
77 parse.
args.push_back(
"contents");
78 display_help_text(parse.
args[0]);
86 catch (ThriftGen::ClientException &e) {
88 catch (std::exception &e) {
97 m_client->status(status);
100 cout <<
"ThriftBroker "
102 if (!status.text.empty())
103 cout <<
" - " << status.text;
118 void thriftbroker::CommandInterpreter::ParseResult::clear() {
125 void thriftbroker::CommandInterpreter::parse_line(
const String &line,
ParseResult &result)
const {
130 boost::char_separator<char> sep(
" ");
131 boost::tokenizer< boost::char_separator<char> > tokens(line, sep);
132 for (
const string& arg : tokens) {
133 if (command.empty()) {
136 else if (boost::algorithm::starts_with(arg,
"--seek=")) {
137 char *base = (
char *)arg.c_str() + 7;
140 result.
offset = strtoll(base, &end, 10);
141 if (errno != 0 || end == base) {
142 parse_error(command);
147 result.
args.push_back(arg);
153 if (!strcasecmp(command.c_str(),
"help")) {
156 else if (!strcasecmp(command.c_str(),
"shutdown")) {
157 if (result.
args.empty() ||
158 (result.
args.size() == 1 && !strcasecmp(result.
args[0].c_str(),
"now")))
161 parse_error(command);
163 else if (!strcasecmp(command.c_str(),
"status")) {
164 if (!result.
args.empty())
165 parse_error(command);
171 cout <<
"Unrecognized command - '" << command <<
"'" << endl;
173 HT_FATALF(
"Unrecognized command - '%s'", command.c_str());
181 const char *g_help_text_contents[] = {
182 "shutdown .............. Shutdown ThriftBroker",
183 "status ................ Get status of ThriftBroker",
185 "Statements must be terminated with ';'. For more information on",
186 "a specific statement, type 'help <statement>', where <statement> is from",
187 "the preceeding list.",
191 const char *g_help_text_shutdown[] = {
194 " This command sends a shutdown request to the filesystem broker.",
195 " If the 'now' argument is given, the broker will do an unclean",
196 " shutdown by exiting immediately. Otherwise, it will wait for",
197 " all pending requests to complete before shutting down.",
201 const char *g_help_text_status[] = {
204 " This command sends a status request to the filesystem broker, printing",
205 " the status output message to the console and returning the status code.",
206 " The return value of the last command issued to the interpreter will be",
207 " used as the exit status.",
212 void thriftbroker::CommandInterpreter::load_help_text() {
213 m_help_text[
"contents"] = g_help_text_contents;
214 m_help_text[
"shutdown"] = g_help_text_shutdown;
215 m_help_text[
"status"] = g_help_text_status;
219 void thriftbroker::CommandInterpreter::display_help_text(
const std::string &command)
const {
220 string lower(command);
221 boost::algorithm::to_lower(lower);
222 auto iter = m_help_text.find(lower);
223 if (iter != m_help_text.end()) {
224 const char **text = iter->second;
226 for (
size_t i=0; text[i]; i++)
227 cout << text[i] << endl;
231 cout << endl <<
"No help for '" << command <<
"'" << endl << endl;
234 void thriftbroker::CommandInterpreter::display_usage(
const std::string &command)
const {
235 string lower(command);
236 boost::algorithm::to_lower(lower);
237 auto iter = m_help_text.find(lower);
239 const char **text = iter->second;
240 cout <<
"Usage: " << text[0] << endl;
243 void thriftbroker::CommandInterpreter::parse_error(
const std::string &command)
const {
244 string lower(command);
245 boost::algorithm::to_lower(lower);
246 auto iter = m_help_text.find(lower);
248 const char **text = iter->second;
250 cout <<
"Parse error." << endl;
251 cout <<
"Usage: " << text[0] << endl;
std::string String
A String is simply a typedef to std::string.
Declaration for ConsoleOutputSquelcher.
static const char * code_to_string(Code code)
bool status(ContextPtr &context, Timer &timer, Status &status)
Runs a status check on the master.
Logging routines and macros.
Compatibility Macros for C/C++.
#define HT_FATALF(msg,...)
Temporarily redirects stdout and stderr to /dev/null.
#define HT_THROWF(_code_, _fmt_,...)
Error codes, Exception handling, error logging.
Declarations for CommandInterpreter.