Skip to content

Commit

Permalink
fix: bash provider and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
shahargl committed Feb 13, 2024
1 parent a851905 commit b353371
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/workflows/bash_example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gi
34 changes: 26 additions & 8 deletions keep/providers/bash_provider/bash_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def _query(self, **kwargs):
Returns:
_type_: _description_
"""
timeout = kwargs.get("timeout", 60)
command = kwargs.get("command", "")
parsed_command = self.io_handler.parse(command)
# parse by pipes
Expand Down Expand Up @@ -57,14 +58,31 @@ def _query(self, **kwargs):
processes.append(process)

# Get the final output
stdout, stderr = processes[-1].communicate()
return_code = processes[-1].returncode
# stdout and stderr are strings or None
if stdout:
stdout = stdout.decode()

if stderr:
stderr = stderr.decode()
try:
stdout, stderr = processes[-1].communicate(timeout=timeout)
return_code = processes[-1].returncode
# stdout and stderr are strings or None
if stdout or stdout == b"":
stdout = stdout.decode()

if stderr or stderr == b"":
stderr = stderr.decode()
except subprocess.TimeoutExpired:
# use check_output to get the output of the last process
# this is some MacOS bug, where communicate() doesn't work and raise TimeoutExpired (idk why but it works on Linux)
try:
self.logger.warning("TimeoutExpired, using check_output - MacOS bug?")
stdout = subprocess.check_output(
cmd, stderr=subprocess.STDOUT, timeout=timeout, shell=True
).decode()
stderr = None
return_code = 0
self.logger.warning("check_output worked")
# todo: fix that
except Exception as e:
stdout = None
stderr = e.args[1].decode()
return_code = e.args[0]

return {"stdout": stdout, "stderr": stderr, "return_code": return_code}

Expand Down

0 comments on commit b353371

Please sign in to comment.