Skip to content

Commit

Permalink
added run_command fuction to opnsense_utils (puzzle#107)
Browse files Browse the repository at this point in the history
* added run_command method and splited the run_function to avoid duplicate code

* simplified code
  • Loading branch information
KiLLuuuhh authored Apr 22, 2024
1 parent 914f4a4 commit 43c6087
Showing 1 changed file with 49 additions and 25 deletions.
74 changes: 49 additions & 25 deletions plugins/module_utils/opnsense_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@
import subprocess


def _run_php_command(php_cmd: str) -> dict:
"""
Helper method to execute a PHP command and capture the output.
Args:
php_cmd (str): The complete PHP command to execute.
Returns:
dict: A dictionary containing stdout, stderr, and return code details.
"""
cmd_result = subprocess.run(
["php", "-r", php_cmd],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=False, # do not raise exception if program fails
)

return {
"stdout": cmd_result.stdout.decode().strip(),
"stdout_lines": cmd_result.stdout.decode().strip().splitlines(),
"stderr": cmd_result.stderr.decode().strip(),
"stderr_lines": cmd_result.stderr.decode().strip().splitlines(),
"rc": cmd_result.returncode,
}


def run_function(
php_requirements: List[str], configure_function: str, configure_params: List = None
) -> dict:
Expand All @@ -26,34 +52,32 @@ def run_function(
and rc of the command
"""
if configure_params is None:
configure_params = []
params_string = ""
else:
params_string = ",".join(configure_params)

# assemble the php require statements
requirements_string = " ".join(
["require '" + req + "';" for req in php_requirements]
)
params_string = ",".join(configure_params)
requirements_string = " ".join([f"require '{req}';" for req in php_requirements])

# assemble php command
php_cmd = f"{requirements_string} {configure_function}({params_string});"
# run command
cmd_result = subprocess.run(
[
"php",
"-r",
php_cmd,
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
# do not raise exception if program fails
# handle subprocess process error in module using stderr
check=False,
)

return {
"stdout": cmd_result.stdout.decode().strip(),
"stdout_lines": cmd_result.stdout.decode().strip().splitlines(),
"stderr": cmd_result.stderr.decode().strip(),
"stderr_lines": cmd_result.stderr.decode().strip().splitlines(),
"rc": cmd_result.returncode,
}
return _run_php_command(php_cmd)


def run_command(php_requirements: List[str], command: str) -> dict:
"""
Executes a PHP command with specified requirements, capturing the output.
Args:
php_requirements (List[str]): PHP files to require before executing the command.
command (str): The PHP command to execute.
Returns:
dict: A dictionary containing stdout, stderr, and return code details.
"""

requirements_string = " ".join([f"require '{req}';" for req in php_requirements])
php_cmd = f"{requirements_string} {command}"

return _run_php_command(php_cmd)

0 comments on commit 43c6087

Please sign in to comment.