From 57dd0e1e53ffe11b7a8ff57ffe8e35ed79f44333 Mon Sep 17 00:00:00 2001 From: Samunroyu <36890229+Samunroyu@users.noreply.github.com> Date: Thu, 4 Jul 2024 17:42:22 +0800 Subject: [PATCH] feat(remote_command): change some remote_command shell output to JSON format (#2058) Some remote commands shell output are format by json. And some remote command are not. Change the output of register_int_command, register_bool_command to JSON format to improve readability by programs (e.g., Python scripts). --- src/nfs/nfs_client_impl.cpp | 2 ++ src/nfs/nfs_server_impl.cpp | 1 + src/utils/command_manager.cpp | 13 +++++++++---- src/utils/command_manager.h | 18 +++++++++++++----- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/nfs/nfs_client_impl.cpp b/src/nfs/nfs_client_impl.cpp index e736b2749f..04f96a16d5 100644 --- a/src/nfs/nfs_client_impl.cpp +++ b/src/nfs/nfs_client_impl.cpp @@ -26,6 +26,7 @@ #include "nfs_client_impl.h" +#include // IWYU pragma: no_include #include @@ -33,6 +34,7 @@ #include "fmt/core.h" #include "nfs/nfs_code_definition.h" #include "nfs/nfs_node.h" +#include "nlohmann/json.hpp" #include "runtime/rpc/dns_resolver.h" // IWYU pragma: keep #include "runtime/rpc/rpc_host_port.h" #include "utils/blob.h" diff --git a/src/nfs/nfs_server_impl.cpp b/src/nfs/nfs_server_impl.cpp index df1418822d..21a7f3a8af 100644 --- a/src/nfs/nfs_server_impl.cpp +++ b/src/nfs/nfs_server_impl.cpp @@ -35,6 +35,7 @@ #include "absl/strings/string_view.h" #include "nfs/nfs_code_definition.h" +#include "nlohmann/json.hpp" #include "runtime/api_layer1.h" #include "runtime/task/async_calls.h" #include "utils/TokenBucket.h" diff --git a/src/utils/command_manager.cpp b/src/utils/command_manager.cpp index bcf37bb873..35b678b81b 100644 --- a/src/utils/command_manager.cpp +++ b/src/utils/command_manager.cpp @@ -129,27 +129,32 @@ std::string command_manager::set_bool(bool &value, const std::string &name, const std::vector &args) { + nlohmann::json msg; + msg["error"] = "ok"; // Query. if (args.empty()) { - return value ? "true" : "false"; + msg[name] = value ? "true" : "false"; + return msg.dump(2); } // Invalid arguments size. if (args.size() > 1) { - return fmt::format("ERR: invalid arguments, only one boolean argument is acceptable"); + msg["error"] = "ERR: invalid arguments, only one boolean argument is acceptable"; + return msg.dump(2); } // Invalid argument. bool new_value; if (!dsn::buf2bool(args[0], new_value, /* ignore_case */ true)) { - return fmt::format("ERR: invalid arguments, '{}' is not a boolean", args[0]); + msg["error"] = fmt::format("ERR: invalid arguments, '{}' is not a boolean", args[0]); + return msg.dump(2); } // Set to a new value. value = new_value; LOG_INFO("set {} to {} by remote command", name, new_value); - return "OK"; + return msg.dump(2); } command_manager::command_manager() diff --git a/src/utils/command_manager.h b/src/utils/command_manager.h index a73966845c..903ccd2900 100644 --- a/src/utils/command_manager.h +++ b/src/utils/command_manager.h @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include #include @@ -134,34 +136,40 @@ class command_manager : public ::dsn::utils::singleton const std::vector &args, const std::function &validator) { + nlohmann::json msg; + msg["error"] = "ok"; // Query. if (args.empty()) { - return std::to_string(value); + msg[name] = fmt::format("{}", std::to_string(value)); + return msg.dump(2); } // Invalid arguments size. if (args.size() > 1) { - return fmt::format("ERR: invalid arguments, only one integer argument is acceptable"); + msg["error"] = "ERR: invalid arguments, only one integer argument is acceptable"; + return msg.dump(2); } // Reset to the default value. if (dsn::utils::iequals(args[0], "DEFAULT")) { value = default_value; - return "OK"; + msg[name] = default_value; + return msg.dump(2); } // Invalid argument. T new_value = 0; if (!internal::buf2signed(args[0], new_value) || !validator(static_cast(new_value))) { - return {"ERR: invalid arguments"}; + msg["error"] = "ERR: invalid arguments"; + return msg.dump(2); } // Set to a new value. value = new_value; LOG_INFO("set {} to {} by remote command", name, new_value); - return "OK"; + return msg.dump(2); } typedef ref_ptr command_instance_ptr;