Skip to content

Commit df54ade

Browse files
committed
catch edit parser fail and save buffer in tempfile
1 parent 93d88a9 commit df54ade

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

netfoundry/ctl.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from tabulate import tabulate
2626
from yaml import dump as yaml_dumps
2727
from yaml import full_load as yaml_loads
28+
from yaml import parser
2829

2930
from ._version import get_versions
3031
from .exceptions import NFAPINoCredentials
@@ -501,7 +502,7 @@ def list(cli):
501502
# and the set of configured, desired keys
502503
valid_keys = set(matches[0].keys()) & set(cli.args.keys)
503504
elif cli.config.general.output == "text":
504-
valid_keys = set(matches[0].keys()) & set(['name','label','organizationShortName','id','edgeRouterAttributes','serviceAttributes','endpointAttributes','status','zitiId','provider','locationCode','ipAddress','region','size','attributes','email'])
505+
valid_keys = set(matches[0].keys()) & set(['name','label','organizationShortName','id','edgeRouterAttributes','serviceAttributes','endpointAttributes','status','zitiId','provider','locationCode','ipAddress','region','size','attributes','email','productVersion'])
505506

506507
if valid_keys:
507508
cli.log.debug("valid keys: %s", str(valid_keys))
@@ -716,18 +717,30 @@ def edit_object_as_yaml(edit: object):
716717
717718
:param obj input: a deserialized (object) to edit and return as yaml
718719
"""
720+
if cli.args.yes:
721+
return edit
719722
EDITOR = os.environ.get('NETFOUNDRY_EDITOR',os.environ.get('EDITOR','vim'))
720-
yaml_dumps(edit, default_flow_style=False)
721723
with tempfile.NamedTemporaryFile(suffix=".yml") as tf:
722-
tf.write(yaml_dumps(edit, default_flow_style=False).encode())
724+
tf.write(yaml_dumps(edit, default_flow_style=False))
723725
tf.flush()
724726
return_code = call(EDITOR.split()+[tf.name])
725727

726728
tf.seek(0)
727729
edited = tf.read()
728730
if return_code == 0:
729-
edited_object = yaml_loads(edited)
730-
return edited_object
731+
try:
732+
edited_object = yaml_loads(edited)
733+
except parser.ParserError as e:
734+
cli.log.error("invalid YAML or JSON: %s", e)
735+
with tempfile.NamedTemporaryFile(suffix=".yml") as tf:
736+
tf.write(edited)
737+
cli.log.warn("your buffer was saved in %s and you may edit and redirect to the same command as stdin or --file", tf.name)
738+
exit(1)
739+
except Exception as e:
740+
cli.log.error("unknown error in %s", e)
741+
exit(1)
742+
else:
743+
return edited_object
731744
else:
732745
cli.log.debug("error editing temporary file")
733746
exit(1)

0 commit comments

Comments
 (0)