This is an automated email from the ASF dual-hosted git repository.
wangdan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pegasus.git
The following commit(s) were added to refs/heads/master by this push:
new 57dd0e1e5 feat(remote_command): change some remote_command shell
output to JSON format (#2058)
57dd0e1e5 is described below
commit 57dd0e1e53ffe11b7a8ff57ffe8e35ed79f44333
Author: Samunroyu <[email protected]>
AuthorDate: Thu Jul 4 17:42:22 2024 +0800
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 e736b2749..04f96a16d 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 <cstdint>
// IWYU pragma: no_include <ext/alloc_traits.h>
#include <mutex>
@@ -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 df1418822..21a7f3a8a 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 bcf37bb87..35b678b81 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<std::string> &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 a73966845..903ccd290 100644
--- a/src/utils/command_manager.h
+++ b/src/utils/command_manager.h
@@ -32,6 +32,8 @@
#include <functional>
#include <map>
#include <memory>
+#include <nlohmann/json.hpp>
+#include <nlohmann/json_fwd.hpp>
#include <string>
#include <vector>
@@ -134,34 +136,40 @@ private:
const std::vector<std::string> &args,
const std::function<bool(int64_t value)>
&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<int64_t>(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> command_instance_ptr;
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]