Skip to content

Commit 183fbb4

Browse files
ShahanaFarooquirustyrussell
authored andcommitted
commando: listrunes command
Changelog-Added: Plugins: `commando-listrunes` command to show issued runes.
1 parent 080a4dd commit 183fbb4

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

plugins/commando.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,73 @@ static struct command_result *json_commando_rune(struct command *cmd,
986986
return send_outreq(plugin, req);
987987
}
988988

989+
static struct command_result *json_add_runestr(struct json_stream *js,
990+
const char *rune_str,
991+
size_t rune_strlen,
992+
bool stored)
993+
{
994+
json_object_start(js, NULL);
995+
json_add_stringn(js, "rune", rune_str, rune_strlen);
996+
if (!stored) {
997+
json_add_bool(js, "stored", false);
998+
}
999+
json_object_end(js);
1000+
return NULL;
1001+
}
1002+
1003+
static struct command_result *listdatastore_done(struct command *cmd,
1004+
const char *buf,
1005+
const jsmntok_t *result,
1006+
struct rune *rune)
1007+
{
1008+
struct json_stream *js;
1009+
const jsmntok_t *t, *d = json_get_member(buf, result, "datastore");
1010+
size_t i;
1011+
const char *runestr;
1012+
bool printed = false;
1013+
1014+
if (rune != NULL) {
1015+
runestr = rune_to_string(tmpctx, rune);
1016+
} else {
1017+
runestr = NULL;
1018+
}
1019+
1020+
js = jsonrpc_stream_success(cmd);
1021+
1022+
json_array_start(js, "runes");
1023+
json_for_each_arr(i, t, d) {
1024+
const jsmntok_t *s = json_get_member(buf, t, "string");
1025+
if (runestr != NULL && !json_tok_streq(buf, s, runestr))
1026+
continue;
1027+
json_add_runestr(js, buf + s->start, s->end - s->start, true);
1028+
printed = true;
1029+
}
1030+
if (rune && !printed) {
1031+
json_add_runestr(js, runestr, strlen(runestr), false);
1032+
}
1033+
json_array_end(js);
1034+
return command_finished(cmd, js);
1035+
}
1036+
1037+
static struct command_result *json_commando_listrunes(struct command *cmd,
1038+
const char *buffer,
1039+
const jsmntok_t *params)
1040+
{
1041+
struct rune *rune;
1042+
struct out_req *req;
1043+
1044+
if (!param(cmd, buffer, params,
1045+
p_opt("rune", param_rune, &rune), NULL))
1046+
return command_param_failed();
1047+
1048+
req = jsonrpc_request_start(plugin, cmd, "listdatastore", listdatastore_done, forward_error, rune);
1049+
json_array_start(req->js, "key");
1050+
json_add_string(req->js, NULL, "commando");
1051+
json_add_string(req->js, NULL, "runes");
1052+
json_array_end(req->js);
1053+
return send_outreq(plugin, req);
1054+
}
1055+
9891056
#if DEVELOPER
9901057
static void memleak_mark_globals(struct plugin *p, struct htable *memtable)
9911058
{
@@ -1062,6 +1129,13 @@ static const struct plugin_command commands[] = { {
10621129
"Takes an optional {rune} with optional {restrictions} and returns {rune}",
10631130
json_commando_rune,
10641131
},
1132+
{
1133+
"commando-listrunes",
1134+
"utility",
1135+
"List runes we have created earlier",
1136+
"Takes an optional {rune} and returns list of {rune}",
1137+
json_commando_listrunes,
1138+
}
10651139
};
10661140

10671141
int main(int argc, char *argv[])

0 commit comments

Comments
 (0)