Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions reference/functions/classesmatching.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
layout: default
title: classesmatching
categories: [Reference, Functions, classesmatching]
published: true
alias: reference-functions-classesmatching.html
tags: [reference, functions, classesmatching]
---

### Function classesmatching

**Synopsis**: classesmatching(arg1) returns type **slist**


*arg1* : Regular expression, *in the range* .\*

Return the defined classes matching regex arg1

**Example**:


```cf3
body common control
{
bundlesequence => { run };
}

bundle agent run
{
vars:
"all" slist => classesmatching(".*");
"c" slist => classesmatching("cfengine");
reports:
"All classes = $(all)";
"Classes matching 'cfengine' = $(c)";
}

```

**Notes**:

This function searches for the regular expression in the list of
currently defined classes (hard, then soft, then local to the current
bundle).

This function replaces the `allclasses.txt` static file available
in older versions of CFEngine.

regex

A regular expression matching zero or more classes in the current list
of defined classes. The regular expression is not anchored
(See [Anchored vs. unanchored regular expressions](#Anchored-vs_002e-unanchored-regular-expressions)).

The function returns the list of classes matched.
47 changes: 47 additions & 0 deletions reference/functions/difference.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
layout: default
title: difference
categories: [Reference, Functions, difference]
published: true
alias: reference-functions-difference.html
tags: [reference, functions, difference]
---

### Function difference

**Synopsis**: difference(arg1,arg2) returns type **slist**

*arg1* : The name of the base list variable, *in the range*
[a-zA-Z0-9\_\$(){}\\[\\].:]+

*arg2* : The name of the subtracted list variable, *in the range*
[a-zA-Z0-9\_\$(){}\\[\\].:]+

Returns the unique elements in arg1 that are not in arg2.

**Example**:


```cf3
bundle agent test

{
vars:
"a" slist => { 1,2,3,"x" };
"b" slist => { "x" };

"listname1" slist => { "a", "b" };
"listname2" slist => { "a", "b" };
"$(listname1)_str" string => join(",", $(listname1));

"diff_$(listname1)_$(listname2)" slist => difference($(listname1), $(listname2));
"diff_$(listname1)_$(listname2)_str" string => join(",", "diff_$(listname1)_$(listname2)");

reports:
"The difference of list '$($(listname1)_str)' with '$($(listname2)_str)' is '$(diff_$(listname1)_$(listname2)_str)'";
}
```

**Notes**:

See also `intersection`.
59 changes: 59 additions & 0 deletions reference/functions/every.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
layout: default
title: every
categories: [Reference, Functions, every]
published: true
alias: reference-functions-every.html
tags: [reference, functions, every]
---

### Function every

**Synopsis**: every(arg1,arg2) returns type **class**


*arg1* : Regular expression to find, *in the range* .\*

*arg2* : The name of the list variable to check, *in the range*
[a-zA-Z0-9\_\$(){}\\[\\].:]+

Return true if every element of the list matches the regular expression.

**Example**:


```cf3
bundle agent test

{
classes:
"every1" expression => every(".*", "test");
"every2" expression => every(".", "test");

vars:
"test" slist => {
1,2,3,
"one", "two", "three",
"long string",
"four", "fix", "six",
"one", "two", "three",
};

reports:
"The test list is $(test)";
every1::
"every() test 1 passed";
!every1::
"every() test 1 failed";
every2::
"every() test 2 failed";
!every2::
"every() test 2 passed";
}
```

**Notes**:

See also `filter`, `some`, and `none`.

The regular expression is unanchored.
44 changes: 44 additions & 0 deletions reference/functions/filestat.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
layout: default
title: filestat
categories: [Reference, Functions, filestat]
published: true
alias: reference-functions-filestat.html
tags: [reference, functions, filestat]
---

### Function filestat

**Synopsis**: filestat(arg1, arg2) returns type **string**


*arg1* : File object name, *in the range* "?(/.\*)
*arg2* : Name of field to retrieve, *in the range*
size,gid,uid,ino,nlink,ctime,atime,mtime,mode,modeoct,permstr,permoct,type,devno,dev_minor,dev_major,basename,dirname

Returns the requested file field

**Example**:


```cf3
bundle agent fileinfo(f)
{
vars:
"fields" slist => splitstring("size,gid,uid,ino,nlink,ctime,atime,mtime,mode,modeoct,permstr,permoct,type,devno,dev_minor,dev_major,basename,dirname", ",", 999);

"stat[$(f)][$(fields)]" string => filestat($(f), $(fields));

reports:
"$(this.bundle): file $(f) has $(fields) = $(stat[$(f)][$(fields)])";
}
```

**Notes**:

The list of fields may be extended as needed.

*History*: Was introduced in version 3.5.0,Enterprise 3.1 (2013)

If the file object does not exist, the function call fails and the
variable does not expand.
67 changes: 67 additions & 0 deletions reference/functions/filter.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
layout: default
title: filter
categories: [Reference, Functions, filter]
published: true
alias: reference-functions-filter.html
tags: [reference, functions, filter]
---

### Function filter

**Synopsis**: filter(arg1,arg2,arg3,arg4,arg5) returns type **slist**


*arg1* : Anchored regular expression or static string to find, *in the range* .\*

*arg2* : The name of the list variable to check, *in the range*
[a-zA-Z0-9\_\$(){}\\[\\].:]+

*arg3* : Boolean: treat arg1 as a regular expression or as a static string. *in the range* true,false

*arg4* : Boolean: Invert filter. *in the range* true,false

*arg5* : Maximum number of elements to return *in the range* 0,999999999

Return list of up to arg5 elements of arg2 that match the specified filtering rules.

**Example**:


```cf3
bundle agent test

{
vars:
"test" slist => {
1,2,3,
"one", "two", "three",
"long string",
"one", "two", "three",
};

"test_grep" slist => filter("[0-9]", "test", "true", "false", 999);
"test_exact1" slist => filter("one", "test", "false", "false", 999);
"test_exact2" slist => filter(".", "test", "false", "false", 999);
"test_invert" slist => filter("[0-9]", "test", "true", "true", 999);
"test_max2" slist => filter(".*", "test", "true", "false", 2);
"test_max0" slist => filter(".*", "test", "true", "false", 0);
"grep" slist => grep("[0-9]", "test");

reports:
"The test list is $(test)";
"The grepped list is $(grep)";
"The filter-grepped list is $(test_grep)";
"The filter-exact list, looking for 'one' is $(test_exact1)";
"This line should not appear: $(test_exact2)";
"The filter-invert list, looking for non-digits, is $(test_invert)";
"The filter-bound list, matching at most 2 items, is $(test_max2)";
"This line should not appear: $(test_max0)";
}
```

**Notes**:

This is a generic filtering function to transform a list into a subset thereof.

See also `grep`, `every`, `some`, and `none`.
57 changes: 57 additions & 0 deletions reference/functions/format.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
layout: default
title: format
categories: [Reference, Functions, format]
published: true
alias: reference-functions-format.html
tags: [reference, functions, format]
---

### Function format

**Synopsis**: format(...) returns type **string**



Applies sprintf-style formatting to a given string, taking enough parameters to match the format.

**Example**:


```cf3
body common control
{
bundlesequence => { run };
}

bundle agent run
{
vars:
"v" string => "2.5.6";
"vlist" slist => splitstring($(v), "\.", 3);
"padded" string => format("%04d%04d%04d", nth("vlist", 0), nth("vlist", 1), nth("vlist", 2));
"a" string => format("%10.10s", "x");
"b" string => format("%-10.10s", "x");
"c" string => format("%04d", 1);
"d" string => format("%07.2f", 1);
"e" string => format("hello %s, my IP is %s", $(sys.policy_hub), $(sys.ipv4));

reports:
"version $(v) => padded $(padded)";
"%10.10s on 'x' => '$(a)'";
"%-10.10s on 'x' => '$(b)'";
"%04d on '1' => '$(c)'";
"%07.2f on '1' => '$(d)'";
"hello my IP is... => '$(e)'";
}
```

**Notes**:

This function will fail if it doesn't have enough arguments; if any
format *specifier* contains the *modifiers* `hLqjzt`; or if any format
*specifier* is not one of `doxfs`.

In other words, this function will format numbers (`o`, `x`, `d` and
`f`) or strings (`s`) but not potentially dangerous things like
individual characters or pointer offsets.
Loading