Skip to content

Commit da4a16f

Browse files
recklessrpc: allow single term commands
Allows listconfig, listinstalled, and listavailable to be called via rpc. Also allow processing non-array result in listconfig output.
1 parent dfd5d6c commit da4a16f

File tree

4 files changed

+63
-25
lines changed

4 files changed

+63
-25
lines changed

contrib/msggen/msggen/schema.json

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30329,12 +30329,19 @@
3032930329
"additionalProperties": false,
3033030330
"properties": {
3033130331
"result": {
30332-
"type": "array",
30333-
"items": {
30334-
"type": "string"
30335-
},
30336-
"description": [
30337-
"Output of the requested reckless command."
30332+
"oneOf": [
30333+
{
30334+
"type": "array",
30335+
"description": [
30336+
"Output of the requested reckless command."
30337+
]
30338+
},
30339+
{
30340+
"type": "object",
30341+
"description": [
30342+
"Output of the requested reckless command."
30343+
]
30344+
}
3033830345
]
3033930346
},
3034030347
"log": {

doc/reckless.7.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ RETURN VALUE
2828

2929
On success, an object is returned, containing:
3030

31-
- **result** (array of strings): Output of the requested reckless command.:
32-
- (string, optional)
31+
- **result** (one of):
32+
- (array): Output of the requested reckless command.
33+
- (object): Output of the requested reckless command.:
3334
- **log** (array of strings): Verbose log entries of the requested reckless command.:
3435
- (string, optional)
3536

doc/schemas/reckless.json

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,19 @@
6767
"additionalProperties": false,
6868
"properties": {
6969
"result": {
70-
"type": "array",
71-
"items": {
72-
"type": "string"
73-
},
74-
"description": [
75-
"Output of the requested reckless command."
70+
"oneOf": [
71+
{
72+
"type": "array",
73+
"description": [
74+
"Output of the requested reckless command."
75+
]
76+
},
77+
{
78+
"type": "object",
79+
"description": [
80+
"Output of the requested reckless command."
81+
]
82+
}
7683
]
7784
},
7885
"log": {

plugins/recklessrpc.c

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static struct command_result *reckless_result(struct io_conn *conn,
6868
reckless->process_failed);
6969
return command_finished(reckless->cmd, response);
7070
}
71-
const jsmntok_t *results, *result, *logs, *log;
71+
const jsmntok_t *results, *result, *logs, *log, *conf;
7272
size_t i;
7373
jsmn_parser parser;
7474
jsmntok_t *toks;
@@ -97,15 +97,26 @@ static struct command_result *reckless_result(struct io_conn *conn,
9797
}
9898

9999
response = jsonrpc_stream_success(reckless->cmd);
100-
json_array_start(response, "result");
101100
results = json_get_member(reckless->stdoutbuf, toks, "result");
102-
json_for_each_arr(i, result, results) {
103-
json_add_string(response,
104-
NULL,
105-
json_strdup(reckless, reckless->stdoutbuf,
106-
result));
101+
conf = json_get_member(reckless->stdoutbuf, results, "requested_lightning_conf");
102+
if (conf) {
103+
plugin_log(plugin, LOG_DBG, "dealing with listconfigs output");
104+
json_object_start(response, "result");
105+
json_for_each_obj(i, result, results) {
106+
json_add_tok(response, json_strdup(tmpctx, reckless->stdoutbuf, result), result+1, reckless->stdoutbuf);
107+
}
108+
json_object_end(response);
109+
110+
} else {
111+
json_array_start(response, "result");
112+
json_for_each_arr(i, result, results) {
113+
json_add_string(response,
114+
NULL,
115+
json_strdup(reckless, reckless->stdoutbuf,
116+
result));
117+
}
118+
json_array_end(response);
107119
}
108-
json_array_end(response);
109120
json_array_start(response, "log");
110121
logs = json_get_member(reckless->stdoutbuf, toks, "log");
111122
json_for_each_arr(i, log, logs) {
@@ -211,13 +222,25 @@ static struct io_plan *stderr_conn_init(struct io_conn *conn,
211222
return stderr_read_more(conn, reckless);
212223
}
213224

225+
static bool is_single_arg_cmd(const char *command) {
226+
if (strcmp(command, "listconfig"))
227+
return true;
228+
if (strcmp(command, "listavailable"))
229+
return true;
230+
if (strcmp(command, "listinstalled"))
231+
return true;
232+
return false;
233+
}
234+
214235
static struct command_result *reckless_call(struct command *cmd,
215236
const char *subcommand,
216237
const char *target,
217238
const char *target2)
218239
{
219-
if (!subcommand || !target)
220-
return command_fail(cmd, PLUGIN_ERROR, "invalid reckless call");
240+
if (!is_single_arg_cmd(subcommand)) {
241+
if (!subcommand || !target)
242+
return command_fail(cmd, PLUGIN_ERROR, "invalid reckless call");
243+
}
221244
char **my_call;
222245
my_call = tal_arrz(tmpctx, char *, 0);
223246
tal_arr_expand(&my_call, "reckless");
@@ -273,7 +296,7 @@ static struct command_result *json_reckless(struct command *cmd,
273296
/* Allow check command to evaluate. */
274297
if (!param(cmd, buf, params,
275298
p_req("command", param_string, &command),
276-
p_req("target/subcommand", param_string, &target),
299+
p_opt("target/subcommand", param_string, &target),
277300
p_opt("target", param_string, &target2),
278301
NULL))
279302
return command_param_failed();

0 commit comments

Comments
 (0)