mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-21 16:14:23 +01:00
cli: implement new 'flattened JSON' mode.
Much nicer for grepping, since `{ "foo": { "bar": [7] } }` is turned into
`foo.bar[0]=7`.
Changelog-Added: cli: New `--flat` mode for easy grepping.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
committed by
Christian Decker
parent
679d3494b4
commit
abb16b4226
@@ -78,6 +78,49 @@ static size_t human_readable(const char *buffer, const jsmntok_t *t, char term)
|
|||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Returns number of tokens digested */
|
||||||
|
static size_t flat_json(const char *prefix,
|
||||||
|
const char *buffer, const jsmntok_t *t)
|
||||||
|
{
|
||||||
|
size_t i, n;
|
||||||
|
char *p;
|
||||||
|
|
||||||
|
switch (t->type) {
|
||||||
|
case JSMN_PRIMITIVE:
|
||||||
|
case JSMN_STRING:
|
||||||
|
printf("%s=%.*s\n",
|
||||||
|
prefix, t->end - t->start, buffer + t->start);
|
||||||
|
return 1;
|
||||||
|
case JSMN_ARRAY:
|
||||||
|
n = 1;
|
||||||
|
for (i = 0; i < t->size; i++) {
|
||||||
|
p = tal_fmt(NULL, "%s[%zi]", prefix, i);
|
||||||
|
n += flat_json(p, buffer, t + n);
|
||||||
|
tal_free(p);
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
case JSMN_OBJECT:
|
||||||
|
n = 1;
|
||||||
|
for (i = 0; i < t->size; i++) {
|
||||||
|
if (streq(prefix, ""))
|
||||||
|
p = tal_fmt(NULL, "%.*s",
|
||||||
|
t[n].end - t[n].start,
|
||||||
|
buffer + t[n].start);
|
||||||
|
else
|
||||||
|
p = tal_fmt(NULL, "%s.%.*s", prefix,
|
||||||
|
t[n].end - t[n].start,
|
||||||
|
buffer + t[n].start);
|
||||||
|
n++;
|
||||||
|
n += flat_json(p, buffer, t + n);
|
||||||
|
tal_free(p);
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
case JSMN_UNDEFINED:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
static int compare_tok(const jsmntok_t *a, const jsmntok_t *b,
|
static int compare_tok(const jsmntok_t *a, const jsmntok_t *b,
|
||||||
const char *buffer)
|
const char *buffer)
|
||||||
{
|
{
|
||||||
@@ -184,6 +227,7 @@ enum format {
|
|||||||
JSON,
|
JSON,
|
||||||
HUMAN,
|
HUMAN,
|
||||||
HELPLIST,
|
HELPLIST,
|
||||||
|
FLAT,
|
||||||
DEFAULT_FORMAT,
|
DEFAULT_FORMAT,
|
||||||
RAW
|
RAW
|
||||||
};
|
};
|
||||||
@@ -194,6 +238,12 @@ static char *opt_set_human(enum format *format)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char *opt_set_flat(enum format *format)
|
||||||
|
{
|
||||||
|
*format = FLAT;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static char *opt_set_json(enum format *format)
|
static char *opt_set_json(enum format *format)
|
||||||
{
|
{
|
||||||
*format = JSON;
|
*format = JSON;
|
||||||
@@ -454,6 +504,8 @@ int main(int argc, char *argv[])
|
|||||||
"<command> [<params>...]", "Show this message. Use the command help (without hyphens -- \"lightning-cli help\") to get a list of all RPC commands");
|
"<command> [<params>...]", "Show this message. Use the command help (without hyphens -- \"lightning-cli help\") to get a list of all RPC commands");
|
||||||
opt_register_noarg("-H|--human-readable", opt_set_human, &format,
|
opt_register_noarg("-H|--human-readable", opt_set_human, &format,
|
||||||
"Human-readable output (default for 'help')");
|
"Human-readable output (default for 'help')");
|
||||||
|
opt_register_noarg("-F|--flat", opt_set_flat, &format,
|
||||||
|
"Flatten output ('x.y.x=' format)");
|
||||||
opt_register_noarg("-J|--json", opt_set_json, &format,
|
opt_register_noarg("-J|--json", opt_set_json, &format,
|
||||||
"JSON output (default unless 'help')");
|
"JSON output (default unless 'help')");
|
||||||
opt_register_noarg("-R|--raw", opt_set_raw, &format,
|
opt_register_noarg("-R|--raw", opt_set_raw, &format,
|
||||||
@@ -624,6 +676,9 @@ int main(int argc, char *argv[])
|
|||||||
case HUMAN:
|
case HUMAN:
|
||||||
human_readable(resp, result, '\n');
|
human_readable(resp, result, '\n');
|
||||||
break;
|
break;
|
||||||
|
case FLAT:
|
||||||
|
flat_json("", resp, result);
|
||||||
|
break;
|
||||||
case JSON:
|
case JSON:
|
||||||
print_json(resp, result, "");
|
print_json(resp, result, "");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|||||||
@@ -1063,6 +1063,17 @@ def test_cli(node_factory):
|
|||||||
assert [l for l in lines if l.startswith('help=')] == []
|
assert [l for l in lines if l.startswith('help=')] == []
|
||||||
assert [l for l in lines if l.startswith('format-hint=')] == []
|
assert [l for l in lines if l.startswith('format-hint=')] == []
|
||||||
|
|
||||||
|
# Flat format is great for grep. LONG LIVE UNIX!
|
||||||
|
out = subprocess.check_output(['cli/lightning-cli',
|
||||||
|
'--network={}'.format(TEST_NETWORK),
|
||||||
|
'--lightning-dir={}'
|
||||||
|
.format(l1.daemon.lightning_dir),
|
||||||
|
'-F',
|
||||||
|
'help']).decode('utf-8')
|
||||||
|
lines = out.splitlines()
|
||||||
|
# Everything is a help[XX]= line, except format-hint.
|
||||||
|
assert [l for l in lines if not re.search(r'^help\[[0-9]*\].', l)] == ['format-hint=simple']
|
||||||
|
|
||||||
|
|
||||||
def test_daemon_option(node_factory):
|
def test_daemon_option(node_factory):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user