1#  Copyright 2020 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 "MATCHING before region" > /tmp/example_select_region.txt
26#@ echo "BEGIN" >> /tmp/example_select_region.txt
27#@ echo "MATCHING inside region" >> /tmp/example_select_region.txt
28#@ echo "END" >> /tmp/example_select_region.txt
29#@ echo "MATCHING after region" >> /tmp/example_select_region.txt
30#@ ```
31#+end_src
32
33#+begin_src cfengine3
34bundle agent main
35# @brief Demonstrate how edit_line can operate within a region of a file
36{
37
38  vars:
39      "file" string => "/tmp/example_select_region.txt";
40  files:
41
42      "$(file)"
43        comment => "We want to delete any lines that begin with the string
44                  MATCHING inside the region described by regular expressions
45                  matching the beginning and end of the region.",
46        create    => "true",
47        edit_line => delete_inside_region("^MATCHING.*",
48                                          "^BEGIN.*",
49                                          "^END.*");
50
51  reports:
52      "$(file)" printfile => cat( $(this.promiser) );
53}
54
55########################################################
56
57bundle edit_line delete_inside_region(line_reg, begin_reg, end_reg)
58# @brief Delete lines matching `line_reg` when found within the expected region starting with `begin_reg` and `end_reg`
59{
60  delete_lines:
61
62      "$(line_reg)"
63        select_region => between( $(begin_reg), $(end_reg) );
64
65}
66
67body select_region between(start, end)
68# @brief Select a region exclusively between regular expressions `start` and `end`.
69{
70      select_start => "$(start)";
71      select_end => "$(end)";
72@if minimum_version(3.10)
73      select_end_match_eof => "false";
74@endif
75}
76
77body printfile cat(file)
78# @brief Report the contents of a file
79# @param file The full path of the file to report
80{
81        file_to_print => "$(file)";
82        number_of_lines => "inf";
83}
84#+end_src
85
86#+begin_src example_output
87#@ ```
88#@ R: /tmp/example_select_region.txt
89#@ R: MATCHING before region
90#@ R: BEGIN
91#@ R: END
92#@ R: MATCHING after region
93#@ ```
94#+end_src
95