Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils/remote: add option to dump RPC response as JSON #3819

Merged
merged 1 commit into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion utils/remote.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ struct Config
std::string unix_socket_path;

bool debug = false;
bool json = false;
bool use_ssl = false;
};

Expand Down Expand Up @@ -234,7 +235,7 @@ enum
****
***/

static auto constexpr Options = std::array<tr_option, 97>{
static auto constexpr Options = std::array<tr_option, 98>{
{ { 'a', "add", "Add torrent files by filename or URL", "a", false, nullptr },
{ 970, "alt-speed", "Use the alternate Limits", "as", false, nullptr },
{ 971, "no-alt-speed", "Don't use the alternate Limits", "AS", false, nullptr },
Expand Down Expand Up @@ -274,6 +275,7 @@ static auto constexpr Options = std::array<tr_option, 97>{
{ 941, "info-peers", "List the current torrent(s)' peers", "ip", false, nullptr },
{ 942, "info-pieces", "List the current torrent(s)' pieces", "ic", false, nullptr },
{ 943, "info-trackers", "List the current torrent(s)' trackers", "it", false, nullptr },
{ 'j', "json", "Return RPC response as a JSON string", "j", false, nullptr },
{ 920, "session-info", "Show the session's details", "si", false, nullptr },
{ 921, "session-stats", "Show the session's statistics", "st", false, nullptr },
{ 'l', "list", "List all torrents", "l", false, nullptr },
Expand Down Expand Up @@ -415,6 +417,7 @@ static int getOptMode(int val)
case 'b': /* debug */
case 'n': /* auth */
case 968: /* Unix domain socket */
case 'j': /* JSON */
case 810: /* authenv */
case 'N': /* netrc */
case 820: /* UseSSL */
Expand Down Expand Up @@ -2141,6 +2144,12 @@ static int processResponse(char const* rpcurl, std::string_view response, Config
TR_PRIsv_ARG(response));
}

if (config.json)
{
printf("%" TR_PRIsv, TR_PRIsv_ARG(response));
return status;
}

if (!tr_variantFromBuf(&top, TR_VARIANT_PARSE_JSON | TR_VARIANT_PARSE_INPLACE, response))
{
tr_logAddWarn(fmt::format("Unable to parse response '{}'", response));
Expand Down Expand Up @@ -2457,6 +2466,10 @@ static int processArgs(char const* rpcurl, int argc, char const* const* argv, Co
config.debug = true;
break;

case 'j': /* return output as JSON */
config.json = true;
break;

case 968: /* Unix domain socket */
config.unix_socket_path = optarg;
break;
Expand Down
3 changes: 3 additions & 0 deletions utils/transmission-remote.1
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ and
.Op Fl if
.Op Fl ip
.Op Fl it
.Op Fl j
.Op Fl l
.Op Fl m | M
.Op Fl n Ar user:pass
Expand Down Expand Up @@ -187,6 +188,8 @@ List the specified torrent's peers
List the specified torrent's pieces
.It Fl it Fl -info-trackers
List the specified torrent's trackers
.It Fl j Fl -json
Return the RPC response as JSON
.It Fl si Fl -session-info
List session information from the server
.It Fl st Fl -session-stats
Expand Down