From 7661c927b41aade6466b511d6975c6e5ab256364 Mon Sep 17 00:00:00 2001 From: Fredrik Corneliusson Date: Tue, 25 Jun 2024 10:33:50 +0200 Subject: [PATCH 1/2] #21 Add script exit status information to rawcmd output. --- click_web/resources/cmd_exec.py | 14 ++++++++++++-- tests/test_flask_post.py | 4 ++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/click_web/resources/cmd_exec.py b/click_web/resources/cmd_exec.py index 6367a74..371737d 100644 --- a/click_web/resources/cmd_exec.py +++ b/click_web/resources/cmd_exec.py @@ -1,3 +1,4 @@ +import json import logging import os import shlex @@ -44,7 +45,7 @@ def exec(self, command_path): command = request.data.decode('utf-8') return self._exec_raw(command) else: - return self._exec(command_path) + return self._exec_html(command_path) def _exec_raw(self, command): """ @@ -52,6 +53,8 @@ def _exec_raw(self, command): Execute the command as provided in the post data and stream the text output from it as response Note: This does not support posting of files and or generating output links to files. Also, it does not obfuscate secrets in the logs at the moment. + Last returned line is a json object with status and return code (exit code) of the command executed. + :param command: the command line after the root command. For example: print-lines 5 --delay 1 --message Red @@ -66,10 +69,17 @@ def generate(): yield f"\nERROR: Got exception when reading output from script: {type(e)}\n" yield traceback.format_exc() raise + finally: + if self.returncode == 0: + yield json.dumps({"result": "OK", "returncode": self.returncode, + "message": "Done"}) + else: + yield json.dumps({"result": "ERROR", "returncode": self.returncode, + "message": f'Script exited with error code: {self.returncode}'}) return Response(generate(), content_type='text/plain; charset=utf-8') - def _exec(self, command_path): + def _exec_html(self, command_path): """ Execute the command and stream the output from it as response :param command_path: diff --git a/tests/test_flask_post.py b/tests/test_flask_post.py index 1a5db3d..33629d2 100644 --- a/tests/test_flask_post.py +++ b/tests/test_flask_post.py @@ -1,3 +1,5 @@ +import json + import pytest from werkzeug.datastructures import MultiDict @@ -138,3 +140,5 @@ def test_rawcmd_exec_with_arg_and_default_opt(data, expected_msg, app, client): assert resp.status_code == 200 assert resp.content_type == 'text/plain; charset=utf-8' assert expected_msg in resp.data + last_line = resp.data.splitlines()[-1] + assert json.loads(last_line) == {"result": "OK", "returncode": 0, "message": "Done"} From bfd7a0d4f0834f7ea5bb02c0c96a4e5ca06128be Mon Sep 17 00:00:00 2001 From: Fredrik Corneliusson Date: Tue, 25 Jun 2024 10:35:45 +0200 Subject: [PATCH 2/2] Bumped version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index b5f9cf7..daceed3 100644 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ setup( name='click-web', - version='0.8.5', + version='0.8.6', url='https://github.com/fredrik-corneliusson/click-web', author='Fredrik Corneliusson', author_email='fredrik.corneliusson@gmail.com',