summaryrefslogtreecommitdiff
path: root/icktool
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2018-04-20 18:41:18 +0300
committerLars Wirzenius <liw@liw.fi>2018-04-20 18:41:18 +0300
commitb5f373fad21a9ae97f02933ac216d65e98645928 (patch)
treefbce744a002c0c97421380c141a118b16a5a273f /icktool
parent659bc5b577097a451b162c9c552e803e3a824e39 (diff)
downloadick2-b5f373fad21a9ae97f02933ac216d65e98645928.tar.gz
Change: icktool status tabular output
Diffstat (limited to 'icktool')
-rwxr-xr-xicktool84
1 files changed, 76 insertions, 8 deletions
diff --git a/icktool b/icktool
index 81dd035..a6cfa1f 100755
--- a/icktool
+++ b/icktool
@@ -121,23 +121,37 @@ class Icktool(cliapp.Application):
self._prettyson(version)
def cmd_status(self, args):
+ table = Table()
+ table.set_columns('project', 'status', 'build_status', 'log_id')
+
api = self._new_api()
token = self._new_token(api)
api.set_token(token)
+
projects = api.show('/projects')
builds = api.show('/builds')
+
for project in projects['projects']:
project_name = project['project']
- status = api.show('/projects/{}/status'.format(project_name))
+ project_status = api.show(
+ '/projects/{}/status'.format(project_name))
+
+ row = {
+ 'project': project_name,
+ 'status': project_status['status'],
+ 'build_status': 'n/a',
+ 'log_id': 'n/a'
+ }
+
+
build = self._latest_build(project_name, builds)
- self._prettyson(builds)
if build:
- actions = build['actions']
- current = build['current_action']
- log = build['log']
- print(project_name, status['status'], log, actions[current])
- else:
- print(project_name, status['status'], 'no builds')
+ row['build_status'] = build['status']
+ row['log_id'] = build['log']
+
+ table.append_row(**row)
+
+ self.output.write(table.format())
def _latest_build(self, project_name, builds):
builds = [b for b in builds['builds'] if b['project'] == project_name]
@@ -217,4 +231,58 @@ class Icktool(cliapp.Application):
self.output.write('\n')
+class Table:
+
+ def __init__(self):
+ self._column_names = None
+ self._rows = []
+
+ def set_columns(self, *column_names):
+ self._column_names = column_names
+
+ def append_row(self, **kwargs):
+ self._rows.append(kwargs)
+
+ def format(self):
+ assert self._column_names is not None
+ headings = {
+ key: key
+ for key in self._column_names
+ }
+ self._rows.insert(0, headings)
+ widths = self._column_widths()
+ underlines = {
+ key: '-' * widths[key]
+ for key in self._column_names
+ }
+ self._rows.insert(1, underlines)
+ lines = [self._format_row(widths, row) for row in self._rows]
+ return ''.join('{}\n'.format(line) for line in lines)
+
+ def _format_headings(self, widths):
+ row = {
+ key: key
+ for key in self._column_names
+ }
+ return self._format_row(widths, row)
+
+ def _format_row(self, widths, row):
+ return ' | '.join(
+ self._format_cell(widths[x], row[x])
+ for x in self._column_names
+ )
+
+ def _format_cell(self, width, value):
+ return '%*s' % (width, value)
+
+ def _column_widths(self):
+ return {
+ key: self._width(key)
+ for key in self._column_names
+ }
+
+ def _width(self, column_name):
+ return max(len(str(row[column_name])) for row in self._rows)
+
+
Icktool(version=ick2.__version__).run()