77
88import re
99import json
10+ import yaml
1011import getopt
1112
1213def convert_zones_to_identifier (cf , zone_name ):
@@ -92,13 +93,13 @@ def convert_virtual_dns_to_identifier(cf, virtual_dns_name):
9293
9394def cli4 (args ):
9495 verbose = False
95- quiet = False
96+ output = 'json'
9697 method = 'GET'
9798
98- usage = 'usage: cli4 [-h|--help] [-v|--verbose] [-q|--quiet] [--get|--patch|--post|-put|--delete] [item=value ...] /command...'
99+ usage = 'usage: cli4 [-h|--help] [-v|--verbose] [-q|--quiet] [-j|--json] [-y|--yaml] [- -get|--patch|--post|-put|--delete] [item=value ...] /command...'
99100
100101 try :
101- opts , args = getopt .getopt (args , 'hvq ' , ['help' , 'verbose' , 'quiet' , 'get' , 'patch' , 'post' , 'put' , 'delete' ])
102+ opts , args = getopt .getopt (args , 'hvqjy ' , ['help' , 'verbose' , 'quiet' , 'json' , 'yaml ' , 'get' , 'patch' , 'post' , 'put' , 'delete' ])
102103 except getopt .GetoptError :
103104 exit (usage )
104105 for opt , arg in opts :
@@ -107,7 +108,9 @@ def cli4(args):
107108 elif opt in ('-v' , '--verbose' ):
108109 verbose = True
109110 elif opt in ('-q' , '--quiet' ):
110- quiet = True
111+ output = None
112+ elif opt in ('-y' , '--yaml' ):
113+ output = 'yaml'
111114 elif opt in ('--get' ):
112115 method = 'GET'
113116 elif opt in ('-P' , '--patch' ):
@@ -131,8 +134,14 @@ def cli4(args):
131134 value = False
132135 elif value [0 ] is '=' and digits_only .match (value [1 :]):
133136 value = int (value [1 :])
134- elif value [0 ] is '[' and value [- 1 ] is ']' :
135- value = value [1 :- 1 ].split (',' )
137+ elif (value [0 ] is '{' and value [- 1 ] is '}' ) or (value [0 ] is '[' and value [- 1 ] is ']' ):
138+ # a json structure - used in pagerules
139+ try :
140+ #value = json.loads(value) - changed to yaml code to remove unicode strings
141+ value = yaml .safe_load (value )
142+ # success
143+ except ValueError :
144+ exit ('cli4: %s="%s" - can\' t parse json value' % (tag , value ))
136145 params [tag ] = value
137146
138147 # what's left is the command itself
@@ -215,6 +224,8 @@ def cli4(args):
215224 except Exception as e :
216225 exit ('cli4: /%s - %s - api error' % (command , e ))
217226
218- if not quiet :
227+ if output == 'json' :
219228 print json .dumps (r , indent = 4 , sort_keys = True )
229+ if output == 'yaml' :
230+ print yaml .dump (r )
220231
0 commit comments