40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
"""
|
|
Command execution for the Edison application.
|
|
"""
|
|
import subprocess
|
|
import logging
|
|
from edison.utils import validation
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def execute_command(shell, command):
|
|
"""
|
|
Execute a shell command.
|
|
|
|
Args:
|
|
shell (str): The shell to use.
|
|
command (str): The command to execute.
|
|
|
|
Returns:
|
|
subprocess.CompletedProcess: The result of the command execution.
|
|
|
|
Raises:
|
|
subprocess.CalledProcessError: If the command execution fails.
|
|
"""
|
|
if validation.is_dangerous_command(command):
|
|
logger.warning(f"Potentially dangerous command detected: {command}")
|
|
# We still allow execution but log a warning
|
|
|
|
try:
|
|
if shell == "powershell.exe":
|
|
result = subprocess.run([shell, "/c", command], shell=False, check=True)
|
|
else:
|
|
# Unix: /bin/bash /bin/zsh: uses -c both Ubuntu and macOS should work, others might not
|
|
result = subprocess.run([shell, "-c", command], shell=False, check=True)
|
|
|
|
logger.debug(f"Command executed successfully: {command}")
|
|
return result
|
|
except subprocess.CalledProcessError as e:
|
|
logger.error(f"Command execution failed: {e}")
|
|
raise
|