mount-tool: add support for a --json= output mode

Let's add a JSON output mode, like we have it for so many of our tools.
This commit is contained in:
Lennart Poettering 2024-07-08 13:44:12 +02:00
parent 070a43ced4
commit 66f4f49c8f
3 changed files with 14 additions and 8 deletions

View File

@ -121,6 +121,7 @@
<xi:include href="standard-options.xml" xpointer="no-pager"/>
<xi:include href="standard-options.xml" xpointer="no-legend" />
<xi:include href="standard-options.xml" xpointer="no-ask-password"/>
<xi:include href="standard-options.xml" xpointer="json"/>
<varlistentry>
<term><option>--quiet</option></term>

View File

@ -74,6 +74,7 @@ static gid_t arg_gid = GID_INVALID;
static bool arg_fsck = true;
static bool arg_aggressive_gc = false;
static bool arg_tmpfs = false;
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
STATIC_DESTRUCTOR_REGISTER(arg_mount_what, freep);
STATIC_DESTRUCTOR_REGISTER(arg_mount_where, freep);
@ -128,6 +129,7 @@ static int help(void) {
" -l --full Do not ellipsize output\n"
" --no-ask-password Do not prompt for password\n"
" -q --quiet Suppress information messages during runtime\n"
" --json=pretty|short|off Generate JSON output\n"
" --user Run as user unit\n"
" -H --host=[USER@]HOST Operate on remote host\n"
" -M --machine=CONTAINER Operate on local container\n"
@ -176,6 +178,7 @@ static int parse_argv(int argc, char *argv[]) {
ARG_AUTOMOUNT_PROPERTY,
ARG_BIND_DEVICE,
ARG_LIST,
ARG_JSON,
};
static const struct option options[] = {
@ -207,6 +210,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "unmount", no_argument, NULL, 'u' }, /* Compat spelling */
{ "collect", no_argument, NULL, 'G' },
{ "tmpfs", no_argument, NULL, 'T' },
{ "json", required_argument, NULL, ARG_JSON },
{},
};
@ -361,6 +365,13 @@ static int parse_argv(int argc, char *argv[]) {
arg_tmpfs = true;
break;
case ARG_JSON:
r = parse_json_argument(optarg, &arg_json_format_flags);
if (r <= 0)
return r;
break;
case '?':
return -EINVAL;
@ -1435,7 +1446,6 @@ static int list_devices(void) {
if (r < 0)
return log_error_errno(r, "Failed to set sort index: %m");
table_set_header(table, arg_legend);
table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
FOREACH_DEVICE(e, d) {
@ -1479,13 +1489,7 @@ static int list_devices(void) {
}
}
pager_open(arg_pager_flags);
r = table_print(table, NULL);
if (r < 0)
return table_log_print_error(r);
return 0;
return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
}
static int run(int argc, char* argv[]) {

View File

@ -23,6 +23,7 @@ systemd-mount --list --full
systemd-mount --list --no-legend
systemd-mount --list --no-pager
systemd-mount --list --quiet
systemd-mount --list --json=pretty
# tmpfs
mkdir -p "$WORK_DIR/mnt/foo/bar"