From a2d6a81f483283da5d050f93c36d1617d3ebac19 Mon Sep 17 00:00:00 2001 From: Wu Tao Date: Sun, 28 Oct 2018 22:53:21 +0800 Subject: [PATCH] shell: implement cc command (#196) --- src/shell/commands.h | 14 ++++++++++++++ src/shell/main.cpp | 44 +++++++++++++++++++++++++------------------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/shell/commands.h b/src/shell/commands.h index 6db3508..0ab5260 100644 --- a/src/shell/commands.h +++ b/src/shell/commands.h @@ -544,6 +544,20 @@ inline bool use_app_as_current(command_executor *e, shell_context *sc, arguments } } +extern void check_in_cluster(std::string cluster_name); + +inline bool cc_command(command_executor *e, shell_context *sc, arguments args) +{ + if (args.argc == 2) { + std::string cluster_name = args.argv[1]; + if (!cluster_name.empty()) { + check_in_cluster(cluster_name); + return true; + } + } + return false; +} + inline bool process_escape_all(command_executor *e, shell_context *sc, arguments args) { if (args.argc == 1) { diff --git a/src/shell/main.cpp b/src/shell/main.cpp index 47ef35b..02d2c0a 100644 --- a/src/shell/main.cpp +++ b/src/shell/main.cpp @@ -101,6 +101,9 @@ static command_executor commands[] = { "[app_name]", use_app_as_current, }, + { + "cc", "check in the specified cluster", "[cluster_name]", cc_command, + }, { "escape_all", "if escape all characters when printing key/value bytes", @@ -507,22 +510,8 @@ static char *hintsCallback(const char *buf, int *color, int *bold) /* Linenoise free hints callback. */ static void freeHintsCallback(void *ptr) { sdsfree((sds)ptr); } -void initialize(int argc, char **argv) +/*extern*/ void check_in_cluster(std::string cluster_name) { - std::cout << "Pegasus Shell " << PEGASUS_VERSION << std::endl; - std::cout << "Type \"help\" for more information." << std::endl; - std::cout << "Type \"Ctrl-D\" or \"Ctrl-C\" to exit the shell." << std::endl; - std::cout << std::endl; - - std::string config_file = argc > 1 ? argv[1] : "config.ini"; - if (!pegasus::pegasus_client_factory::initialize(config_file.c_str())) { - std::cout << "ERROR: init pegasus failed: " << config_file << std::endl; - dsn_exit(-1); - } else { - std::cout << "The config file is: " << config_file << std::endl; - } - - std::string cluster_name = argc > 2 ? argv[2] : "mycluster"; s_global_context.current_cluster_name = cluster_name; std::string section = "uri-resolver.dsn://" + s_global_context.current_cluster_name; std::string key = "arguments"; @@ -530,8 +519,8 @@ void initialize(int argc, char **argv) dsn::replication::replica_helper::load_meta_servers( s_global_context.meta_list, section.c_str(), key.c_str()); - s_global_context.ddl_client.reset( - new dsn::replication::replication_ddl_client(s_global_context.meta_list)); + s_global_context.ddl_client = + dsn::make_unique(s_global_context.meta_list); // get real cluster name from zk std::string name; @@ -541,6 +530,25 @@ void initialize(int argc, char **argv) } std::cout << "The cluster name is: " << cluster_name << std::endl; std::cout << "The cluster meta list is: " << server_list << std::endl; +} + +void initialize(int argc, char **argv) +{ + std::cout << "Pegasus Shell " << PEGASUS_VERSION << std::endl; + std::cout << "Type \"help\" for more information." << std::endl; + std::cout << "Type \"Ctrl-D\" or \"Ctrl-C\" to exit the shell." << std::endl; + std::cout << std::endl; + + std::string config_file = argc > 1 ? argv[1] : "config.ini"; + if (!pegasus::pegasus_client_factory::initialize(config_file.c_str())) { + std::cout << "ERROR: init pegasus failed: " << config_file << std::endl; + dsn_exit(-1); + } else { + std::cout << "The config file is: " << config_file << std::endl; + } + + std::string cluster_name = argc > 2 ? argv[2] : "mycluster"; + check_in_cluster(cluster_name); linenoiseSetMultiLine(1); linenoiseSetCompletionCallback(completionCallback); @@ -586,7 +594,6 @@ int main(int argc, char **argv) return 0; } -#if defined(__linux__) #include #include #include @@ -603,4 +610,3 @@ static char const rcsid[] = #endif ", built at " __DATE__ " " __TIME__ " $"; const char *pegasus_shell_rcsid() { return rcsid; } -#endif -- GitLab