1# Copyright 2021 Northern.tech AS 2 3# This file is part of CFEngine 3 - written and maintained by Northern.tech AS. 4 5# This program is free software; you can redistribute it and/or modify it 6# under the terms of the GNU General Public License as published by the 7# Free Software Foundation; version 3. 8 9# This program is distributed in the hope that it will be useful, 10# but WITHOUT ANY WARRANTY; without even the implied warranty of 11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12# GNU General Public License for more details. 13 14# You should have received a copy of the GNU General Public License 15# along with this program; if not, write to the Free Software 16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 17 18# To the extent this program is licensed as part of the Enterprise 19# versions of Cfengine, the applicable Commercial Open Source License 20# (COSL) may apply to this file if you as a licensee so wish it. See 21# included file COSL.txt. 22 23#+begin_src prep 24#@ ``` 25#@ echo 'ClassExpr,Sort,Token,Value' > /tmp/classfiltercsv.csv 26#@ echo '# This is a comment' >> /tmp/classfiltercsv.csv 27#@ echo 'any,A,net.ipv4.ip_forward,ANYVALUE' >> /tmp/classfiltercsv.csv 28#@ echo 'example_class1,z,net.ipv4.ip_forward,ANYVALUE' >> /tmp/classfiltercsv.csv 29#@ echo 'example_class2,a,net.ipv4.ip_forward,127.0.0.3' >> /tmp/classfiltercsv.csv 30#@ echo 'not_defined,Z,net.ipv4.ip_forward,NOT_DEFINED' >> /tmp/classfiltercsv.csv 31#@ echo 'example_class3,1,net.ipv4.ip_forward,127.0.0.4' >> /tmp/classfiltercsv.csv 32#@ echo 'also_undefined,0,net.ipv4.ip_forward,NOT_DEFINED' >> /tmp/classfiltercsv.csv 33#@ sed -i 's/$/\r/' /tmp/classfiltercsv.csv 34#@ ``` 35#+end_src 36 37############################################################################### 38 39#+begin_src cfengine3 40bundle agent example_classfiltercsv 41{ 42 classes: 43 "example_class1"; 44 "example_class2"; 45 "example_class3"; 46 47 vars: 48 "data_file" string => "/tmp/classfiltercsv.csv"; 49 "d" data => classfiltercsv($(data_file), "true", 0, 1); 50 51 reports: 52 "Filtered data: $(with)" with => string_mustache("{{%-top-}}", d); 53} 54bundle agent __main__ 55{ 56 methods: 57 "example_classfiltercsv"; 58} 59#+end_src 60#+begin_src example_output 61#@ ``` 62#@ R: Filtered data: [ 63#@ { 64#@ "Sort": "1", 65#@ "Token": "net.ipv4.ip_forward", 66#@ "Value": "127.0.0.4" 67#@ }, 68#@ { 69#@ "Sort": "A", 70#@ "Token": "net.ipv4.ip_forward", 71#@ "Value": "ANYVALUE" 72#@ }, 73#@ { 74#@ "Sort": "a", 75#@ "Token": "net.ipv4.ip_forward", 76#@ "Value": "127.0.0.3" 77#@ }, 78#@ { 79#@ "Sort": "z", 80#@ "Token": "net.ipv4.ip_forward", 81#@ "Value": "ANYVALUE" 82#@ } 83#@ ] 84#@ ``` 85#+end_src 86