Skip to content

Commit

Permalink
scsictl shall accept generic key/value pairs for options that take pa…
Browse files Browse the repository at this point in the history
…rameters (#1240) (#1274)

* scsictl accepts generic key/value pairs for options that take parameters
  • Loading branch information
uweseimet authored Oct 31, 2023
1 parent 8bd06ea commit 7bbcf59
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
7 changes: 5 additions & 2 deletions cpp/scsictl/scsictl_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
23 changes: 22 additions & 1 deletion cpp/shared/protobuf_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion cpp/shared/protobuf_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>&);
Expand Down
14 changes: 14 additions & 0 deletions cpp/test/protobuf_util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7bbcf59

Please sign in to comment.