cli.py: add option to print out resource as JSON
This commit is contained in:
parent
07e5d9845a
commit
bd03d4c6ad
15
cli.py
15
cli.py
@ -66,7 +66,15 @@ def init_cli_resource():
|
||||
@click.argument('path')
|
||||
@click.option('--all/--one', default=False)
|
||||
@click.option('--tag', default=None)
|
||||
def show(tag, all, path):
|
||||
@click.option('--use-json/--no-use-json', default=False)
|
||||
def show(use_json, tag, all, path):
|
||||
import json
|
||||
import six
|
||||
|
||||
printer = lambda r: six.print_(r)
|
||||
if use_json:
|
||||
printer = lambda r: six.print_(json.dumps(r.to_dict()))
|
||||
|
||||
if all or tag:
|
||||
for name, resource in xr.load_all(path).items():
|
||||
show = True
|
||||
@ -75,10 +83,9 @@ def init_cli_resource():
|
||||
show = False
|
||||
|
||||
if show:
|
||||
print resource
|
||||
print
|
||||
printer(resource)
|
||||
else:
|
||||
print xr.load(path)
|
||||
printer(xr.load(path))
|
||||
|
||||
resource.add_command(show)
|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import copy
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from copy import deepcopy
|
||||
|
||||
@ -45,11 +43,16 @@ class Resource(object):
|
||||
self.tags = tags or []
|
||||
|
||||
def __repr__(self):
|
||||
return ("Resource(name='{0}', metadata={1}, args={2}, "
|
||||
"tags={3})").format(self.name,
|
||||
json.dumps(self.metadata),
|
||||
json.dumps(self.args_show()),
|
||||
self.tags)
|
||||
return ("Resource(name='{name}', metadata={metadata}, args={args}, "
|
||||
"tags={tags})").format(**self.to_dict)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'name': self.name,
|
||||
'metadata': self.metadata,
|
||||
'args': self.args_show(),
|
||||
'tags': self.tags,
|
||||
}
|
||||
|
||||
def args_show(self):
|
||||
def formatter(v):
|
||||
|
Loading…
Reference in New Issue
Block a user