Skip to content

Commit fffd079

Browse files
committed
chore: Add helper to parse map style cmd line args
The helper will be used to parse renamed commands as well as aliases. Signed-off-by: Abhijat Malviya <[email protected]>
1 parent 753c25e commit fffd079

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

src/server/command_registry.cc

+39-13
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,43 @@ uint32_t ImplicitAclCategories(uint32_t mask) {
7272
return out;
7373
}
7474

75+
absl::flat_hash_map<std::string, std::string> ParseCmdlineArgMap(
76+
const absl::Flag<std::vector<std::string>>& flag, const bool allow_duplicates = false) {
77+
const auto& mappings = absl::GetFlag(flag);
78+
absl::flat_hash_map<std::string, std::string> parsed_mappings;
79+
parsed_mappings.reserve(mappings.size());
80+
81+
for (const std::string& mapping : mappings) {
82+
if (mapping.find('=') == std::string::npos) {
83+
LOG(ERROR) << "Malformed command " << mapping << " for " << flag.Name()
84+
<< ", expected key=value";
85+
exit(1);
86+
}
87+
88+
const std::vector<std::string> kv = absl::StrSplit(mapping, '=');
89+
if (kv.size() > 2) {
90+
LOG(ERROR) << "Malformed command " << mapping << " for " << flag.Name()
91+
<< ", expected key=value";
92+
exit(1);
93+
}
94+
95+
const std::string key = absl::AsciiStrToUpper(std::move(kv[0]));
96+
const std::string value = kv.size() == 1 ? "" : absl::AsciiStrToUpper(std::move(kv[1]));
97+
98+
if (key == value) {
99+
LOG(ERROR) << "Invalid attempt to map " << key << " to itself in " << flag.Name();
100+
exit(1);
101+
}
102+
103+
const bool inserted = parsed_mappings.emplace(std::move(key), std::move(value)).second;
104+
if (!allow_duplicates && !inserted) {
105+
LOG(ERROR) << "Duplicate insert to " << flag.Name() << " not allowed";
106+
exit(1);
107+
}
108+
}
109+
return parsed_mappings;
110+
}
111+
75112
} // namespace
76113

77114
CommandId::CommandId(const char* name, uint32_t mask, int8_t arity, int8_t first_key,
@@ -133,17 +170,7 @@ optional<facade::ErrorReply> CommandId::Validate(CmdArgList tail_args) const {
133170
}
134171

135172
CommandRegistry::CommandRegistry() {
136-
vector<string> rename_command = GetFlag(FLAGS_rename_command);
137-
138-
for (string command_data : rename_command) {
139-
pair<string_view, string_view> kv = StrSplit(command_data, '=');
140-
auto [_, inserted] =
141-
cmd_rename_map_.emplace(AsciiStrToUpper(kv.first), AsciiStrToUpper(kv.second));
142-
if (!inserted) {
143-
LOG(ERROR) << "Invalid rename_command flag, trying to give 2 names to a command";
144-
exit(1);
145-
}
146-
}
173+
cmd_rename_map_ = ParseCmdlineArgMap(FLAGS_rename_command);
147174

148175
for (string name : GetFlag(FLAGS_restricted_commands)) {
149176
restricted_cmds_.emplace(AsciiStrToUpper(name));
@@ -165,8 +192,7 @@ CommandRegistry& CommandRegistry::operator<<(CommandId cmd) {
165192

166193
absl::InlinedVector<std::string_view, 2> maybe_subcommand = StrSplit(cmd.name(), " ");
167194
const bool is_sub_command = maybe_subcommand.size() == 2;
168-
auto it = cmd_rename_map_.find(maybe_subcommand.front());
169-
if (it != cmd_rename_map_.end()) {
195+
if (const auto it = cmd_rename_map_.find(maybe_subcommand.front()); it != cmd_rename_map_.end()) {
170196
if (it->second.empty()) {
171197
return *this; // Incase of empty string we want to remove the command from registry.
172198
}

0 commit comments

Comments
 (0)