This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
refactor: restrict remote-command to only one command name #809
Open
neverchanje
wants to merge
5
commits into
XiaoMi:master
Choose a base branch
from
neverchanje:fixmetacmd
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e2cbb60
refactor: refine help command
neverchanje c11a49f
Merge branch 'master' of https://github.com/XiaoMi/rdsn into fixmetacmd
neverchanje 7d73816
fix: revert unnecessary modifications
neverchanje 1901f57
Merge branch 'master' of https://github.com/XiaoMi/rdsn into fixmetacmd
neverchanje f0585b5
remove redundant comment
neverchanje File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,11 +25,12 @@ | |
*/ | ||
|
||
#include <iostream> | ||
#include <thread> | ||
#include <sstream> | ||
#include <utility> | ||
|
||
#include <dsn/utility/utils.h> | ||
#include <dsn/tool-api/command_manager.h> | ||
#include <dsn/dist/fmt_logging.h> | ||
|
||
namespace dsn { | ||
|
||
|
@@ -38,29 +39,19 @@ dsn_handle_t command_manager::register_command(const std::vector<std::string> &c | |
const std::string &help_long, | ||
command_handler handler) | ||
{ | ||
utils::auto_write_lock l(_lock); | ||
bool is_valid_cmd = false; | ||
dcheck_eq(commands.size(), 1); | ||
const std::string &cmd = commands[0]; | ||
dassert(!cmd.empty(), "should not register empty command"); | ||
|
||
for (const std::string &cmd : commands) { | ||
if (!cmd.empty()) { | ||
is_valid_cmd = true; | ||
auto it = _handlers.find(cmd); | ||
dassert(it == _handlers.end(), "command '%s' already regisered", cmd.c_str()); | ||
} | ||
} | ||
dassert(is_valid_cmd, "should not register empty command"); | ||
utils::auto_write_lock l(_lock); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
command_instance *c = new command_instance(); | ||
auto c = new command_instance(); | ||
c->commands = commands; | ||
c->help_long = help_long; | ||
c->help_short = help_one_line; | ||
c->handler = handler; | ||
c->handler = std::move(handler); | ||
|
||
for (const std::string &cmd : commands) { | ||
if (!cmd.empty()) { | ||
_handlers[cmd] = c; | ||
} | ||
} | ||
_handlers[cmd] = c; | ||
return c; | ||
} | ||
|
||
|
@@ -97,15 +88,15 @@ bool command_manager::run_command(const std::string &cmd, | |
|
||
command_manager::command_manager() | ||
{ | ||
register_command({"help", "h", "H", "Help"}, | ||
"help|Help|h|H [command] - display help information", | ||
register_command({"help"}, | ||
"help - display help information", | ||
"", | ||
[this](const std::vector<std::string> &args) { | ||
std::stringstream ss; | ||
|
||
if (args.size() == 0) { | ||
utils::auto_read_lock l(_lock); | ||
for (const auto &c : this->_handlers) { | ||
for (const auto &c : _handlers) { | ||
ss << c.second->help_short << std::endl; | ||
} | ||
} else { | ||
|
@@ -123,52 +114,6 @@ command_manager::command_manager() | |
|
||
return ss.str(); | ||
}); | ||
|
||
register_command( | ||
{"repeat", "r", "R", "Repeat"}, | ||
"repeat|Repeat|r|R interval_seconds max_count command - execute command periodically", | ||
"repeat|Repeat|r|R interval_seconds max_count command - execute command every interval " | ||
"seconds, to the max count as max_count (0 for infinite)", | ||
[this](const std::vector<std::string> &args) { | ||
std::stringstream ss; | ||
|
||
if (args.size() < 3) { | ||
return "insufficient arguments"; | ||
} | ||
|
||
int interval_seconds = atoi(args[0].c_str()); | ||
if (interval_seconds <= 0) { | ||
return "invalid interval argument"; | ||
} | ||
|
||
int max_count = atoi(args[1].c_str()); | ||
if (max_count < 0) { | ||
return "invalid max count"; | ||
} | ||
|
||
if (max_count == 0) { | ||
max_count = std::numeric_limits<int>::max(); | ||
} | ||
|
||
std::string cmd = args[2]; | ||
std::vector<std::string> largs; | ||
for (int i = 3; i < (int)args.size(); i++) { | ||
largs.push_back(args[i]); | ||
} | ||
|
||
for (int i = 0; i < max_count; i++) { | ||
std::string output; | ||
auto r = this->run_command(cmd, largs, output); | ||
|
||
if (!r) { | ||
break; | ||
} | ||
|
||
std::this_thread::sleep_for(std::chrono::seconds(interval_seconds)); | ||
} | ||
|
||
return "repeat command completed"; | ||
}); | ||
} | ||
|
||
command_manager::~command_manager() { _handlers.clear(); } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,40 +24,39 @@ | |||||
* THE SOFTWARE. | ||||||
*/ | ||||||
|
||||||
/* | ||||||
* Description: | ||||||
* Unit-test for command_manager. | ||||||
* | ||||||
* Revision history: | ||||||
* Nov., 2015, @qinzuoyan (Zuoyan Qin), first version | ||||||
* xxxx-xx-xx, author, fix bug about xxx | ||||||
*/ | ||||||
|
||||||
#include <dsn/tool-api/command_manager.h> | ||||||
#include <gtest/gtest.h> | ||||||
|
||||||
using namespace ::dsn; | ||||||
namespace dsn { | ||||||
|
||||||
void command_manager_module_init() | ||||||
struct command_manager_test : public ::testing::Test | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
dsn::command_manager::instance().register_command( | ||||||
{"test-cmd"}, | ||||||
"test-cmd - just for command_manager unit-test", | ||||||
"test-cmd arg1 arg2 ...", | ||||||
[](const std::vector<std::string> &args) { | ||||||
std::stringstream ss; | ||||||
ss << "test-cmd response: ["; | ||||||
for (size_t i = 0; i < args.size(); ++i) { | ||||||
if (i != 0) | ||||||
ss << " "; | ||||||
ss << args[i]; | ||||||
} | ||||||
ss << "]"; | ||||||
return ss.str(); | ||||||
}); | ||||||
} | ||||||
command_manager_test() | ||||||
{ | ||||||
_test_cmd = command_manager::instance().register_command( | ||||||
{"test-cmd"}, | ||||||
"just for command_manager unit-test", | ||||||
"test-cmd arg1 arg2 ...", | ||||||
[](const std::vector<std::string> &args) { | ||||||
std::stringstream ss; | ||||||
ss << "test-cmd response: ["; | ||||||
for (size_t i = 0; i < args.size(); ++i) { | ||||||
if (i != 0) | ||||||
ss << " "; | ||||||
ss << args[i]; | ||||||
} | ||||||
ss << "]"; | ||||||
return ss.str(); | ||||||
}); | ||||||
} | ||||||
|
||||||
~command_manager_test() override { command_manager::instance().deregister_command(_test_cmd); } | ||||||
|
||||||
TEST(command_manager, exist_command) | ||||||
private: | ||||||
dsn_handle_t _test_cmd; | ||||||
}; | ||||||
|
||||||
TEST_F(command_manager_test, exist_command) | ||||||
{ | ||||||
const std::string cmd = "test-cmd"; | ||||||
const std::vector<std::string> cmd_args{"this", "is", "test", "argument"}; | ||||||
|
@@ -68,7 +67,7 @@ TEST(command_manager, exist_command) | |||||
ASSERT_EQ(output, expect_output); | ||||||
} | ||||||
|
||||||
TEST(command_manager, not_exist_command) | ||||||
TEST_F(command_manager_test, not_exist_command) | ||||||
{ | ||||||
const std::string cmd = "not-exist-cmd"; | ||||||
const std::vector<std::string> cmd_args{"arg1", "arg2"}; | ||||||
|
@@ -78,3 +77,5 @@ TEST(command_manager, not_exist_command) | |||||
std::string expect_output = std::string("unknown command '") + cmd + "'"; | ||||||
ASSERT_EQ(output, expect_output); | ||||||
} | ||||||
|
||||||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So
commands
can be replaced byconst std::string &command
?