qvm-template: Initial support for machine-readable listings.
This commit is contained in:
parent
7b6fa39d1c
commit
336b5c68c1
@ -113,6 +113,8 @@ def parser_gen() -> argparse.ArgumentParser:
|
|||||||
' locally but not in repos) templates.'))
|
' locally but not in repos) templates.'))
|
||||||
parser_x.add_argument('--upgrades', action='store_true',
|
parser_x.add_argument('--upgrades', action='store_true',
|
||||||
help='Show upgradable templates.')
|
help='Show upgradable templates.')
|
||||||
|
parser_x.add_argument('--machine-readable', action='store_true',
|
||||||
|
help='Enable machine-readable output.')
|
||||||
parser_x.add_argument('templates', nargs='*', metavar='TEMPLATE')
|
parser_x.add_argument('templates', nargs='*', metavar='TEMPLATE')
|
||||||
# qvm-template search
|
# qvm-template search
|
||||||
parser_search = parser_add_command('search',
|
parser_search = parser_add_command('search',
|
||||||
@ -861,25 +863,46 @@ def list_templates(args: argparse.Namespace,
|
|||||||
tpl_list.append((status, data.name, version_str, data.reponame))
|
tpl_list.append((status, data.name, version_str, data.reponame))
|
||||||
|
|
||||||
def append_info(data, status, install_time=None):
|
def append_info(data, status, install_time=None):
|
||||||
tpl_list.append((status, 'Name', ':', data.name))
|
tpl_list.append((status, data, install_time))
|
||||||
tpl_list.append((status, 'Epoch', ':', data.epoch))
|
|
||||||
tpl_list.append((status, 'Version', ':', data.version))
|
def info_to_human_output(tpls):
|
||||||
tpl_list.append((status, 'Release', ':', data.release))
|
output = []
|
||||||
tpl_list.append((status, 'Size', ':',
|
# TODO: Do groupby here to avoid including ``status`` in each row.
|
||||||
qubesadmin.utils.size_to_human(data.dlsize)))
|
for status, data, install_time in tpls:
|
||||||
tpl_list.append((status, 'Repository', ':', data.reponame))
|
output.append((status, 'Name', ':', data.name))
|
||||||
tpl_list.append((status, 'Buildtime', ':', str(data.buildtime)))
|
output.append((status, 'Epoch', ':', data.epoch))
|
||||||
if install_time:
|
output.append((status, 'Version', ':', data.version))
|
||||||
tpl_list.append((status, 'Install time', ':', str(install_time)))
|
output.append((status, 'Release', ':', data.release))
|
||||||
tpl_list.append((status, 'URL', ':', data.url))
|
output.append((status, 'Size', ':',
|
||||||
tpl_list.append((status, 'License', ':', data.licence))
|
qubesadmin.utils.size_to_human(data.dlsize)))
|
||||||
tpl_list.append((status, 'Summary', ':', data.summary))
|
output.append((status, 'Repository', ':', data.reponame))
|
||||||
# Only show "Description" for the first line
|
output.append((status, 'Buildtime', ':', str(data.buildtime)))
|
||||||
title = 'Description'
|
if install_time:
|
||||||
for line in data.description.splitlines():
|
output.append((status, 'Install time', ':', str(install_time)))
|
||||||
tpl_list.append((status, title, ':', line))
|
output.append((status, 'URL', ':', data.url))
|
||||||
title = ''
|
output.append((status, 'License', ':', data.licence))
|
||||||
tpl_list.append((status, ' ', ' ', ' ')) # empty line
|
output.append((status, 'Summary', ':', data.summary))
|
||||||
|
# Only show "Description" for the first line
|
||||||
|
title = 'Description'
|
||||||
|
for line in data.description.splitlines():
|
||||||
|
output.append((status, title, ':', line))
|
||||||
|
title = ''
|
||||||
|
output.append((status, ' ', ' ', ' ')) # empty line
|
||||||
|
return output
|
||||||
|
|
||||||
|
def info_to_machine_output(tpls):
|
||||||
|
output = []
|
||||||
|
for status, data, install_time in tpls:
|
||||||
|
name, epoch, version, release, reponame, dlsize, \
|
||||||
|
buildtime, licence, url, summary, description = data
|
||||||
|
dlsize = str(dlsize)
|
||||||
|
buildtime = str(buildtime)
|
||||||
|
install_time = str(install_time) if install_time else ''
|
||||||
|
# TODO: Escape newlines in description?
|
||||||
|
output.append((status, name, epoch, version, release, reponame,
|
||||||
|
dlsize, buildtime, install_time, licence, url, summary,
|
||||||
|
description))
|
||||||
|
return output
|
||||||
|
|
||||||
if operation == 'list':
|
if operation == 'list':
|
||||||
append = append_list
|
append = append_list
|
||||||
@ -940,9 +963,19 @@ def list_templates(args: argparse.Namespace,
|
|||||||
if len(tpl_list) == 0:
|
if len(tpl_list) == 0:
|
||||||
parser.error('No matching templates to list')
|
parser.error('No matching templates to list')
|
||||||
|
|
||||||
for k, grp in itertools.groupby(tpl_list, lambda x: x[0]):
|
if not args.machine_readable:
|
||||||
print(k.title())
|
if operation == 'info':
|
||||||
qubesadmin.tools.print_table(list(map(lambda x: x[1:], grp)))
|
tpl_list = info_to_human_output(tpl_list)
|
||||||
|
for status, grp in itertools.groupby(tpl_list, lambda x: x[0]):
|
||||||
|
print(status.title())
|
||||||
|
qubesadmin.tools.print_table(list(map(lambda x: x[1:], grp)))
|
||||||
|
else:
|
||||||
|
if operation == 'info':
|
||||||
|
tpl_list = info_to_machine_output(tpl_list)
|
||||||
|
for status, grp in itertools.groupby(tpl_list, lambda x: x[0]):
|
||||||
|
print('|' + status.value)
|
||||||
|
for line in grp:
|
||||||
|
print('|'.join(line[1:]) + '|')
|
||||||
|
|
||||||
def search(args: argparse.Namespace, app: qubesadmin.app.QubesBase) -> None:
|
def search(args: argparse.Namespace, app: qubesadmin.app.QubesBase) -> None:
|
||||||
"""Command that searches template details for given patterns.
|
"""Command that searches template details for given patterns.
|
||||||
|
Loading…
Reference in New Issue
Block a user