Skip to content

Commit b51424d

Browse files
reckless-rpc: auto-accept reckless config creation dialog
This would interupt most commands during the config reading step until the user accepts the prompt to create a new empty config.
1 parent ea769b9 commit b51424d

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

plugins/recklessrpc.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ struct reckless {
2525
char *stderrbuf;
2626
size_t stdout_read; /* running total */
2727
size_t stdout_new; /* new since last read */
28+
size_t stderr_read;
29+
size_t stderr_new;
2830
pid_t pid;
2931
};
3032

@@ -34,6 +36,18 @@ struct lconfig {
3436
char *network;
3537
} lconfig;
3638

39+
static struct io_plan *reckless_in_init(struct io_conn *conn,
40+
struct reckless *reckless)
41+
{
42+
plugin_log(plugin, LOG_DBG, "Reckless stdin initialized. Writing Y.");
43+
return io_write(conn, "Y", 1, io_close_cb, NULL);
44+
}
45+
46+
static void reckless_send_yes(struct reckless *reckless)
47+
{
48+
io_new_conn(reckless, reckless->stdinfd, reckless_in_init, reckless);
49+
}
50+
3751
static struct io_plan *read_more(struct io_conn *conn, struct reckless *rkls)
3852
{
3953
plugin_log(plugin, LOG_DBG, "reckless-stdout:%s\n", rkls->stdoutbuf);
@@ -117,6 +131,37 @@ static struct io_plan *conn_init(struct io_conn *conn, struct reckless *rkls)
117131
return read_more(conn, rkls);
118132
}
119133

134+
static void stderr_conn_finish(struct io_conn *conn, void *reckless UNUSED)
135+
{
136+
io_close(conn);
137+
}
138+
139+
static struct io_plan *stderr_read_more(struct io_conn *conn,
140+
struct reckless *rkls)
141+
{
142+
plugin_log(plugin, LOG_DBG, "reckless-stderr:%s\n",
143+
rkls->stderrbuf);
144+
rkls->stderr_read += rkls->stderr_new;
145+
if (rkls->stderr_read == tal_count(rkls->stderrbuf))
146+
tal_resize(&rkls->stderrbuf, rkls->stderr_read * 2);
147+
if (strends(rkls->stderrbuf, "[Y] to create one now.\n")) {
148+
plugin_log(plugin, LOG_DBG, "inputting Y");
149+
reckless_send_yes(rkls);
150+
plugin_log(plugin, LOG_DBG, "wrote Y to stdin");
151+
}
152+
return io_read_partial(conn, rkls->stderrbuf + rkls->stderr_read,
153+
tal_count(rkls->stderrbuf) - rkls->stderr_read,
154+
&rkls->stderr_new, stderr_read_more, rkls);
155+
}
156+
157+
static struct io_plan *stderr_conn_init(struct io_conn *conn,
158+
struct reckless *reckless)
159+
{
160+
plugin_log(plugin, LOG_DBG, "new stderr connection to reckless");
161+
io_set_finish(conn, stderr_conn_finish, NULL);
162+
return stderr_read_more(conn, reckless);
163+
}
164+
120165
static struct command_result *reckless_call(struct command *cmd,
121166
const char *call)
122167
{
@@ -145,6 +190,8 @@ static struct command_result *reckless_call(struct command *cmd,
145190
reckless->stderrbuf = tal_arrz(reckless, char, 1024);
146191
reckless->stdout_read = 0;
147192
reckless->stdout_new = 0;
193+
reckless->stderr_read = 0;
194+
reckless->stderr_new = 0;
148195
plugin_log(plugin, LOG_DBG, "calling reckless");
149196
char * full_cmd;
150197
full_cmd = tal_fmt(tmpctx, "calling: [");
@@ -160,6 +207,7 @@ static struct command_result *reckless_call(struct command *cmd,
160207

161208
/* FIXME: fail if invalid pid*/
162209
io_new_conn(reckless, reckless->stdoutfd, conn_init, reckless);
210+
io_new_conn(reckless, reckless->stderrfd, stderr_conn_init, reckless);
163211
tal_free(my_call);
164212
return command_still_pending(cmd);
165213
}

0 commit comments

Comments
 (0)