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/client.py | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'cli/objects/client.py') diff --git a/cli/objects/client.py b/cli/objects/client.py index cf5e0b9..a2f8356 100644 --- a/cli/objects/client.py +++ b/cli/objects/client.py @@ -14,8 +14,11 @@ class OgClient(): @staticmethod def list_clients(rest): - r = rest.get('/clients') - print_json(r.text) + res = rest.get('/clients') + if not res: + return 1 + print_json(res.text) + return 0 @staticmethod def list_client_hardware(rest, args): @@ -28,8 +31,11 @@ class OgClient(): parsed_args = parser.parse_args(args) payload = {'client': parsed_args.client_ip} - r = rest.get('/hardware', payload=payload) - print_json(r.text) + res = rest.get('/hardware', payload=payload) + if not res: + return 1 + print_json(res.text) + return 0 @staticmethod def get_client_properties(rest, args): @@ -41,8 +47,11 @@ class OgClient(): parsed_args = parser.parse_args(args) payload = {'client': parsed_args.client_ip} - r = rest.get('/client/info', payload=payload) - print_json(r.text) + res = rest.get('/client/info', payload=payload) + if not res: + return 1 + print_json(res.text) + return 0 @staticmethod def request_refresh(rest, args): @@ -55,7 +64,10 @@ class OgClient(): parsed_args = parser.parse_args(args) payload = {'clients': parsed_args.client_ip} - rest.post('/refresh', payload=payload) + res = rest.post('/refresh', payload=payload) + if not res: + return 1 + return 0 @staticmethod def add_client(rest, args): @@ -82,8 +94,8 @@ class OgClient(): nargs='?', required=True, help='specify the ip address of the client') - r = rest.get('/mode') - boot_choices = r.json()['modes'] + res = rest.get('/mode') + boot_choices = res.json()['modes'] parser.add_argument('--boot-mode', nargs='?', required=True, @@ -100,7 +112,7 @@ class OgClient(): err = True if err: parser.print_help(file=sys.stderr) - sys.exit(1) + return 1 payload = { 'name': parsed_args.hostname, 'netmask': '0', @@ -113,7 +125,10 @@ class OgClient(): 'netdriver': 'generic', 'livedir': 'ogLive' } - rest.post('/client/add', payload=payload) + res = rest.post('/client/add', payload=payload) + if not res: + return 1 + return 0 @staticmethod def delete_client(rest, args): @@ -127,4 +142,7 @@ class OgClient(): payload = { 'clients': parsed_args.ip, } - rest.post('/client/delete', payload=payload) + res = rest.post('/client/delete', payload=payload) + if not res: + return 1 + return 0 -- cgit v1.2.3-18-g5258