Ever wanted to manipulate a string - temporarily - for an individual promise? Check out the with
attribute and its special, $(with)
variable.
Sometimes you need some variation on a string for a specific case. Traditionally, to achieve this you’d simply define another variable.
Here is a contrived example:
I have a string, nginx
and I want to emit a report that contains both the string itself and the upper case version of the string.
bundle agent __main__
{
vars:
"MyString"
string => "nginx";
"MyString_upcase"
string => string_upcase( "$(MyString)" );
reports:
"'$(MyString)' upper-cased is '$(MyString_upcase)'";
}
cf-agent --no-lock --log-level info --file /tmp/feature-friday-2.cf
R: 'nginx' upper-cased is 'NGINX'
The with
attribute lets us define the $(with)
variable for a specific promise, letting us avoid the definition of a variable simply to hold the upper case version.
bundle agent __main__
{
vars:
"MyString" string => "nginx";
reports:
"'$(MyString)' upper-cased is '$(with)'"
with => string_upcase( "$(MyString)");
}
cf-agent --no-lock --log-level info --file /tmp/feature-friday-2-1.cf
R: 'nginx' upper-cased is 'NGINX'
The benefits of this really start to shine when you need to do the same translation on a list being iterated:
bundle agent __main__
{
vars:
"MyStrings"
slist => {
"nginx",
"apache",
"cherokee",
"tomcat",
"lighthttpd"
};
reports:
"'$(MyStrings)' upper-cased is '$(with)'"
with => string_upcase( "$(MyStrings)");
}
cf-agent --no-lock --log-level info --file /tmp/feature-friday-2-2.cf
R: 'nginx' upper-cased is 'NGINX'
R: 'apache' upper-cased is 'APACHE'
R: 'cherokee' upper-cased is 'CHEROKEE'
R: 'tomcat' upper-cased is 'TOMCAT'
R: 'lighthttpd' upper-cased is 'LIGHTHTTPD'
Happy Friday! 🎉