mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-21 08:04:26 +01:00
lightning-cli: change default printing in response to "format-hint": "simple".
And set it for 'help <command>'. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
- Lightningd: add support for `signet` networks using the `--network=signet` or `--signet` startup option
|
- Lightningd: add support for `signet` networks using the `--network=signet` or `--signet` startup option
|
||||||
- JSON API: `listfunds` now returns also `funding_output` for `channels`
|
- JSON API: `listfunds` now returns also `funding_output` for `channels`
|
||||||
|
- plugins: plugins can now suggest `lightning-cli` default to -H for responses.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
|||||||
@@ -201,6 +201,7 @@ static void human_help(char *buffer, const jsmntok_t *result)
|
|||||||
enum format {
|
enum format {
|
||||||
JSON,
|
JSON,
|
||||||
HUMAN,
|
HUMAN,
|
||||||
|
HELPLIST,
|
||||||
DEFAULT_FORMAT,
|
DEFAULT_FORMAT,
|
||||||
RAW
|
RAW
|
||||||
};
|
};
|
||||||
@@ -392,6 +393,25 @@ static void tal_error(const char *msg)
|
|||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static enum format delete_format_hint(const char *resp,
|
||||||
|
jsmntok_t **toks,
|
||||||
|
jsmntok_t *result)
|
||||||
|
{
|
||||||
|
const jsmntok_t *hint;
|
||||||
|
enum format format = JSON;
|
||||||
|
|
||||||
|
hint = json_get_member(resp, result, "format-hint");
|
||||||
|
if (!hint)
|
||||||
|
return format;
|
||||||
|
|
||||||
|
if (json_tok_streq(resp, hint, "simple"))
|
||||||
|
format = HUMAN;
|
||||||
|
|
||||||
|
/* Don't let hint appear in the output! */
|
||||||
|
json_tok_remove(toks, result, hint, 1);
|
||||||
|
return format;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
setup_locale();
|
setup_locale();
|
||||||
@@ -453,16 +473,9 @@ int main(int argc, char *argv[])
|
|||||||
method = "help";
|
method = "help";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (format == DEFAULT_FORMAT) {
|
|
||||||
if (streq(method, "help"))
|
|
||||||
format = HUMAN;
|
|
||||||
else
|
|
||||||
format = JSON;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Launch a manpage if we have a help command with an argument. We do
|
/* Launch a manpage if we have a help command with an argument. We do
|
||||||
* not need to have lightningd running in this case. */
|
* not need to have lightningd running in this case. */
|
||||||
if (streq(method, "help") && format == HUMAN && argc >= 3) {
|
if (streq(method, "help") && format == DEFAULT_FORMAT && argc >= 3) {
|
||||||
command = argv[2];
|
command = argv[2];
|
||||||
char *page = tal_fmt(ctx, "lightning-%s", command);
|
char *page = tal_fmt(ctx, "lightning-%s", command);
|
||||||
|
|
||||||
@@ -591,19 +604,35 @@ int main(int argc, char *argv[])
|
|||||||
json_tok_full_len(id), json_tok_full(resp, id));
|
json_tok_full_len(id), json_tok_full(resp, id));
|
||||||
|
|
||||||
if (!error || json_tok_is_null(resp, error)) {
|
if (!error || json_tok_is_null(resp, error)) {
|
||||||
// if we have specific help command
|
if (format == DEFAULT_FORMAT) {
|
||||||
if (format == HUMAN)
|
/* This works best when we order it. */
|
||||||
if (streq(method, "help") && command == NULL)
|
if (streq(method, "help") && command == NULL)
|
||||||
human_help(resp, result);
|
format = HELPLIST;
|
||||||
else
|
else
|
||||||
human_readable(resp, result, '\n');
|
/* Use offset of result to get non-const ptr */
|
||||||
else if (format == RAW)
|
format = delete_format_hint(resp, &toks,
|
||||||
|
/* const-washing */
|
||||||
|
toks + (result - toks));
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (format) {
|
||||||
|
case HELPLIST:
|
||||||
|
human_help(resp, result);
|
||||||
|
break;
|
||||||
|
case HUMAN:
|
||||||
|
human_readable(resp, result, '\n');
|
||||||
|
break;
|
||||||
|
case JSON:
|
||||||
|
print_json(resp, result, "");
|
||||||
|
printf("\n");
|
||||||
|
break;
|
||||||
|
case RAW:
|
||||||
printf("%.*s\n",
|
printf("%.*s\n",
|
||||||
json_tok_full_len(result),
|
json_tok_full_len(result),
|
||||||
json_tok_full(resp, result));
|
json_tok_full(resp, result));
|
||||||
else {
|
break;
|
||||||
print_json(resp, result, "");
|
default:
|
||||||
printf("\n");
|
abort();
|
||||||
}
|
}
|
||||||
tal_free(lightning_dir);
|
tal_free(lightning_dir);
|
||||||
tal_free(rpc_filename);
|
tal_free(rpc_filename);
|
||||||
|
|||||||
@@ -172,6 +172,10 @@ the JSON-RPC call `id`, which is internally remapped to a unique
|
|||||||
integer instead, in order to avoid collisions. When passing the result
|
integer instead, in order to avoid collisions. When passing the result
|
||||||
back the `id` field is restored to its original value.
|
back the `id` field is restored to its original value.
|
||||||
|
|
||||||
|
Note that if your `result` for an RPC call includes `"format-hint":
|
||||||
|
"simple"`, then `lightning-cli` will default to printing your output
|
||||||
|
in "human-readable" flat form.
|
||||||
|
|
||||||
## Event notifications
|
## Event notifications
|
||||||
|
|
||||||
Event notifications allow a plugin to subscribe to events in
|
Event notifications allow a plugin to subscribe to events in
|
||||||
|
|||||||
@@ -425,6 +425,9 @@ static struct command_result *json_help(struct command *cmd,
|
|||||||
}
|
}
|
||||||
json_array_end(response);
|
json_array_end(response);
|
||||||
|
|
||||||
|
/* Tell cli this is simple enough to be formatted flat for humans */
|
||||||
|
json_add_string(response, "format-hint", "simple");
|
||||||
|
|
||||||
return command_success(cmd, response);
|
return command_success(cmd, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user