Skip to content

Commit

Permalink
Merge pull request #22 from fredrik-corneliusson/21-add-script-exit-s…
Browse files Browse the repository at this point in the history
…tatus-information-to-rawcmd-output

21 add script exit status information to rawcmd output
  • Loading branch information
fredrik-corneliusson authored Jun 25, 2024
2 parents cea7f1d + bfd7a0d commit d98693d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
14 changes: 12 additions & 2 deletions click_web/resources/cmd_exec.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import os
import shlex
Expand Down Expand Up @@ -44,14 +45,16 @@ 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):
"""
This is for providing an API that is easy to call from scripts etc.
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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions tests/test_flask_post.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

import pytest
from werkzeug.datastructures import MultiDict

Expand Down Expand Up @@ -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"}

0 comments on commit d98693d

Please sign in to comment.