Skip to content

Commit

Permalink
add variabletostring processor
Browse files Browse the repository at this point in the history
  • Loading branch information
jgstew committed Sep 29, 2023
1 parent 7d95b00 commit 6db107c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
52 changes: 52 additions & 0 deletions SharedProcessors/VariableToString.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/local/autopkg/python
#
# James Stewart @JGStew - 2023
#
"""See docstring for VariableToString class"""

from autopkglib import ( # pylint: disable=import-error,wrong-import-position,unused-import
Processor,
ProcessorError,
)

__all__ = ["VariableToString"]


class VariableToString(Processor):
"""Reads a variable and returns it as a string."""

description = __doc__

input_variables = {
"input_variable": {
"required": True,
"description": "The variable to read from.",
},
"output_variable": {
"required": False,
"description": "Name of the output variable to store the value.",
},
}
output_variables = {}

__doc__ = description

def main(self):
"""Execution starts here"""
input_variable = self.env.get("input_variable", "")
output_variable = self.env.get("output_variable", input_variable)

value = str(self.env.get(input_variable))

if value is not None:
self.env[output_variable] = value
self.output_variables[output_variable] = {
"description": "the custom output"
}
else:
raise ProcessorError(f"Variable not found.")


if __name__ == "__main__":
PROCESSOR = VariableToString()
PROCESSOR.execute_shell()
26 changes: 26 additions & 0 deletions Test-Recipes/VariableToString.test.recipe.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
Description: Test VariableToString Processor
Identifier: com.github.jgstew.test.VariableToString
Input:
NAME: VariableToStringTest
float: 1.2
int: 1
MinimumVersion: "2.3"
Process:
- Processor: com.github.jgstew.SharedProcessors/VariableToString
Arguments:
input_variable: float

- Processor: com.github.jgstew.SharedProcessors/AssertInputContainsString
Arguments:
input_string: "%float%"
assert_string: 1.2

- Processor: com.github.jgstew.SharedProcessors/VariableToString
Arguments:
input_variable: int

- Processor: com.github.jgstew.SharedProcessors/AssertInputContainsString
Arguments:
input_string: "%int%"
assert_string: 1

0 comments on commit 6db107c

Please sign in to comment.