From 10a3897f92ee058155987cea23eec28b28377d4b Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Thu, 21 Nov 2024 16:46:18 +0100 Subject: cli: ensure the program returns 0 on success and 1 on error propagate a returncode in each operation and make it the returncode of the program. Prevent sys.exit calls in post(), get() and delete() request handlers to enable cleanup code and error handling. Keep a basic error log inside the request functions if the connection can't be established or if the response contains an error code. --- cli/objects/repo.py | 63 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 21 deletions(-) (limited to 'cli/objects/repo.py') diff --git a/cli/objects/repo.py b/cli/objects/repo.py index 7085633..d172a0d 100644 --- a/cli/objects/repo.py +++ b/cli/objects/repo.py @@ -13,8 +13,11 @@ class OgRepo(): @staticmethod def list_repos(rest): - r = rest.get('/repositories') - print_json(r.text) + res = rest.get('/repositories') + if not res: + return 1 + print_json(res.text) + return 0 @staticmethod def _add_repo(rest, parsed_args): @@ -22,37 +25,43 @@ class OgRepo(): 'addr': parsed_args.ip, 'name': parsed_args.name, } - rest.post('/repository/add', payload=payload) + res = rest.post('/repository/add', payload=payload) + if not res: + return 1 + return 0 @staticmethod def _add_repo_ip(rest, parsed_args): for ip in parsed_args.ip: if not check_address(ip): print(f'Invalid IP address: {ip}') - return + return 1 - r = rest.get('/repositories') + res = rest.get('/repositories') target_repo = None - for repo in r.json()['repositories']: + for repo in res.json()['repositories']: if repo['id'] == parsed_args.id: target_repo = repo break if not target_repo: print('Invalid repository id specified') - return + return 1 for ip in parsed_args.ip: if ip in target_repo['addr']: print(f'The repository already contains the address {ip}') - return + return 1 payload = { 'id': parsed_args.id, 'addr': target_repo['addr'] + parsed_args.ip, 'name': target_repo['name'], } - rest.post('/repository/update', payload=payload) + res = rest.post('/repository/update', payload=payload) + if not res: + return 1 + return 0 @staticmethod def add_repo(rest, args): @@ -101,30 +110,33 @@ class OgRepo(): 'name': parsed_args.name, } - rest.post('/repository/update', payload=payload) + res = rest.post('/repository/update', payload=payload) + if not res: + return 1 + return 0 @staticmethod def _delete_repo_ip(rest, parsed_args): for ip in parsed_args.ip: if not check_address(ip): print(f'Invalid IP address: {ip}') - return + return 1 - r = rest.get('/repositories') + res = rest.get('/repositories') target_repo = None - for repo in r.json()['repositories']: + for repo in res.json()['repositories']: if repo['id'] == parsed_args.id: target_repo = repo break if not target_repo: print('Invalid repository id specified') - return + return 1 for ip in parsed_args.ip: if ip not in target_repo['addr']: print(f'Invalid address {ip}: The repository has the following IPs: {target_repo["addr"]}') - return + return 1 target_repo['addr'].remove(ip) @@ -133,12 +145,18 @@ class OgRepo(): 'addr': target_repo['addr'], 'name': target_repo['name'], } - rest.post('/repository/update', payload=payload) + res = rest.post('/repository/update', payload=payload) + if not res: + return 1 + return 0 @staticmethod def _delete_repo(rest, parsed_args): payload = {'id': parsed_args.id} - rest.post('/repository/delete', payload=payload) + res = rest.post('/repository/delete', payload=payload) + if not res: + return 1 + return 0 @staticmethod def delete_repo(rest, args): @@ -184,8 +202,8 @@ class OgRepo(): help='Any valid client ip address') parsed_args = parser.parse_args(args) - r = rest.get('/scopes') - scopes = r.json() + res = rest.get('/scopes') + scopes = res.json() ips = set() for room in parsed_args.room_id: @@ -195,7 +213,10 @@ class OgRepo(): ips.add(l) if not ips: print('No valid clients specified.') - return + return 1 payload = {'id': parsed_args.id, 'clients': list(ips)} - rest.post('/client/repo', payload=payload) + res = rest.post('/client/repo', payload=payload) + if not res: + return 1 + return 0 -- cgit v1.2.3-18-g5258