diff --git a/SharedProcessors/VariableToString.py b/SharedProcessors/VariableToString.py new file mode 100644 index 0000000..1bd8d27 --- /dev/null +++ b/SharedProcessors/VariableToString.py @@ -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() diff --git a/Test-Recipes/VariableToString.test.recipe.yaml b/Test-Recipes/VariableToString.test.recipe.yaml new file mode 100644 index 0000000..f5d75b7 --- /dev/null +++ b/Test-Recipes/VariableToString.test.recipe.yaml @@ -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