@@ -72,6 +72,43 @@ uint32_t ImplicitAclCategories(uint32_t mask) {
72
72
return out;
73
73
}
74
74
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
+
75
112
} // namespace
76
113
77
114
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 {
133
170
}
134
171
135
172
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);
147
174
148
175
for (string name : GetFlag (FLAGS_restricted_commands)) {
149
176
restricted_cmds_.emplace (AsciiStrToUpper (name));
@@ -165,8 +192,7 @@ CommandRegistry& CommandRegistry::operator<<(CommandId cmd) {
165
192
166
193
absl::InlinedVector<std::string_view, 2 > maybe_subcommand = StrSplit (cmd.name (), " " );
167
194
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 ()) {
170
196
if (it->second .empty ()) {
171
197
return *this ; // Incase of empty string we want to remove the command from registry.
172
198
}
0 commit comments