Skip to content

Commit

Permalink
Fixes #11602: Add a method to compute and format output
Browse files Browse the repository at this point in the history
  • Loading branch information
amousset committed Oct 12, 2017
1 parent 4f4811e commit 02a35eb
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#######################################################
#
# Read a file into a string
#
#######################################################

bundle common acc_path
{
vars:
"root" string => getenv("NCF_TESTS_ACCEPTANCE", 1024);
}

body common control
{
inputs => { "${acc_path.root}/default.cf.sub", "${acc_path.root}/default_ncf.cf.sub", "@{ncf_inputs.default_files}" };
bundlesequence => { configuration, default("${this.promise_filename}") };
version => "1.0";
}

#######################################################

bundle agent init
{
vars:
"tmp" string => getenv("TEMP", 1024);
"three" string => "3";
}

#######################################################

bundle agent test
{
methods:

"ph1" usebundle => variable_string_from_expression("prefix", "var1", "${init.three}*(2.0+3.0)", "%d");
}

#######################################################

bundle agent check
{
classes:

"ok_1" expression => "variable_string_from_expression_var1_kept.!variable_string_from_expression_var1_repaired.!variable_string_from_expression_var1_error";
"ok_var1" expression => strcmp("${prefix.var1}", "15");

"ok" expression => "ok_1.ok_var1";

reports:
ok::
"$(this.promise_filename) Pass";
!ok::
"$(this.promise_filename) FAIL";
}
79 changes: 79 additions & 0 deletions tree/30_generic_methods/variable_string_from_expression.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#####################################################################################
# Copyright 2017 Normation SAS
#####################################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, Version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#####################################################################################

# @name Variable string from expression
# @description Define a variable from an expression
# @documentation To use the generated variable, you must use the form `${variable_prefix.variable_name}` with each name replaced with the parameters of this method.
#
# Be careful that using a global variable can lead to unpredictable content in case of multiple definition, which is implicitly the case when a technique has more than one instance (directive).
# Please note that only global variables are available within templates.
#
# ## Usage
#
# This function will evaluate a mathematical expression that may contain variables and format the result according to the provided formatt string.
#
# The formatting string uses the standard POSIX printf format.
#
# #### Examples
#
# If you use:
#
# ```
# variable_string("prefix", "var", "10");
# variable_string_from_expression("prefix", "sum", "2.0+3.0", "%d");
# variable_string_from_expression("prefix", "product", "3*${prefix.var}", "%d");
# ```
#
# The `prefix.sum` string variable will contain `5` and `prefix.product` will contain `30`.
#
# @parameter variable_prefix The prefix of the variable name
# @parameter variable_name The variable to define, the full name will be variable_prefix.variable_name
# @parameter expression The mathematical expression to evaluate
# @parameter format The format string to use
#
# @class_prefix variable_string_from_expression
# @class_parameter variable_name

bundle agent variable_string_from_expression(variable_prefix, variable_name, expression, format)
{
vars:
"old_class_prefix" string => canonify("variable_string_from_expression_${variable_name}");
"promisers" slist => { @{this.callers_promisers}, cf_null }, policy => "ifdefined";
"class_prefix" string => canonify(join("_", "promisers"));
"args" slist => { "${variable_prefix}", "${variable_name}", "${expression}", "${format}" };

# define the variable within the variable_prefix namespace
"temp" string => eval("${expression}");
"${variable_prefix}.${variable_name}" string => format("${format}", "${temp}");

classes:
"variable_defined" expression => isvariable("${variable_prefix}.${variable_name}");

methods:
!variable_defined::
"error" usebundle => _classes_failure("${old_class_prefix}");
"error" usebundle => _classes_failure("${class_prefix}");

variable_defined::
"success" usebundle => _classes_success("${old_class_prefix}");
"success" usebundle => _classes_success("${class_prefix}");

any::
"report"
usebundle => _log("Set the string ${variable_prefix}.${variable_name} to the result of ${expression} formatted by '${format}'", "${old_class_prefix}", "${class_prefix}", @{args});
}

0 comments on commit 02a35eb

Please sign in to comment.