{devices,flavours}/cli: add --output-file for json dumping
This commit is contained in:
parent
161e14a438
commit
2ad4690c0a
2 changed files with 35 additions and 13 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue