diff --git a/devices/cli.py b/devices/cli.py index c2fcb38..c35811e 100644 --- a/devices/cli.py +++ b/devices/cli.py @@ -24,7 +24,13 @@ from .device import get_devices, get_profile_device default=False, help='Download packages while trying to parse deviceinfo', ) -def cmd_devices(json: bool = False, force_parse_deviceinfo: Optional[bool] = True, download_packages: bool = False): +@click.option('--output-file', type=click.Path(exists=False, file_okay=True), help="Dump JSON to file") +def cmd_devices( + json: bool = False, + force_parse_deviceinfo: Optional[bool] = True, + download_packages: bool = False, + output_file: Optional[str] = None, +): 'list the available devices and descriptions' devices = get_devices() if not devices: @@ -38,7 +44,10 @@ def cmd_devices(json: bool = False, force_parse_deviceinfo: Optional[bool] = Tru logging.debug(f"Failed to get profile device for visual highlighting, not a problem: {ex}") output = [''] json_output = {} - use_colors = colors_supported(False if json else config.runtime.colors) + interactive_json = json and not output_file + if output_file: + json = True + use_colors = colors_supported(False if interactive_json else config.runtime.colors) for name in sorted(devices.keys()): prefix = '' suffix = '' @@ -55,7 +64,8 @@ def cmd_devices(json: bool = False, force_parse_deviceinfo: Optional[bool] = Tru if json: json_output[name] = device.get_summary().toDict() - continue + if interactive_json: + continue if profile_device and profile_device.name == device.name: prefix = color_str('>>> ', bold=True, fg="bright_green", use_colors=use_colors) suffix = '\n\n' @@ -65,7 +75,10 @@ def cmd_devices(json: bool = False, force_parse_deviceinfo: Optional[bool] = Tru # prefix each line in the snippet snippet = '\n'.join([f'{prefix}{line}' for line in snippet.split('\n')]) output.append(f"{snippet}\n") - if json: + if interactive_json: output = ['\n' + json_dump(json_output, indent=4)] + if output_file: + with open(output_file, 'w') as fd: + fd.write(json_dump(json_output)) for line in output: print(line) diff --git a/flavours/cli.py b/flavours/cli.py index 9634954..a9ccdb7 100644 --- a/flavours/cli.py +++ b/flavours/cli.py @@ -1,7 +1,8 @@ import click import logging -from json import dumps +from json import dumps as json_dump +from typing import Optional from config.state import config from utils import colors_supported, color_str @@ -13,15 +14,20 @@ profile_option = click.option('-p', '--profile', help="name of the profile to us @click.command(name='flavours') @click.option('-j', '--json', is_flag=True, help='output machine-parsable JSON format') -def cmd_flavours(json: bool = False): +@click.option('--output-file', type=click.Path(exists=False, file_okay=True), help="Dump JSON to file") +def cmd_flavours(json: bool = False, output_file: Optional[str] = None): 'list information about available flavours' results = [] + json_results = {} profile_flavour = None flavours = get_flavours() - use_colors = colors_supported(config.runtime.colors) + interactive_json = json and not output_file + use_colors = colors_supported(config.runtime.colors) and not interactive_json + if output_file: + json = True if not flavours: raise Exception("No flavours found!") - if not json: + if not interactive_json: try: profile_flavour = get_profile_flavour() except Exception as ex: @@ -32,7 +38,7 @@ def cmd_flavours(json: bool = False): f.parse_flavourinfo() except Exception as ex: logging.debug(f"A problem happened while parsing flavourinfo for {name}, continuing anyway. Exception: {ex}") - if not json: + if not interactive_json: block = [*f.nice_str(newlines=True, colors=use_colors).split('\n'), ''] if profile_flavour == f: prefix = color_str('>>> ', bold=True, fg='bright_green', use_colors=use_colors) @@ -42,7 +48,7 @@ def cmd_flavours(json: bool = False): ] block = [prefix + line for line in block] results += block - else: + if json: d = dict(f) d["description"] = f.flavour_info.description if (f.flavour_info and f.flavour_info.description) else f.description if "flavour_info" in d and d["flavour_info"]: @@ -52,10 +58,13 @@ def cmd_flavours(json: bool = False): d["pkgbuild"] = f.pkgbuild.path if f.pkgbuild else None d["package"] = f.pkgbuild.name d["arches"] = sorted(f.pkgbuild.arches) if f.pkgbuild else None - results += [d] + json_results[d["name"]] = d print() - if json: - print(dumps({r['name']: r for r in results}, indent=4)) + if output_file: + with open(output_file, 'w') as fd: + fd.write(json_dump(json_results)) + if interactive_json: + print(json_dump(json_results, indent=4)) else: for r in results: print(r)