plugins: make use of dynamic_plugin_list() in 'plugin list'

And make a function for the 'rescan' subcommand so that we are
consistent and have only subcommand logic in the main function,
which return command_still_pending() in any case (but 'stop').

patched-by: @rustyrussell
This commit is contained in:
darosior 2019-09-15 18:51:41 +02:00 committed by Rusty Russell
parent b9e40d4cc7
commit 7f181edebb
1 changed files with 36 additions and 19 deletions

View File

@ -223,6 +223,35 @@ plugin_dynamic_stop(struct command *cmd, const char *plugin_name)
"Could not find plugin %s", plugin_name);
}
/**
* Look for additions in the default plugin directory.
*/
static struct command_result *
plugin_dynamic_rescan_plugins(struct command *cmd)
{
bool found;
struct plugin *p;
/* This will not fail on "already registered" error. */
plugins_add_default_dir(cmd->ld->plugins,
path_join(tmpctx, cmd->ld->config_dir, "plugins"));
found = false;
list_for_each(&cmd->ld->plugins->plugins, p, list) {
if (p->plugin_state == UNCONFIGURED) {
struct dynamic_plugin *dp = tal(cmd, struct dynamic_plugin);
dp->plugin = p;
dp->cmd = cmd;
plugin_start(dp);
found = true;
}
}
if (!found)
return plugin_dynamic_list_plugins(cmd);
return command_still_pending(cmd);
}
/**
* A plugin command which permits to control plugins without restarting
* lightningd. It takes a subcommand, and an optional subcommand parameter.
@ -234,13 +263,11 @@ static struct command_result *json_plugin_control(struct command *cmd,
{
const char *subcmd;
subcmd = param_subcommand(cmd, buffer, params,
"start", "stop", "startdir", "rescan", "list", NULL);
"start", "stop", "startdir",
"rescan", "list", NULL);
if (!subcmd)
return command_param_failed();
struct plugin *p;
struct json_stream *response;
if (streq(subcmd, "stop")) {
const char *plugin_name;
@ -286,28 +313,18 @@ static struct command_result *json_plugin_control(struct command *cmd,
NULL))
return command_param_failed();
plugins_add_default_dir(cmd->ld->plugins,
path_join(tmpctx, cmd->ld->config_dir, "plugins"));
return plugin_dynamic_rescan_plugins(cmd);
} else if (streq(subcmd, "list")) {
if (!param(cmd, buffer, params,
p_req("subcommand", param_ignore, cmd),
NULL))
return command_param_failed();
/* Don't do anything as we return the plugin list anyway */
return plugin_dynamic_list_plugins(cmd);
}
response = json_stream_success(cmd);
json_array_start(response, "plugins");
list_for_each(&cmd->ld->plugins->plugins, p, list) {
json_object_start(response, NULL);
json_add_string(response, "name", p->cmd);
json_add_bool(response, "active",
p->plugin_state == CONFIGURED);
json_object_end(response);
}
json_array_end(response);
return command_success(cmd, response);
/* subcmd must be one of the above: param_subcommand checked it! */
abort();
}
static const struct json_command plugin_control_command = {