Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.

Commit

Permalink
feat(patch): enable providing patch data with args as well as json file
Browse files Browse the repository at this point in the history
  • Loading branch information
devrimyatar committed May 7, 2021
1 parent 9f3e52c commit 4ed73d3
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions cli/config_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ def join(self):
parser.add_argument("--key-file", help="Path to SSL Key file")
parser.add_argument("-noverify", help="Ignore verifying the SSL certificate", action='store_true', default=True)

parser.add_argument("--patch-add", help="Colon delimited key:value pair for add patch operation. For example loggingLevel:DEBUG")
parser.add_argument("--patch-remove", help="Colon delimited key:value pair for remove patch operation. For example loggingLevel:DEBUG")
parser.add_argument("--patch-replace", help="Colon delimited key:value pair for replace patch operation. For example loggingLevel:DEBUG")
parser.add_argument("--patch-data", help="Colon delimited key:value pair for patch operation. For example loggingLevel:DEBUG")

# parser.add_argument("-show-data-type", help="Show data type in schema query", action='store_true')
parser.add_argument("--data", help="Path to json data file")
args = parser.parse_args()
Expand Down Expand Up @@ -1533,15 +1538,16 @@ def process_command_post(self, path, suffix_param, endpoint_params, data_fn):
def process_command_put(self, path, suffix_param, endpoint_params, data_fn):
self.process_command_post(path, suffix_param, endpoint_params, data_fn)

def process_command_patch(self, path, suffix_param, endpoint_params, data_fn):
def process_command_patch(self, path, suffix_param, endpoint_params, data_fn, data=None):

try:
data = self.get_json_from_file(data_fn)
except ValueError as ve:
self.exit_with_error(str(ve))
if not data:
try:
data = self.get_json_from_file(data_fn)
except ValueError as ve:
self.exit_with_error(str(ve))

if not isinstance(data, list):
self.exit_with_error("{} must be array of /components/schemas/PatchRequest".format(data_fn))
if not isinstance(data, list):
self.exit_with_error("{} must be array of /components/schemas/PatchRequest".format(data_fn))

op_modes = ('add', 'remove', 'replace', 'move', 'copy', 'test')

Expand Down Expand Up @@ -1598,11 +1604,33 @@ def process_command_by_id(self, operation_id, url_suffix, endpoint_args, data_fn
endpoint = Menu('', info=path)
schema = self.get_scheme_for_endpoint(endpoint)

if schema and not data_fn:

data = None
op_path = self.get_path_by_id(operation_id)
if op_path['__method__'] == 'patch' and not data_fn:
pop, pdata = '', ''
if args.patch_add:
pop = 'add'
pdata = args.patch_add
elif args.patch_remove:
pop = 'remove'
pdata = args.patch_remove
elif args.patch_replace:
pop = 'replace'
pdata = args.patch_replace

if pop:
if pdata.count(':') != 1:
self.exit_with_error("Please provide --patch-data as colon delimited key:value pair")

ppath, pval = pdata.split(':')
data = [{'op': pop, 'path': '/'+ ppath.lstrip('/'), 'value': pval}]

if (schema and not data_fn) and not data:
self.exit_with_error("Please provide schema with --data argument")

caller_function = getattr(self, 'process_command_' + path['__method__'])
caller_function(path, suffix_param, endpoint_params, data_fn)
caller_function(path, suffix_param, endpoint_params, data_fn, data=data)

def make_schema_val(self, stype):
if stype == 'object':
Expand Down Expand Up @@ -1717,6 +1745,8 @@ def main():
cliObject.process_command_by_id(args.operation_id, args.url_suffix, args.endpoint_args, args.data)
print()
except Exception as e:
if os.environ.get('errstdout'):
print(traceback.print_exc())
print(u"\u001b[38;5;{}mAn Unhandled error raised: {}\u001b[0m".format(error_color, e))
with open(error_log_file, 'a') as w:
traceback.print_exc(file=w)
Expand Down

0 comments on commit 4ed73d3

Please sign in to comment.