Skip to content

Commit

Permalink
feat(remote_command): change some remote_command shell output to JSON…
Browse files Browse the repository at this point in the history
… 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).
  • Loading branch information
Samunroyu authored Jul 4, 2024
1 parent 7f50394 commit 57dd0e1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/nfs/nfs_client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@

#include "nfs_client_impl.h"

#include <cstdint>
// IWYU pragma: no_include <ext/alloc_traits.h>
#include <mutex>

#include "absl/strings/string_view.h"
#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"
Expand Down
1 change: 1 addition & 0 deletions src/nfs/nfs_server_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 9 additions & 4 deletions src/utils/command_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 13 additions & 5 deletions src/utils/command_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <functional>
#include <map>
#include <memory>
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <string>
#include <vector>

Expand Down Expand Up @@ -134,34 +136,40 @@ class command_manager : public ::dsn::utils::singleton<command_manager>
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;
Expand Down

0 comments on commit 57dd0e1

Please sign in to comment.