scsictl shall accept generic key/value pairs for options that take parameters (#1240) (#1274)

* scsictl accepts generic key/value pairs for options that take parameters
This commit is contained in:
Uwe Seimet 2023-10-31 09:02:28 +01:00 committed by GitHub
parent 8bd06ea5cd
commit 7bbcf59c76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 4 deletions

View File

@ -207,8 +207,11 @@ int ScsiCtl::run(const vector<char *>& args) const
case 's':
command.set_operation(SERVER_INFO);
if (optarg) {
SetCommandParams(command, optarg);
if (optarg) {
if (const string error = SetCommandParams(command, optarg); !error.empty()) {
cerr << "Error: " << error << endl;
exit(EXIT_FAILURE);
}
}
break;

View File

@ -42,8 +42,12 @@ void protobuf_util::ParseParameters(PbDeviceDefinition& device, const string& pa
}
}
void protobuf_util::SetCommandParams(PbCommand& command, const string& params)
string protobuf_util::SetCommandParams(PbCommand& command, const string& params)
{
if (params.find(KEY_VALUE_SEPARATOR) != string::npos) {
return SetFromGenericParams(command, params);
}
string folder_pattern;
string file_pattern;
string operations;
@ -69,6 +73,23 @@ void protobuf_util::SetCommandParams(PbCommand& command, const string& params)
SetParam(command, "folder_pattern", folder_pattern);
SetParam(command, "file_pattern", file_pattern);
SetParam(command, "operations", operations);
return "";
}
string protobuf_util::SetFromGenericParams(PbCommand& command, const string& params)
{
for (const string& key_value : Split(params, COMPONENT_SEPARATOR)) {
const auto& param = Split(key_value, KEY_VALUE_SEPARATOR, 2);
if (param.size() > 1 && !param[0].empty()) {
SetParam(command, param[0], param[1]);
}
else {
return "Parameter '" + key_value + "' has to be a key/value pair";
}
}
return "";
}
void protobuf_util::SetProductData(PbDeviceDefinition& device, const string& data)

View File

@ -38,7 +38,8 @@ namespace protobuf_util
}
void ParseParameters(PbDeviceDefinition&, const string&);
void SetCommandParams(PbCommand&, const string&);
string SetCommandParams(PbCommand&, const string&);
string SetFromGenericParams(PbCommand&, const string&);
void SetProductData(PbDeviceDefinition&, const string&);
string SetIdAndLun(PbDeviceDefinition&, const string&);
string ListDevices(const vector<PbDevice>&);

View File

@ -87,6 +87,20 @@ TEST(ProtobufUtil, SetCommandParams)
EXPECT_EQ("operations", GetParam(command6, "operations"));
}
TEST(ProtobufUtil, SetFromGenericParams)
{
PbCommand command1;
EXPECT_TRUE(SetFromGenericParams(command1, "operations=mapping_info:folder_pattern=pattern").empty());
EXPECT_EQ("mapping_info", GetParam(command1, "operations"));
EXPECT_EQ("pattern", GetParam(command1, "folder_pattern"));
PbCommand command2;
EXPECT_FALSE(SetFromGenericParams(command2, "=mapping_info").empty());
PbCommand command3;
EXPECT_FALSE(SetFromGenericParams(command3, "=").empty());
}
TEST(ProtobufUtil, ListDevices)
{
vector<PbDevice> devices;