|
23 | 23 | # POSSIBILITY OF SUCH DAMAGE.
|
24 | 24 | """list module for the cli."""
|
25 | 25 | import click
|
| 26 | +import json |
26 | 27 | import texttable
|
27 | 28 | import typing
|
28 | 29 |
|
|
31 | 32 | import libiocage.lib.JailFilter
|
32 | 33 | import libiocage.lib.Logger
|
33 | 34 |
|
34 |
| -supported_output_formats = ['table', 'csv', 'list'] |
| 35 | +supported_output_formats = ['table', 'csv', 'list', 'json'] |
35 | 36 |
|
36 | 37 |
|
37 | 38 | @click.command(name="list", help="List a specified dataset type, by default"
|
@@ -97,6 +98,8 @@ def cli(ctx, dataset_type, header, _long, remote, plugins,
|
97 | 98 | _print_list(jails, columns, header, "\t")
|
98 | 99 | elif output_format == "csv":
|
99 | 100 | _print_list(jails, columns, header, ";")
|
| 101 | + elif output_format == "json": |
| 102 | + _print_json(jails, columns) |
100 | 103 | else:
|
101 | 104 | _print_table(jails, columns, header, _sort)
|
102 | 105 |
|
@@ -147,6 +150,26 @@ def _print_list(
|
147 | 150 | print(separator.join(_lookup_jail_values(jail, columns)))
|
148 | 151 |
|
149 | 152 |
|
| 153 | +def _print_json( |
| 154 | + jails: typing.Generator[libiocage.lib.Jails.JailsGenerator, None, None], |
| 155 | + columns: list, |
| 156 | + **json_dumps_args |
| 157 | +): |
| 158 | + |
| 159 | + if "indent" not in json_dumps_args.keys(): |
| 160 | + json_dumps_args["indent"] = 2 |
| 161 | + |
| 162 | + if "sort_keys" not in json_dumps_args.keys(): |
| 163 | + json_dumps_args["sort_keys"] = True |
| 164 | + |
| 165 | + output = [] |
| 166 | + |
| 167 | + for jail in jails: |
| 168 | + output.append(dict(zip(columns, _lookup_jail_values(jail, columns)))) |
| 169 | + |
| 170 | + print(json.dumps(output, **json_dumps_args)) |
| 171 | + |
| 172 | + |
150 | 173 | def _lookup_jail_values(jail, columns) -> typing.List[str]:
|
151 | 174 | return list(map(
|
152 | 175 | lambda column: jail.getstring(column),
|
|
0 commit comments