diff options
author | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-11-08 10:06:00 +0100 |
---|---|---|
committer | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-11-11 09:13:56 +0100 |
commit | 3d0aff3d1d47a053a678ecbec69d585cf1bbb275 (patch) | |
tree | 26bc0cf97e232bd8253b65a6824ad48766a37570 /cli/objects/repo.py | |
parent | 59f2f501aa6dfc2f238f670625c11d9fd3d49cef (diff) |
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.
Diffstat (limited to 'cli/objects/repo.py')
-rw-r--r-- | cli/objects/repo.py | 63 |
1 files changed, 42 insertions, 21 deletions
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 |