Created
January 1, 2017 08:53
-
-
Save lilydjwg/b042e0393fa50d9906cdcc2b357f2be3 to your computer and use it in GitHub Desktop.
zfs-show: a wrapper to `zfs get` to show values from multiple properties in a table nicely
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import subprocess | |
from collections import namedtuple, OrderedDict | |
Record = namedtuple('Record', 'name property value source') | |
def get_widths(item): | |
return [len(x) for x in item.values()] | |
def main(argv): | |
output = subprocess.check_output(['zfs', 'get'] + argv) | |
items = [] | |
lines_iter = iter(output.decode().splitlines()) | |
next(lines_iter) | |
for l in lines_iter: | |
rec = Record(*l.split()) | |
if items and items[-1]['name'] == rec.name: | |
items[-1][rec.property] = rec.value | |
else: | |
d = OrderedDict() | |
d['name'] = rec.name | |
d[rec.property] = rec.value | |
items.append(d) | |
if not items: | |
return | |
widths = [get_widths(x) for x in items] | |
# header | |
widths.append([len(x) for x in items[0].keys()]) | |
column_widths = [max(x) for x in zip(*widths)] | |
fmt = '%%-%ds' % column_widths[0] | |
fmt += ' '.join('%%%ds' % w for w in column_widths[1:]) | |
print(fmt % tuple(items[0].keys())) | |
for x in items: | |
print(fmt % tuple(x.values())) | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment