This is an automated email from the ASF dual-hosted git repository.

dmeden pushed a commit to branch 10-Dev
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/10-Dev by this push:
     new dc51e9a4f traffic_ctl: Tidy up. (#9030)
dc51e9a4f is described below

commit dc51e9a4f1c08253f02746ae85a51e0c4e687dcd
Author: Damian Meden <[email protected]>
AuthorDate: Mon Aug 22 11:27:26 2022 -0300

    traffic_ctl: Tidy up. (#9030)
    
    - Remove unnecessary copy for the arguments object.
    - Use scoped constant strings.
    - Remove unnecessary functions.
    - Make errors clear in case of logic error.
---
 src/traffic_ctl/CtrlCommands.cc | 170 ++++++++++++++++++----------------------
 src/traffic_ctl/CtrlCommands.h  |  81 +++++++++++++------
 src/traffic_ctl/traffic_ctl.cc  |  16 ++--
 3 files changed, 141 insertions(+), 126 deletions(-)

diff --git a/src/traffic_ctl/CtrlCommands.cc b/src/traffic_ctl/CtrlCommands.cc
index 35e8b4fae..7db72f734 100644
--- a/src/traffic_ctl/CtrlCommands.cc
+++ b/src/traffic_ctl/CtrlCommands.cc
@@ -36,15 +36,15 @@ const std::unordered_map<std::string_view, 
BasePrinter::Options::OutputFormat> _
   {"rpc", BasePrinter::Options::OutputFormat::RPC}};
 
 BasePrinter::Options::OutputFormat
-parse_format(ts::Arguments &args)
+parse_format(ts::Arguments *args)
 {
-  if (args.get("records")) {
+  if (args->get("records")) {
     return BasePrinter::Options::OutputFormat::RECORDS;
   }
 
   BasePrinter::Options::OutputFormat 
val{BasePrinter::Options::OutputFormat::LEGACY};
 
-  if (auto data = args.get("format"); data) {
+  if (auto data = args->get("format"); data) {
     ts::TextView fmt{data.value()};
     if (auto search = _Fmt_str_to_enum.find(fmt); search != 
std::end(_Fmt_str_to_enum)) {
       val = search->second;
@@ -54,20 +54,22 @@ parse_format(ts::Arguments &args)
 }
 
 BasePrinter::Options
-parse_print_opts(ts::Arguments &args)
+parse_print_opts(ts::Arguments *args)
 {
   return {parse_format(args)};
 }
 } // namespace
 
 
//------------------------------------------------------------------------------------------------------------------------------------
-CtrlCommand::CtrlCommand(ts::Arguments args) : _arguments(args) {}
+CtrlCommand::CtrlCommand(ts::Arguments *args) : _arguments(args) {}
 
 void
 CtrlCommand::execute()
 {
   if (_invoked_func) {
     _invoked_func();
+  } else {
+    throw std::logic_error("CtrlCommand::execute(): Internal error. There 
should be a function to invoke. (_invoked_func not set)");
   }
 }
 
@@ -107,41 +109,34 @@ CtrlCommand::invoke_rpc(shared::rpc::ClientRequest const 
&request, std::string &
   resp                       = invoke_rpc(encodedRequest);
 }
 // 
-----------------------------------------------------------------------------------------------------------------------------------
-RecordCommand::RecordCommand(ts::Arguments args) : CtrlCommand(args) {}
-
-void
-RecordCommand::execute()
+ConfigCommand::ConfigCommand(ts::Arguments *args) : RecordCommand(args)
 {
-  execute_subcommand();
-}
-ConfigCommand::ConfigCommand(ts::Arguments args) : RecordCommand(args)
-{
-  BasePrinter::Options printOpts{parse_print_opts(_arguments)};
-  if (args.get("match")) {
+  BasePrinter::Options printOpts{parse_print_opts(args)};
+  if (args->get(MATCH_STR)) {
     _printer      = std::make_unique<RecordPrinter>(printOpts);
     _invoked_func = [&]() { config_match(); };
-  } else if (args.get("get")) {
+  } else if (args->get(GET_STR)) {
     _printer      = std::make_unique<RecordPrinter>(printOpts);
     _invoked_func = [&]() { config_get(); };
-  } else if (args.get("diff")) {
+  } else if (args->get(DIFF_STR)) {
     _printer      = std::make_unique<DiffConfigPrinter>(printOpts);
     _invoked_func = [&]() { config_diff(); };
-  } else if (args.get("describe")) {
+  } else if (args->get(DESCRIBE_STR)) {
     _printer      = std::make_unique<RecordDescribePrinter>(printOpts);
     _invoked_func = [&]() { config_describe(); };
-  } else if (args.get("defaults")) {
+  } else if (args->get(DEFAULTS_STR)) {
     _printer      = std::make_unique<RecordPrinter>(printOpts);
     _invoked_func = [&]() { config_defaults(); };
-  } else if (args.get("set")) {
+  } else if (args->get(SET_STR)) {
     _printer      = std::make_unique<ConfigSetPrinter>(printOpts);
     _invoked_func = [&]() { config_set(); };
-  } else if (args.get("status")) {
+  } else if (args->get(STATUS_STR)) {
     _printer      = std::make_unique<RecordPrinter>(printOpts);
     _invoked_func = [&]() { config_status(); };
-  } else if (args.get("reload")) {
+  } else if (args->get(RELOAD_STR)) {
     _printer      = std::make_unique<ConfigReloadPrinter>(printOpts);
     _invoked_func = [&]() { config_reload(); };
-  } else if (args.get("registry")) {
+  } else if (args->get(REGISTRY_STR)) {
     _printer      = std::make_unique<ConfigShowFileRegistryPrinter>(printOpts);
     _invoked_func = [&]() { config_show_file_registry(); };
   } else {
@@ -149,14 +144,6 @@ ConfigCommand::ConfigCommand(ts::Arguments args) : 
RecordCommand(args)
   }
 }
 
-void
-ConfigCommand::execute_subcommand()
-{
-  if (_invoked_func) {
-    _invoked_func();
-  }
-}
-
 shared::rpc::JSONRPCResponse
 RecordCommand::record_fetch(ts::ArgumentData argData, bool isRegex, 
RecordQueryType recQueryType)
 {
@@ -171,19 +158,19 @@ RecordCommand::record_fetch(ts::ArgumentData argData, 
bool isRegex, RecordQueryT
 void
 ConfigCommand::config_match()
 {
-  _printer->write_output(record_fetch(_arguments.get("match"), 
shared::rpc::REGEX, RecordQueryType::CONFIG));
+  _printer->write_output(record_fetch(get_parsed_arguments()->get(MATCH_STR), 
shared::rpc::REGEX, RecordQueryType::CONFIG));
 }
 
 void
 ConfigCommand::config_get()
 {
-  _printer->write_output(record_fetch(_arguments.get("get"), 
shared::rpc::NOT_REGEX, RecordQueryType::CONFIG));
+  _printer->write_output(record_fetch(get_parsed_arguments()->get(GET_STR), 
shared::rpc::NOT_REGEX, RecordQueryType::CONFIG));
 }
 
 void
 ConfigCommand::config_describe()
 {
-  _printer->write_output(record_fetch(_arguments.get("describe"), 
shared::rpc::NOT_REGEX, RecordQueryType::CONFIG));
+  
_printer->write_output(record_fetch(get_parsed_arguments()->get(DESCRIBE_STR), 
shared::rpc::NOT_REGEX, RecordQueryType::CONFIG));
 }
 void
 ConfigCommand::config_defaults()
@@ -211,7 +198,7 @@ ConfigCommand::config_status()
 void
 ConfigCommand::config_set()
 {
-  auto const &data = _arguments.get("set");
+  auto const &data = get_parsed_arguments()->get(SET_STR);
   ConfigSetRecordRequest request{{data[0], data[1]}};
   shared::rpc::JSONRPCResponse response = invoke_rpc(request);
 
@@ -228,52 +215,43 @@ ConfigCommand::config_show_file_registry()
   _printer->write_output(invoke_rpc(ConfigShowFileRegistryRequest{}));
 }
 
//------------------------------------------------------------------------------------------------------------------------------------
-MetricCommand::MetricCommand(ts::Arguments args) : RecordCommand(args)
+MetricCommand::MetricCommand(ts::Arguments *args) : RecordCommand(args)
 {
-  // auto const fmt = parse_format(_arguments);
-  BasePrinter::Options printOpts{parse_print_opts(_arguments)};
-  if (args.get("match")) {
+  BasePrinter::Options printOpts{parse_print_opts(args)};
+  if (args->get(MATCH_STR)) {
     _printer      = std::make_unique<MetricRecordPrinter>(printOpts);
     _invoked_func = [&]() { metric_match(); };
-  } else if (args.get("get")) {
+  } else if (args->get(GET_STR)) {
     _printer      = std::make_unique<MetricRecordPrinter>(printOpts);
     _invoked_func = [&]() { metric_get(); };
-  } else if (args.get("describe")) {
+  } else if (args->get(DESCRIBE_STR)) {
     _printer      = std::make_unique<RecordDescribePrinter>(printOpts);
     _invoked_func = [&]() { metric_describe(); };
-  } else if (args.get("clear")) {
+  } else if (args->get(CLEAR_STR)) {
     _printer      = std::make_unique<GenericPrinter>(printOpts);
     _invoked_func = [&]() { metric_clear(); };
-  } else if (args.get("zero")) {
+  } else if (args->get(ZERO_STR)) {
     _printer      = std::make_unique<GenericPrinter>(printOpts);
     _invoked_func = [&]() { metric_zero(); };
   }
 }
 
-void
-MetricCommand::execute_subcommand()
-{
-  if (_invoked_func) {
-    _invoked_func();
-  }
-}
-
 void
 MetricCommand::metric_get()
 {
-  _printer->write_output(record_fetch(_arguments.get("get"), 
shared::rpc::NOT_REGEX, RecordQueryType::METRIC));
+  _printer->write_output(record_fetch(get_parsed_arguments()->get(GET_STR), 
shared::rpc::NOT_REGEX, RecordQueryType::METRIC));
 }
 
 void
 MetricCommand::metric_match()
 {
-  _printer->write_output(record_fetch(_arguments.get("match"), 
shared::rpc::REGEX, RecordQueryType::METRIC));
+  _printer->write_output(record_fetch(get_parsed_arguments()->get(MATCH_STR), 
shared::rpc::REGEX, RecordQueryType::METRIC));
 }
 
 void
 MetricCommand::metric_describe()
 {
-  _printer->write_output(record_fetch(_arguments.get("describe"), 
shared::rpc::NOT_REGEX, RecordQueryType::METRIC));
+  
_printer->write_output(record_fetch(get_parsed_arguments()->get(DESCRIBE_STR), 
shared::rpc::NOT_REGEX, RecordQueryType::METRIC));
 }
 
 void
@@ -285,7 +263,7 @@ MetricCommand::metric_clear()
 void
 MetricCommand::metric_zero()
 {
-  auto records = _arguments.get("zero");
+  auto records = get_parsed_arguments()->get(ZERO_STR);
   ClearMetricRequest request{// names
                              {{std::begin(records), std::end(records)}}};
 
@@ -293,16 +271,16 @@ MetricCommand::metric_zero()
 }
 
//------------------------------------------------------------------------------------------------------------------------------------
 // TODO, let call the super const
-HostCommand::HostCommand(ts::Arguments args) : CtrlCommand(args)
+HostCommand::HostCommand(ts::Arguments *args) : CtrlCommand(args)
 {
-  BasePrinter::Options printOpts{parse_print_opts(_arguments)};
-  if (_arguments.get("status")) {
+  BasePrinter::Options printOpts{parse_print_opts(args)};
+  if (get_parsed_arguments()->get(STATUS_STR)) {
     _printer      = std::make_unique<GetHostStatusPrinter>(printOpts);
     _invoked_func = [&]() { status_get(); };
-  } else if (_arguments.get("down")) {
+  } else if (get_parsed_arguments()->get(DOWN_STR)) {
     _printer      = std::make_unique<SetHostStatusPrinter>(printOpts);
     _invoked_func = [&]() { status_down(); };
-  } else if (_arguments.get("up")) {
+  } else if (get_parsed_arguments()->get(UP_STR)) {
     _printer      = std::make_unique<SetHostStatusPrinter>(printOpts);
     _invoked_func = [&]() { status_up(); };
   }
@@ -311,7 +289,7 @@ HostCommand::HostCommand(ts::Arguments args) : 
CtrlCommand(args)
 void
 HostCommand::status_get()
 {
-  auto const &data = _arguments.get("status");
+  auto const &data = get_parsed_arguments()->get(STATUS_STR);
   HostGetStatusRequest request{{std::begin(data), std::end(data)}};
 
   auto response = invoke_rpc(request);
@@ -322,9 +300,11 @@ HostCommand::status_get()
 void
 HostCommand::status_down()
 {
-  auto hosts = _arguments.get("down");
-  HostSetStatusRequest request{
-    {HostSetStatusRequest::Params::Op::DOWN, {std::begin(hosts), 
std::end(hosts)}, _arguments.get("reason").value(), "0"}};
+  auto hosts = get_parsed_arguments()->get(DOWN_STR);
+  HostSetStatusRequest request{{HostSetStatusRequest::Params::Op::DOWN,
+                                {std::begin(hosts), std::end(hosts)},
+                                
get_parsed_arguments()->get(REASON_STR).value(),
+                                "0"}};
   auto response = invoke_rpc(request);
   _printer->write_output(response);
 }
@@ -332,26 +312,28 @@ HostCommand::status_down()
 void
 HostCommand::status_up()
 {
-  auto hosts = _arguments.get("up");
-  HostSetStatusRequest request{
-    {HostSetStatusRequest::Params::Op::UP, {std::begin(hosts), 
std::end(hosts)}, _arguments.get("reason").value(), "0"}};
+  auto hosts = get_parsed_arguments()->get(UP_STR);
+  HostSetStatusRequest request{{HostSetStatusRequest::Params::Op::UP,
+                                {std::begin(hosts), std::end(hosts)},
+                                
get_parsed_arguments()->get(REASON_STR).value(),
+                                "0"}};
 
   auto response = invoke_rpc(request);
   _printer->write_output(response);
 }
 
//------------------------------------------------------------------------------------------------------------------------------------
-PluginCommand::PluginCommand(ts::Arguments args) : CtrlCommand(args)
+PluginCommand::PluginCommand(ts::Arguments *args) : CtrlCommand(args)
 {
-  if (_arguments.get("msg")) {
+  if (get_parsed_arguments()->get(MSG_STR)) {
     _invoked_func = [&]() { plugin_msg(); };
   }
-  _printer = std::make_unique<GenericPrinter>(parse_print_opts(_arguments));
+  _printer = std::make_unique<GenericPrinter>(parse_print_opts(args));
 }
 
 void
 PluginCommand::plugin_msg()
 {
-  auto msgs = _arguments.get("msg");
+  auto msgs = get_parsed_arguments()->get(MSG_STR);
   BasicPluginMessageRequest::Params params;
   params.tag = msgs[0];
   if (msgs.size() > 1) {
@@ -362,19 +344,19 @@ PluginCommand::plugin_msg()
   auto response = invoke_rpc(request);
 }
 
//------------------------------------------------------------------------------------------------------------------------------------
-DirectRPCCommand::DirectRPCCommand(ts::Arguments args) : CtrlCommand(args)
+DirectRPCCommand::DirectRPCCommand(ts::Arguments *args) : CtrlCommand(args)
 {
-  BasePrinter::Options printOpts{parse_print_opts(_arguments)};
+  BasePrinter::Options printOpts{parse_print_opts(args)};
 
-  if (_arguments.get("get-api")) {
+  if (get_parsed_arguments()->get(GET_API_STR)) {
     _printer      = std::make_unique<RPCAPIPrinter>(printOpts);
     _invoked_func = [&]() { get_rpc_api(); };
     return;
-  } else if (_arguments.get("file")) {
+  } else if (get_parsed_arguments()->get(FILE_STR)) {
     _invoked_func = [&]() { from_file_request(); };
-  } else if (_arguments.get("input")) {
+  } else if (get_parsed_arguments()->get(INPUT_STR)) {
     _invoked_func = [&]() { read_from_input(); };
-  } else if (_arguments.get("invoke")) {
+  } else if (get_parsed_arguments()->get(INVOKE_STR)) {
     _invoked_func = [&]() { invoke_method(); };
     if (printOpts._format == BasePrinter::Options::OutputFormat::LEGACY) {
       // overwrite this and let it drop json instead.
@@ -401,7 +383,7 @@ void
 DirectRPCCommand::from_file_request()
 {
   // TODO: remove all the output messages from here if possible
-  auto filenames = _arguments.get("file");
+  auto filenames = get_parsed_arguments()->get(FILE_STR);
   for (auto &&filename : filenames) {
     std::string text;
     // run some basic validation on the passed files, they should
@@ -447,7 +429,7 @@ DirectRPCCommand::read_from_input()
     std::cin >> std::noskipws;
     // read cin.
     std::string content((std::istreambuf_iterator<char>(std::cin)), 
std::istreambuf_iterator<char>());
-    if (!_arguments.get("raw") && !validate_input(content)) {
+    if (!get_parsed_arguments()->get(RAW_STR) && !validate_input(content)) {
       _printer->write_output(ts::bwprint(text, "Content not accepted. 
expecting a valid sequence or structure\n"));
       return;
     }
@@ -463,12 +445,12 @@ void
 DirectRPCCommand::invoke_method()
 {
   shared::rpc::ClientRequest request;
-  if (auto method = _arguments.get("invoke"); method) {
+  if (auto method = get_parsed_arguments()->get(INVOKE_STR); method) {
     request.method = method.value();
     // We build up the parameter content if passed.
-    if (auto params = _arguments.get("params"); params) {
+    if (auto params = get_parsed_arguments()->get(PARAMS_STR); params) {
       std::ostringstream ss;
-      for (auto &&param : _arguments.get("params")) {
+      for (auto &&param : params) {
         ss << param;
         ss << '\n';
       }
@@ -479,10 +461,10 @@ DirectRPCCommand::invoke_method()
 }
 
 
//------------------------------------------------------------------------------------------------------------------------------------
-ServerCommand::ServerCommand(ts::Arguments args) : CtrlCommand(args)
+ServerCommand::ServerCommand(ts::Arguments *args) : CtrlCommand(args)
 {
-  BasePrinter::Options printOpts{parse_print_opts(_arguments)};
-  if (_arguments.get("drain")) {
+  BasePrinter::Options printOpts{parse_print_opts(args)};
+  if (get_parsed_arguments()->get(DRAIN_STR)) {
     _printer      = std::make_unique<GenericPrinter>(printOpts);
     _invoked_func = [&]() { server_drain(); };
   }
@@ -494,10 +476,10 @@ ServerCommand::server_drain()
   shared::rpc::JSONRPCResponse response;
   // TODO, can call_request take a && ?? if needed in the cmd just pass by ref.
 
-  if (_arguments.get("undo")) {
+  if (get_parsed_arguments()->get(UNDO_STR)) {
     response = invoke_rpc(ServerStopDrainRequest{});
   } else {
-    bool newConn = _arguments.get("no-new-connection");
+    const bool newConn = get_parsed_arguments()->get(NO_NEW_CONN_STR);
     ServerStartDrainRequest request{{newConn}};
     response = invoke_rpc(request);
   }
@@ -505,13 +487,13 @@ ServerCommand::server_drain()
   _printer->write_output(response);
 }
 // 
//------------------------------------------------------------------------------------------------------------------------------------
-StorageCommand::StorageCommand(ts::Arguments args) : CtrlCommand(args)
+StorageCommand::StorageCommand(ts::Arguments *args) : CtrlCommand(args)
 {
-  BasePrinter::Options printOpts{parse_print_opts(_arguments)};
-  if (_arguments.get("status")) {
+  BasePrinter::Options printOpts{parse_print_opts(args)};
+  if (get_parsed_arguments()->get(STATUS_STR)) {
     _printer      = std::make_unique<CacheDiskStoragePrinter>(printOpts);
     _invoked_func = [&]() { get_storage_status(); };
-  } else if (_arguments.get("offline")) {
+  } else if (get_parsed_arguments()->get(OFFLINE_STR)) {
     _printer      = 
std::make_unique<CacheDiskStorageOfflinePrinter>(printOpts);
     _invoked_func = [&]() { set_storage_offline(); };
   }
@@ -520,7 +502,7 @@ StorageCommand::StorageCommand(ts::Arguments args) : 
CtrlCommand(args)
 void
 StorageCommand::get_storage_status()
 {
-  auto disks = _arguments.get("status");
+  auto disks = get_parsed_arguments()->get(STATUS_STR);
   GetStorageDeviceStatusRequest request{{{std::begin(disks), 
std::end(disks)}}};
   auto response = invoke_rpc(request);
   _printer->write_output(response);
@@ -528,9 +510,9 @@ StorageCommand::get_storage_status()
 void
 StorageCommand::set_storage_offline()
 {
-  auto disks = _arguments.get("offline");
+  auto disks = get_parsed_arguments()->get(OFFLINE_STR);
   SetStorageDeviceOfflineRequest request{{{std::begin(disks), 
std::end(disks)}}};
   auto response = invoke_rpc(request);
   _printer->write_output(response);
 }
-// 
//------------------------------------------------------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------------------------------------------------------------
diff --git a/src/traffic_ctl/CtrlCommands.h b/src/traffic_ctl/CtrlCommands.h
index 7943b3ef9..3fc9a2d38 100644
--- a/src/traffic_ctl/CtrlCommands.h
+++ b/src/traffic_ctl/CtrlCommands.h
@@ -38,12 +38,14 @@ public:
   virtual ~CtrlCommand() = default;
 
   /// @brief This object will hold the arguments for now.
-  CtrlCommand(ts::Arguments args);
+  /// @note If you don't need to handle the args in your derived class you 
should just inherit CtrlCommand ctor:
+  ///       'using CtrlCommand::CtrlCommand;'
+  CtrlCommand(ts::Arguments *args);
 
   /// @brief Main execution point for a particular command. This function will 
invoke @c _invoked_func which should be set
   ///        by the derived class. In case you do not want the @c 
_invoked_func to be called directly, you should override this
-  ///        member function and call it yourself. @c RecordCommand does it 
and forwards the call to his childrens.
-  ///        If @c _invoked_func is not properly set, the function will not be 
called.
+  ///        member function and call it yourself.
+  ///        If @c _invoked_func is not properly set, the function will throw 
@c logic_error
   virtual void execute();
 
 protected:
@@ -59,10 +61,10 @@ protected:
   /// @param A Client request.
   /// @return A server response.
   shared::rpc::JSONRPCResponse invoke_rpc(shared::rpc::ClientRequest const 
&request);
+
   /// @brief Function that calls the rpc server. The response will not be 
decoded, it will be a raw string.
   void invoke_rpc(shared::rpc::ClientRequest const &request, std::string &bw);
 
-  ts::Arguments _arguments;              //!< parsed traffic_ctl arguments.
   std::unique_ptr<BasePrinter> _printer; //!< Specific output formatter. This 
should be created by the derived class.
 
   /// @brief The whole design is that the command will execute the @c 
_invoked_func once invoked. This function ptr should be
@@ -70,8 +72,16 @@ protected:
   ///        the execute() function and do something else. Check @c 
RecordCommand as an example.
   std::function<void(void)> _invoked_func; //!< Actual function that the 
command will execute.
 
+  /// @brief Return the parsed arguments.
+  ts::Arguments *
+  get_parsed_arguments()
+  {
+    return _arguments;
+  }
+
 private:
-  shared::rpc::RPCClient _rpcClient; //!< RPC socket client implementation.
+  ts::Arguments *_arguments = nullptr; //!< parsed traffic_ctl arguments.
+  shared::rpc::RPCClient _rpcClient;   //!< RPC socket client implementation.
 };
 
 // 
-----------------------------------------------------------------------------------------------------------------------------------
@@ -82,13 +92,14 @@ private:
 class RecordCommand : public CtrlCommand
 {
 public:
+  using CtrlCommand::CtrlCommand;
   virtual ~RecordCommand() = default;
-  /// @brief RecordCommand constructor.
-  RecordCommand(ts::Arguments args);
-  /// @brief We will override this function as we want to call 
execute_subcommand() in the derived class.
-  void execute() override;
 
 protected:
+  static inline const std::string MATCH_STR{"match"};
+  static inline const std::string GET_STR{"get"};
+  static inline const std::string DESCRIBE_STR{"describe"};
+
   /// @brief Handy enum to hold which kind of records we are requesting.
   enum class RecordQueryType { CONFIG = 0, METRIC };
   /// @brief Function to fetch record from the rpc server.
@@ -96,16 +107,17 @@ protected:
   /// @param isRegex if the request should be done by regex or name.
   /// @param recQueryType Config or Metric.
   shared::rpc::JSONRPCResponse record_fetch(ts::ArgumentData argData, bool 
isRegex, RecordQueryType recQueryType);
-
-  /// @brief To be override
-  virtual void
-  execute_subcommand()
-  {
-  }
 };
 // 
-----------------------------------------------------------------------------------------------------------------------------------
 class ConfigCommand : public RecordCommand
 {
+  static inline const std::string DIFF_STR{"diff"};
+  static inline const std::string DEFAULTS_STR{"defaults"};
+  static inline const std::string SET_STR{"set"};
+  static inline const std::string STATUS_STR{"status"};
+  static inline const std::string RELOAD_STR{"reload"};
+  static inline const std::string REGISTRY_STR{"registry"};
+
   void config_match();
   void config_get();
   void config_describe();
@@ -117,12 +129,14 @@ class ConfigCommand : public RecordCommand
   void config_show_file_registry();
 
 public:
-  ConfigCommand(ts::Arguments args);
-  void execute_subcommand() override;
+  ConfigCommand(ts::Arguments *args);
 };
 // 
-----------------------------------------------------------------------------------------------------------------------------------
 class MetricCommand : public RecordCommand
 {
+  static inline const std::string CLEAR_STR{"clear"};
+  static inline const std::string ZERO_STR{"zero"};
+
   void metric_get();
   void metric_match();
   void metric_describe();
@@ -130,16 +144,20 @@ class MetricCommand : public RecordCommand
   void metric_zero();
 
 public:
-  MetricCommand(ts::Arguments args);
-  void execute_subcommand() override;
+  MetricCommand(ts::Arguments *args);
 };
 // 
-----------------------------------------------------------------------------------------------------------------------------------
 class HostCommand : public CtrlCommand
 {
 public:
-  HostCommand(ts::Arguments args);
+  HostCommand(ts::Arguments *args);
 
 private:
+  static inline const std::string STATUS_STR{"status"};
+  static inline const std::string DOWN_STR{"down"};
+  static inline const std::string UP_STR{"up"};
+  static inline const std::string REASON_STR{"reason"};
+
   void status_get();
   void status_down();
   void status_up();
@@ -148,18 +166,26 @@ private:
 class PluginCommand : public CtrlCommand
 {
 public:
-  PluginCommand(ts::Arguments args);
+  PluginCommand(ts::Arguments *args);
 
 private:
+  static inline const std::string MSG_STR{"msg"};
   void plugin_msg();
 };
 // 
-----------------------------------------------------------------------------------------------------------------------------------
 class DirectRPCCommand : public CtrlCommand
 {
 public:
-  DirectRPCCommand(ts::Arguments args);
+  DirectRPCCommand(ts::Arguments *args);
 
 private:
+  static inline const std::string GET_API_STR{"get-api"};
+  static inline const std::string FILE_STR{"file"};
+  static inline const std::string INPUT_STR{"input"};
+  static inline const std::string INVOKE_STR{"invoke"};
+  static inline const std::string RAW_STR{"raw"};
+  static inline const std::string PARAMS_STR{"params"};
+
   void from_file_request();
   void get_rpc_api();
   void read_from_input();
@@ -171,17 +197,24 @@ private:
 class ServerCommand : public CtrlCommand
 {
 public:
-  ServerCommand(ts::Arguments args);
+  ServerCommand(ts::Arguments *args);
 
 private:
+  static inline const std::string DRAIN_STR{"drain"};
+  static inline const std::string UNDO_STR{"undo"};
+  static inline const std::string NO_NEW_CONN_STR{"no-new-connection"};
+
   void server_drain();
 };
 //
 // 
-----------------------------------------------------------------------------------------------------------------------------------
 struct StorageCommand : public CtrlCommand {
-  StorageCommand(ts::Arguments args);
+  StorageCommand(ts::Arguments *args);
 
 private:
+  static inline const std::string STATUS_STR{"status"};
+  static inline const std::string OFFLINE_STR{"offline"};
+
   void get_storage_status();
   void set_storage_offline();
 };
diff --git a/src/traffic_ctl/traffic_ctl.cc b/src/traffic_ctl/traffic_ctl.cc
index c1b28e92d..41d801709 100644
--- a/src/traffic_ctl/traffic_ctl.cc
+++ b/src/traffic_ctl/traffic_ctl.cc
@@ -172,25 +172,25 @@ main(int argc, const char **argv)
     Layout::create();
 
     if (args.get("config")) {
-      command = std::make_shared<ConfigCommand>(args);
+      command = std::make_shared<ConfigCommand>(&args);
     } else if (args.get("metric")) {
-      command = std::make_shared<MetricCommand>(args);
+      command = std::make_shared<MetricCommand>(&args);
     } else if (args.get("server")) {
-      command = std::make_shared<ServerCommand>(args);
+      command = std::make_shared<ServerCommand>(&args);
     } else if (args.get("storage")) {
-      command = std::make_shared<StorageCommand>(args);
+      command = std::make_shared<StorageCommand>(&args);
     } else if (args.get("plugin")) {
-      command = std::make_shared<PluginCommand>(args);
+      command = std::make_shared<PluginCommand>(&args);
     } else if (args.get("host")) {
-      command = std::make_shared<HostCommand>(args);
+      command = std::make_shared<HostCommand>(&args);
     } else if (args.get("rpc")) {
-      command = std::make_shared<DirectRPCCommand>(args);
+      command = std::make_shared<DirectRPCCommand>(&args);
     }
     // Execute
     args.invoke();
   } catch (std::exception const &ex) {
     status_code = CTRL_EX_ERROR;
-    std::cerr << "Error found.\n" << ex.what() << '\n';
+    std::cerr << "Error found:\n" << ex.what() << '\n';
   }
 
   return status_code;

Reply via email to