Skip to content

Commit

Permalink
Added variables
Browse files Browse the repository at this point in the history
  • Loading branch information
NekoLuka committed Sep 12, 2023
1 parent 5bda2f1 commit 4b7b893
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ The first command is equal to `echo {message} | base64`.
- params (list): A list of strings which are keys found in either the query string or request body (supported content types: JSON), whose value is passed to the command (note: values from duplicate keys are concatenated with spaces).
- return_stdout (bool): If the output of STDOUT should be returned when the command chain finishes successfully (default: true).
- commands (list): A list of objects containing the definitions for the commands that need to be executed (at least one route is required).
- variables (dict): A key-value combination where the value is a command of which the stdout is added to the params.

#### Commands options

Expand All @@ -91,7 +92,6 @@ To run all tests, run `python tests.py`

## Roadmap

- Add conditional logic in commands (by using the test command).
- Add more accepted request body types.
- Add files for accepted default responses.
- Support to run commands as different users on the system.
Expand Down
32 changes: 30 additions & 2 deletions commander.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@

class Commander:
def __init__(self, commands: List[CommandBody], return_stdout: bool = True,
use_params: bool = True, params: Dict[str, str] = None):
use_params: bool = True, params: Dict[str, str] = None, variables: Dict[str, str] = None):
self.commands = commands
self.return_stdout = return_stdout
self.params = params
self.params = params if params is not None else dict()
self.use_params = use_params
self.command_stack: List[CommandBody] = []
self.variables = variables if variables is not None else dict()

def parse_variables(self) -> Tuple[ResponseEnum, str]:
for key, value in self.variables.items():
status, evaluated_value = self.evaluate_variable(value)
if status != ResponseEnum.OK:
return status, evaluated_value
self.params[key] = evaluated_value
return ResponseEnum.OK, ""

def get_correct_stdin(self, stdin_param: Union[str, None]) -> Union[str, None]:
if self.use_params and stdin_param:
Expand All @@ -32,6 +41,9 @@ def check_return_code(return_code: int, expected_return_code: int) -> bool:
return return_code == expected_return_code

def execute_commands(self) -> Tuple[ResponseEnum, Union[str, None]]:
status, value = self.parse_variables()
if status != ResponseEnum.OK:
return status, value
for command in self.commands:
if command["condition"] is not None:
status, value = self.evaluate_condition(command["condition"])
Expand Down Expand Up @@ -76,3 +88,19 @@ def evaluate_condition(self, condition: str) -> Tuple[ResponseEnum, Union[int, s
)
_ = p.communicate()
return ResponseEnum.OK, 0 if self.check_return_code(p.returncode, 0) else 1

def evaluate_variable(self, variable: str) -> Tuple[ResponseEnum, str]:
try:
variable = self.format_command(variable)
except MissingParameterError as e:
return ResponseEnum.InternalServerError, str(e)
p = subprocess.Popen(
variable,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
text=True,
shell=True
)
stdout, _ = p.communicate()
return ResponseEnum.OK, stdout
2 changes: 2 additions & 0 deletions config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def _set_default_route_values(route: RouteBody) -> RouteBody:
route["params"] = []
if "return_stdout" not in keys:
route["return_stdout"] = True
if "variables" not in keys:
route["variables"] = {}
return route

@staticmethod
Expand Down
1 change: 1 addition & 0 deletions local_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class RouteBody(TypedDict):
params: Union[None, List[str]]
return_stdout: bool
commands: List[CommandBody]
variables: Dict[str, str]


class ParsedRouteBody(TypedDict):
Expand Down

0 comments on commit 4b7b893

Please sign in to comment.