Source code for mailos.tools.bash_command
"""Bash command execution tool."""
import subprocess
from typing import Dict
from mailos.utils.logger_utils import logger
from mailos.vendors.models import Tool
[docs]
def run_command(command: str) -> Dict:
"""Execute a bash command and return its output.
Args:
command: The command to execute
Returns:
Dict containing command output or error message
"""
try:
# Execute command and capture output
result = subprocess.run(
command,
shell=True,
check=True,
capture_output=True,
text=True,
)
return {
"status": "success",
"data": {
"stdout": result.stdout,
"stderr": result.stderr,
"return_code": result.returncode,
}
}
except subprocess.CalledProcessError as e:
logger.error(f"Command failed: {str(e)}")
return {
"status": "error",
"message": f"Command failed with return code {e.returncode}: {e.stderr}"
}
except Exception as e:
logger.error(f"Error executing command: {str(e)}")
return {
"status": "error",
"message": str(e)
}
# Define the bash command tool
bash_command_tool = Tool(
name="bash_command",
description="Execute bash commands on the system",
parameters={
"type": "object",
"properties": {
"command": {
"type": "string",
"description": "The bash command to execute",
}
},
},
required_params=["command"],
function=run_command,
)