From 7bbcf59c76f1aa68e7cbb71bbb5ff9a543d853cf Mon Sep 17 00:00:00 2001 From: Uwe Seimet <48174652+uweseimet@users.noreply.github.com> Date: Tue, 31 Oct 2023 09:02:28 +0100 Subject: [PATCH] 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 --- cpp/scsictl/scsictl_core.cpp | 7 +++++-- cpp/shared/protobuf_util.cpp | 23 ++++++++++++++++++++++- cpp/shared/protobuf_util.h | 3 ++- cpp/test/protobuf_util_test.cpp | 14 ++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/cpp/scsictl/scsictl_core.cpp b/cpp/scsictl/scsictl_core.cpp index 9efad57a..6f6b8860 100644 --- a/cpp/scsictl/scsictl_core.cpp +++ b/cpp/scsictl/scsictl_core.cpp @@ -207,8 +207,11 @@ int ScsiCtl::run(const vector& 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; diff --git a/cpp/shared/protobuf_util.cpp b/cpp/shared/protobuf_util.cpp index 17239bd0..e4c1b9b0 100644 --- a/cpp/shared/protobuf_util.cpp +++ b/cpp/shared/protobuf_util.cpp @@ -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) diff --git a/cpp/shared/protobuf_util.h b/cpp/shared/protobuf_util.h index cc155915..f07b0322 100644 --- a/cpp/shared/protobuf_util.h +++ b/cpp/shared/protobuf_util.h @@ -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&); diff --git a/cpp/test/protobuf_util_test.cpp b/cpp/test/protobuf_util_test.cpp index 2793a657..c6c9d4d8 100644 --- a/cpp/test/protobuf_util_test.cpp +++ b/cpp/test/protobuf_util_test.cpp @@ -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 devices;