Skip to content

Commit 87b4480

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 ad61785 commit 87b4480

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

plugins/recklessrpc.c

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

@@ -35,6 +37,17 @@ struct lconfig {
3537
char *network;
3638
} lconfig;
3739

40+
static struct io_plan *reckless_in_init(struct io_conn *conn,
41+
struct reckless *reckless)
42+
{
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+
3851
static struct io_plan *read_more(struct io_conn *conn, struct reckless *rkls)
3952
{
4053
rkls->stdout_read += rkls->stdout_new;
@@ -114,6 +127,33 @@ static struct io_plan *conn_init(struct io_conn *conn, struct reckless *rkls)
114127
return read_more(conn, rkls);
115128
}
116129

130+
static void stderr_conn_finish(struct io_conn *conn, void *reckless UNUSED)
131+
{
132+
io_close(conn);
133+
}
134+
135+
static struct io_plan *stderr_read_more(struct io_conn *conn,
136+
struct reckless *rkls)
137+
{
138+
rkls->stderr_read += rkls->stderr_new;
139+
if (rkls->stderr_read == tal_count(rkls->stderrbuf))
140+
tal_resize(&rkls->stderrbuf, rkls->stderr_read * 2);
141+
if (strends(rkls->stderrbuf, "[Y] to create one now.\n")) {
142+
plugin_log(plugin, LOG_DBG, "confirming config creation");
143+
reckless_send_yes(rkls);
144+
}
145+
return io_read_partial(conn, rkls->stderrbuf + rkls->stderr_read,
146+
tal_count(rkls->stderrbuf) - rkls->stderr_read,
147+
&rkls->stderr_new, stderr_read_more, rkls);
148+
}
149+
150+
static struct io_plan *stderr_conn_init(struct io_conn *conn,
151+
struct reckless *reckless)
152+
{
153+
io_set_finish(conn, stderr_conn_finish, NULL);
154+
return stderr_read_more(conn, reckless);
155+
}
156+
117157
static struct command_result *reckless_call(struct command *cmd,
118158
const char *call)
119159
{
@@ -142,6 +182,8 @@ static struct command_result *reckless_call(struct command *cmd,
142182
reckless->stderrbuf = tal_arrz(reckless, char, 1024);
143183
reckless->stdout_read = 0;
144184
reckless->stdout_new = 0;
185+
reckless->stderr_read = 0;
186+
reckless->stderr_new = 0;
145187
char * full_cmd;
146188
full_cmd = tal_fmt(tmpctx, "calling:");
147189
for (int i=0; i<tal_count(my_call); i++)
@@ -154,6 +196,7 @@ static struct command_result *reckless_call(struct command *cmd,
154196

155197
/* FIXME: fail if invalid pid*/
156198
io_new_conn(reckless, reckless->stdoutfd, conn_init, reckless);
199+
io_new_conn(reckless, reckless->stderrfd, stderr_conn_init, reckless);
157200
tal_free(my_call);
158201
return command_still_pending(cmd);
159202
}

0 commit comments

Comments
 (0)