Support reading OpenRPC documents from a file

Bump liberty, it generated incorrect help messages.
This commit is contained in:
Přemysl Eric Janouch 2020-10-13 21:15:56 +02:00
parent 63c8a79479
commit 5854ed1b32
Signed by: p
GPG Key ID: A0420B94F92B9493
3 changed files with 32 additions and 21 deletions

View File

@ -76,9 +76,9 @@ Protocol
*-o* _ORIGIN_, *--origin*=_ORIGIN_:: *-o* _ORIGIN_, *--origin*=_ORIGIN_::
Set the HTTP Origin header to _ORIGIN_. Some servers may need this. Set the HTTP Origin header to _ORIGIN_. Some servers may need this.
*-O*, *--openrpc*:: *-O*[__PATH__], *--openrpc*[**=**__PATH__]::
Call "rpc.discover" upon start-up in order to pull in OpenRPC data for Call "rpc.discover" upon start-up in order to pull in OpenRPC data for
tab completion of method names. tab completion of method names. If a path is given, it is read from a file.
Program information Program information
~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~

View File

@ -1096,7 +1096,6 @@ static struct app_context
bool compact; ///< Whether to not pretty print bool compact; ///< Whether to not pretty print
bool verbose; ///< Print requests bool verbose; ///< Print requests
bool trust_all; ///< Don't verify peer certificates bool trust_all; ///< Don't verify peer certificates
bool openrpc; ///< OpenRPC method name completion
bool null_as_id; ///< JSON null is used as an ID bool null_as_id; ///< JSON null is used as an ID
int64_t next_id; ///< Next autogenerated ID int64_t next_id; ///< Next autogenerated ID
@ -3243,16 +3242,17 @@ parse_rpc_discover (struct app_context *ctx, struct str *buf, struct error **e)
{ {
// Just optimistically punch through, I don't have time for this shit // Just optimistically punch through, I don't have time for this shit
json_error_t error; json_error_t error;
json_t *response = NULL, *result = NULL, *value = NULL; json_t *response = NULL, *methods = NULL, *value = NULL;
if (!(response = json_loadb (buf->str, buf->len, 0, &error))) if (!(response = json_loadb (buf->str, buf->len, 0, &error)))
error_set (e, "parse failure: %s", error.text); error_set (e, "parse failure: %s", error.text);
else if (!(result = json_object_get (response, "result")) else if ((!(methods = json_object_get (response, "result"))
|| !(result = json_object_get (result, "methods"))) || !(methods = json_object_get (methods, "methods")))
&& !(methods = json_object_get (response, "methods")))
error_set (e, "unsupported"); error_set (e, "unsupported");
else else
{ {
const char *name = NULL; const char *name = NULL;
for (size_t i = 0; (value = json_array_get (result, i)); i++) for (size_t i = 0; (value = json_array_get (methods, i)); i++)
if ((value = json_object_get (value, "name")) if ((value = json_object_get (value, "name"))
&& (name = json_string_value (value))) && (name = json_string_value (value)))
str_map_set (&ctx->methods, name, (void *) 1); str_map_set (&ctx->methods, name, (void *) 1);
@ -3261,17 +3261,26 @@ parse_rpc_discover (struct app_context *ctx, struct str *buf, struct error **e)
} }
static void static void
init_openrpc (struct app_context *ctx) init_openrpc (struct app_context *ctx, const char *openrpc)
{ {
if (!ctx->openrpc) // Three possibilities: NULL for not requested, "" for rpc.discover,
// and anything else has to be a path to a JSON file
if (!openrpc)
return; return;
json_t *id = json_integer (ctx->next_id++);
struct str buf = str_make (); struct str buf = str_make ();
struct error *error; struct error *error;
if (!(error = json_rpc_call_raw (ctx, "rpc.discover", id, NULL, &buf))) if (*openrpc)
parse_rpc_discover (ctx, &buf, &error); // This may or may not have a protocol envelope
read_file (openrpc, &buf, &error);
else
{
json_t *id = json_integer (ctx->next_id++);
error = json_rpc_call_raw (ctx, "rpc.discover", id, NULL, &buf);
json_decref (id); json_decref (id);
}
if (!error)
parse_rpc_discover (ctx, &buf, &error);
if (error) if (error)
{ {
@ -3536,7 +3545,7 @@ init_watchers (struct app_context *ctx)
static void static void
parse_program_arguments (struct app_context *ctx, int argc, char **argv, parse_program_arguments (struct app_context *ctx, int argc, char **argv,
const char **origin, const char **endpoint) const char **origin, const char **endpoint, const char **openrpc)
{ {
static const struct opt opts[] = static const struct opt opts[] =
{ {
@ -3546,10 +3555,11 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv,
{ 'n', "null-as-id", NULL, 0, "JSON null is used as an `id'" }, { 'n', "null-as-id", NULL, 0, "JSON null is used as an `id'" },
{ 'o', "origin", "O", 0, "set the HTTP Origin header" }, { 'o', "origin", "O", 0, "set the HTTP Origin header" },
// So far you have to explicitly enable this rather than disable // So far you have to explicitly enable this rather than disable
{ 'O', "openrpc", NULL, 0, "method name completion using OpenRPC" }, { 'O', "openrpc", "PATH", OPT_OPTIONAL_ARG,
"method name completion using OpenRPC" },
{ 't', "trust-all", NULL, 0, "don't care about SSL/TLS certificates" }, { 't', "trust-all", NULL, 0, "don't care about SSL/TLS certificates" },
{ 'v', "verbose", NULL, 0, "print raw requests and responses" }, { 'v', "verbose", NULL, 0, "print raw requests and responses" },
{ 'w', "write-default-cfg", "FILENAME", { 'w', "write-default-cfg", "PATH",
OPT_OPTIONAL_ARG | OPT_LONG_ONLY, OPT_OPTIONAL_ARG | OPT_LONG_ONLY,
"write a default configuration file and exit" }, "write a default configuration file and exit" },
{ 'd', "debug", NULL, 0, "run in debug mode" }, { 'd', "debug", NULL, 0, "run in debug mode" },
@ -3576,7 +3586,8 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv,
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
case 'o': *origin = optarg; break; case 'o': *origin = optarg; break;
case 'O': ctx->openrpc = true; break; case 'O': *openrpc = optarg ? optarg : ""; break;
case 'n': ctx->null_as_id = true; break; case 'n': ctx->null_as_id = true; break;
case 'c': ctx->compact = true; break; case 'c': ctx->compact = true; break;
case 't': ctx->trust_all = true; break; case 't': ctx->trust_all = true; break;
@ -3657,8 +3668,8 @@ main (int argc, char *argv[])
register_config_modules (&g_ctx); register_config_modules (&g_ctx);
config_load (&g_ctx.config, config_item_object ()); config_load (&g_ctx.config, config_item_object ());
const char *origin = NULL, *endpoint = NULL; const char *origin = NULL, *endpoint = NULL, *openrpc = NULL;
parse_program_arguments (&g_ctx, argc, argv, &origin, &endpoint); parse_program_arguments (&g_ctx, argc, argv, &origin, &endpoint, &openrpc);
g_ctx.input = input_new (); g_ctx.input = input_new ();
g_ctx.input->user_data = &g_ctx; g_ctx.input->user_data = &g_ctx;
@ -3719,7 +3730,7 @@ main (int argc, char *argv[])
g_ctx.input->vtable->start (g_ctx.input, PROGRAM_NAME); g_ctx.input->vtable->start (g_ctx.input, PROGRAM_NAME);
ev_set_userdata (EV_DEFAULT_ &g_ctx); ev_set_userdata (EV_DEFAULT_ &g_ctx);
init_openrpc (&g_ctx); init_openrpc (&g_ctx, openrpc);
ev_run (EV_DEFAULT_ 0); ev_run (EV_DEFAULT_ 0);
// User has terminated the program, let's save the history and clean up // User has terminated the program, let's save the history and clean up

@ -1 +1 @@
Subproject commit e029aae1d3d1884ca868c3694bdec0456b3e8267 Subproject commit 69101eb1554ad2fca6de30cdbaccac076210d7e3